-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnevula.ts
399 lines (356 loc) · 11.4 KB
/
nevula.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/** Checks if `largeSpan` can contain `smallSpan` */
export function containsSpan(largeSpan: Span, smallSpan: Span): boolean {
return largeSpan.start <= smallSpan.start && smallSpan.end <= largeSpan.end;
}
/** Assertion util for the ts compiler to tell it that it should never happen */
export class UnreachableCaseError extends Error {
constructor(val: never) {
super(`Unreachable case: ${JSON.stringify(val)}`);
}
}
/** Partition a list into two parts based on a boolean: `[true, false]` */
export function partition<T>(list: T[], filter: (item: T) => boolean) {
let result: [T[], T[]] = [[], []];
for (let i = 0; i < list.length; i++) {
const item = list[i];
if (filter(item)) {
result[0].push(item);
} else {
result[1].push(item);
}
}
return result;
}
/** A span, similar to ranges in other languages */
export type Span = { start: number; end: number };
/** A generic type meant to make making entities easier and more consistent */
export type EntityType<N, T = {}> = {
/** The entity type */
type: N;
/** The inner text span, similar to innerText */
innerSpan: Span;
/** The span of the outer text, similar to outerHTML */
outerSpan: Span;
/** The list of entities that this entity contains */
entities: Entity[];
/** Params to configure the entity */
params: T;
};
/** A text entity */
export type Entity =
| EntityType<"text">
| EntityType<"bold">
| EntityType<"italic">
| EntityType<"underline">
| EntityType<"strikethrough">
| EntityType<"code">
| EntityType<"codeblock", {
// todo: describe better
/** What language highlighting should be used to highlight the codeblock */
lang?: string;
}>
| EntityType<"blockquote", {
/** Not currently used, only typed for spec complience */
borderColor?: string;
}>
| EntityType<"custom", {
/** The custom expression type */
type: string;
}>;
/**
* Generate a global regex from a record
*
* each name will become a named capture group
* */
const generateRegex = (parts: Record<string, RegExp>) => {
// todo: named grouped regexep's can be slow
return RegExp(
Object.entries(parts)
.map(([type, pattern]) => `(${pattern.source})`)
.join("|"),
"g",
);
};
function generateMapping<T extends string>(parts: Record<T, RegExp>): T[] {
return [...Object.keys(parts)] as T[];
}
const TOKEN_PARTS = {
escape: /\\[\\*/_~`\[\]]/,
bold: /\*\*/,
italic: /\/\//,
underline: /__/,
strikethrough: /~~/,
codeblock: /```/,
code: /``/,
custom_start: /\[(?:.|\w+):/,
custom_end: /]/,
newline: /\r?\n/,
};
// todo: manually do this
const TOKENS = generateRegex(TOKEN_PARTS);
const TYPES = generateMapping(TOKEN_PARTS);
function tokenType(token: RegExpMatchArray) {
return TYPES[token.findIndex((g, i) => i != 0 && g != null) - 1];
}
/** A marker used for identifying and matching tokens */
export type Marker = {
type: "bold" | "italic" | "underline" | "strikethrough" | "blockquote";
span: Span;
};
/**
* Parses a string into entities
* @returns A root text entitiy, meant to make entity rendering easier
*/
export function parseMarkup(text: string): Entity {
let markers: Marker[] = [];
let entities: Entity[] = [];
let tokens = [...text.matchAll(TOKENS)];
/** checks if a line is the beginning to or the end of a blockquote */
function parseLine(indice: Span) {
const markerIndex = markers.findIndex((m) => m.type === "blockquote");
// todo: this is nearly the same as the bold, italic, etc... family of parsing rules and can likely be simplified into a function
// for now though I'm keeping it like this for performance and the fear of micro-drying
if (markerIndex >= 0) {
const marker = markers[markerIndex];
const innerSpan = { start: marker.span.end, end: indice.start };
const outerSpan = { start: marker.span.start, end: indice.end };
const [innerEntities, remainingEntities] = partition(
entities,
(e) => containsSpan(outerSpan, e.outerSpan),
);
markers.splice(markerIndex);
entities = remainingEntities;
entities.push({
type: "blockquote",
innerSpan,
outerSpan,
entities: innerEntities,
params: {},
});
}
if (text.startsWith("> ", indice.end)) {
markers.push({
type: "blockquote",
span: { start: indice.start, end: indice.end + 2 },
});
}
}
if (!tokens) throw new Error("Did not parse...");
parseLine({ start: 0, end: 0 });
for (let pos = 0; pos < tokens.length; pos++) {
const token = tokens[pos];
const type = tokenType(token);
if (token.index == null) {
throw new Error(`No index found for token: ${token}`);
}
const indice: Span = {
start: token.index,
end: token.index + token[0].length,
};
switch (type) {
// newline is first because it's the most common and should match first for performance
case "newline":
parseLine(indice);
break;
case "bold":
case "italic":
case "underline":
case "strikethrough": {
const markerIndex = markers.findIndex((m) => m.type === type);
if (markerIndex >= 0) {
const marker = markers[markerIndex];
const innerSpan = { start: marker.span.end, end: indice.start };
const outerSpan = { start: marker.span.start, end: indice.end };
const [innerEntities, remainingEntities] = partition(
entities,
(e) => containsSpan(outerSpan, e.innerSpan),
);
markers.splice(markerIndex);
entities = remainingEntities;
entities.push({
type,
innerSpan,
outerSpan,
entities: innerEntities,
params: {},
});
} else {
markers.push({
type: type,
span: indice,
});
}
break;
}
case "code": {
// because code doesn't have innerEntities, we can skip parsing those tokens
const markerIndex = tokens.findIndex((t, i) =>
i > pos && tokenType(t) === "code"
);
if (markerIndex >= 0) {
const escapes = tokens.slice(pos, markerIndex).filter((e) =>
tokenType(e) === "escape"
);
const endToken = tokens[markerIndex];
const endIndice: Span = {
start: endToken.index!,
end: endToken.index! + endToken[0].length,
};
// todo: write a better system that's more generalized for escaping
entities.push({
type: "code",
innerSpan: { start: indice.end, end: endIndice.start },
outerSpan: { start: indice.start, end: endIndice.end },
entities: escapes.map((m) => ({
type: "text",
innerSpan: { start: m.index! + 1, end: m.index! + m[0].length },
outerSpan: { start: m.index!, end: m.index! + m[0].length },
entities: [],
params: {},
})),
params: {},
});
pos = markerIndex;
}
break;
}
case "codeblock": {
// find the matching token
const markerIndex = tokens.findIndex((t, i) =>
i > pos && tokenType(t) === "codeblock"
);
if (markerIndex >= 0) {
const escapes = tokens.slice(pos, markerIndex).filter((e) =>
tokenType(e) === "escape"
);
const endToken = tokens[markerIndex];
const endIndice: Span = {
start: endToken.index!,
end: endToken.index! + endToken[0].length,
};
// get lang param
const langRegex = /\w*\r?\n/y;
langRegex.lastIndex = indice.end;
const args = langRegex.exec(text);
// remove the \n
const lang = args?.[0]?.trim();
entities.push({
type: "codeblock",
// add the lang length to the innerSpan start to skip that when getting the text
innerSpan: {
start: indice.end + (args?.[0]?.length ?? 0),
end: endIndice.start,
},
outerSpan: { start: indice.start, end: endIndice.end },
entities: escapes.map((m) => ({
type: "text",
innerSpan: { start: m.index! + 1, end: m.index! + m[0].length },
outerSpan: { start: m.index!, end: m.index! + m[0].length },
entities: [],
params: {},
})),
params: {
lang: lang,
},
});
pos = markerIndex;
}
break;
}
case "custom_start": {
const markerIndex = tokens.findIndex((t, i) =>
i > pos && tokenType(t) === "custom_end"
);
if (markerIndex >= 0) {
const escapes = tokens.slice(pos, markerIndex).filter((e) =>
tokenType(e) === "escape"
);
const endToken = tokens[markerIndex];
const endIndice: Span = {
start: endToken.index!,
end: endToken.index! + endToken[0].length,
};
entities.push({
type: "custom",
innerSpan: { start: indice.end, end: endIndice.start },
outerSpan: { start: indice.start, end: endIndice.end },
entities: escapes.map((m) => ({
type: "text",
innerSpan: { start: m.index! + 1, end: m.index! + m[0].length },
outerSpan: { start: m.index!, end: m.index! + m[0].length },
entities: [],
params: {},
})),
params: { type: token[0].slice(1, -1) },
});
pos = markerIndex;
}
break;
}
case "escape": {
const span = { start: indice.start + 1, end: indice.end };
entities.push({
type: "text",
innerSpan: span,
outerSpan: indice,
entities: [],
params: {},
});
break;
}
// skip custom_end, it's not used for matching anything behind it
case "custom_end":
break;
default:
throw new UnreachableCaseError(type);
}
}
parseLine({ start: text.length, end: text.length });
return ({
type: "text",
innerSpan: { start: 0, end: text.length },
outerSpan: { start: 0, end: text.length },
entities: entities,
params: {},
});
}
/** modifies an entity's entities to add text spans */
export function addTextSpans(entity: Entity): Entity {
if (entity.entities.length === 0 && entity.type === "text") {
return entity;
}
let entities: Entity[] = [];
for (let i = 0; i < entity.entities.length; i++) {
const e = entity.entities[i];
const textSpan = {
start: entities[entities.length - 1]?.outerSpan.end ??
entity.innerSpan.start,
end: e.outerSpan.start,
};
if (textSpan.end > textSpan.start) {
entities.push({
type: "text",
innerSpan: textSpan,
outerSpan: textSpan,
entities: [],
params: {},
});
}
entities.push(addTextSpans(e));
}
const endingTextSpan = {
start: entity.entities[entity.entities.length - 1]?.outerSpan.end ??
entity.innerSpan.start,
end: entity.innerSpan.end,
};
if (endingTextSpan.end > endingTextSpan.start) {
entities.push({
type: "text",
innerSpan: endingTextSpan,
outerSpan: endingTextSpan,
entities: [],
params: {},
});
}
return { ...entity, entities };
}