deno.land / x / lume@v2.1.4 / deps / katex-auto-render / splitAtDelimiters.js

splitAtDelimiters.js
نووسراو ببینە
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
/* eslint no-constant-condition:0 */const findEndOfMath = function(delimiter, text, startIndex) { // Adapted from // https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx let index = startIndex; let braceLevel = 0;
const delimLength = delimiter.length;
while (index < text.length) { const character = text[index];
if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) { return index; } else if (character === "\\") { index++; } else if (character === "{") { braceLevel++; } else if (character === "}") { braceLevel--; }
index++; }
return -1;};
const escapeRegex = function(string) { return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");};
const amsRegex = /^\\begin{/;
const splitAtDelimiters = function(text, delimiters) { let index; const data = [];
const regexLeft = new RegExp( "(" + delimiters.map((x) => escapeRegex(x.left)).join("|") + ")" );
while (true) { index = text.search(regexLeft); if (index === -1) { break; } if (index > 0) { data.push({ type: "text", data: text.slice(0, index), }); text = text.slice(index); // now text starts with delimiter } // ... so this always succeeds: const i = delimiters.findIndex((delim) => text.startsWith(delim.left)); index = findEndOfMath(delimiters[i].right, text, delimiters[i].left.length); if (index === -1) { break; } const rawData = text.slice(0, index + delimiters[i].right.length); const math = amsRegex.test(rawData) ? rawData : text.slice(delimiters[i].left.length, index); data.push({ type: "math", data: math, rawData, display: delimiters[i].display, }); text = text.slice(index + delimiters[i].right.length); }
if (text !== "") { data.push({ type: "text", data: text, }); }
return data;};
export default splitAtDelimiters;
lume

Version Info

Tagged at
7 months ago