tsondb 0.5.18 → 0.5.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/shared/utils/markdown.js +86 -110
- package/package.json +1 -1
|
@@ -1,74 +1,85 @@
|
|
|
1
1
|
const codeRule = {
|
|
2
2
|
pattern: /`(.*?)`/,
|
|
3
|
-
map:
|
|
3
|
+
map: result => ({
|
|
4
|
+
kind: "code",
|
|
5
|
+
content: result[1] ?? "",
|
|
6
|
+
}),
|
|
7
|
+
mapHighlighting: result => ({
|
|
4
8
|
kind: "code",
|
|
5
|
-
content:
|
|
9
|
+
content: `\`${result[1] ?? ""}\``,
|
|
6
10
|
}),
|
|
7
11
|
};
|
|
8
12
|
const boldWithItalicRule = {
|
|
9
13
|
pattern: /(?<!\\)\*\*((.*?[^\\*])?\*(?!\*).*?[^\\*]\*.*?)(?<!\\)\*\*/,
|
|
10
|
-
map: (result, parseInside
|
|
14
|
+
map: (result, parseInside) => ({
|
|
15
|
+
kind: "bold",
|
|
16
|
+
content: parseInside(result[1] ?? ""),
|
|
17
|
+
}),
|
|
18
|
+
mapHighlighting: (result, parseInside) => ({
|
|
11
19
|
kind: "bold",
|
|
12
|
-
content:
|
|
13
|
-
? [
|
|
14
|
-
{ kind: "text", content: "**" },
|
|
15
|
-
...parseInside(result[1] ?? ""),
|
|
16
|
-
{ kind: "text", content: "**" },
|
|
17
|
-
]
|
|
18
|
-
: parseInside(result[1] ?? ""),
|
|
20
|
+
content: [textNode("**"), ...parseInside(result[1] ?? ""), textNode("**")],
|
|
19
21
|
}),
|
|
20
22
|
};
|
|
21
23
|
const italicWithBoldRule = {
|
|
22
24
|
pattern: /(?<![\\*])\*(?=\*\*|[^*])(.*?\*\*.*?\*\*.*?)(?<=[^\\*]|[^\\]\*\*)\*(?!\*)/,
|
|
23
|
-
map: (result, parseInside
|
|
25
|
+
map: (result, parseInside) => ({
|
|
24
26
|
kind: "italic",
|
|
25
|
-
content:
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
]
|
|
31
|
-
: parseInside(result[1] ?? ""),
|
|
27
|
+
content: parseInside(result[1] ?? ""),
|
|
28
|
+
}),
|
|
29
|
+
mapHighlighting: (result, parseInside) => ({
|
|
30
|
+
kind: "italic",
|
|
31
|
+
content: [textNode("*"), ...parseInside(result[1] ?? ""), textNode("*")],
|
|
32
32
|
}),
|
|
33
33
|
};
|
|
34
34
|
const boldRule = {
|
|
35
35
|
pattern: /(?<!\\)\*\*(.*?[^\\*])\*\*/,
|
|
36
|
-
map: (result, parseInside
|
|
36
|
+
map: (result, parseInside) => ({
|
|
37
|
+
kind: "bold",
|
|
38
|
+
content: parseInside(result[1] ?? ""),
|
|
39
|
+
}),
|
|
40
|
+
mapHighlighting: (result, parseInside) => ({
|
|
37
41
|
kind: "bold",
|
|
38
|
-
content:
|
|
39
|
-
? [
|
|
40
|
-
{ kind: "text", content: "**" },
|
|
41
|
-
...parseInside(result[1] ?? ""),
|
|
42
|
-
{ kind: "text", content: "**" },
|
|
43
|
-
]
|
|
44
|
-
: parseInside(result[1] ?? ""),
|
|
42
|
+
content: [textNode("**"), ...parseInside(result[1] ?? ""), textNode("**")],
|
|
45
43
|
}),
|
|
46
44
|
};
|
|
47
45
|
const italicRule = {
|
|
48
46
|
pattern: /(?<!\\)\*(.*?[^\\*])\*/,
|
|
49
|
-
map: (result, parseInside
|
|
47
|
+
map: (result, parseInside) => ({
|
|
50
48
|
kind: "italic",
|
|
51
|
-
content:
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
]
|
|
57
|
-
: parseInside(result[1] ?? ""),
|
|
49
|
+
content: parseInside(result[1] ?? ""),
|
|
50
|
+
}),
|
|
51
|
+
mapHighlighting: (result, parseInside) => ({
|
|
52
|
+
kind: "italic",
|
|
53
|
+
content: [textNode("*"), ...parseInside(result[1] ?? ""), textNode("*")],
|
|
58
54
|
}),
|
|
59
55
|
};
|
|
60
56
|
const linkRule = {
|
|
61
57
|
pattern: /(?<!\\)\[(.*?[^\\])\]\((.*?[^\\])\)/,
|
|
62
|
-
map: (result, parseInside
|
|
58
|
+
map: (result, parseInside) => ({
|
|
59
|
+
kind: "link",
|
|
60
|
+
href: result[2] ?? "",
|
|
61
|
+
content: parseInside(result[1] ?? ""),
|
|
62
|
+
}),
|
|
63
|
+
mapHighlighting: (result, parseInside) => ({
|
|
63
64
|
kind: "link",
|
|
64
65
|
href: result[2] ?? "",
|
|
65
|
-
content:
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
content: [textNode("["), ...parseInside(result[1] ?? ""), textNode(`](${result[2] ?? ""})`)],
|
|
67
|
+
}),
|
|
68
|
+
};
|
|
69
|
+
const textNode = (content) => ({
|
|
70
|
+
kind: "text",
|
|
71
|
+
content: content,
|
|
72
|
+
});
|
|
73
|
+
const parseEscapedCharacters = (text) => text.replace(/\\([*_`[\]()\\])/g, "$1");
|
|
74
|
+
const textRule = {
|
|
75
|
+
pattern: /.+/,
|
|
76
|
+
map: result => ({
|
|
77
|
+
kind: "text",
|
|
78
|
+
content: parseEscapedCharacters(result[0]),
|
|
79
|
+
}),
|
|
80
|
+
mapHighlighting: result => ({
|
|
81
|
+
kind: "text",
|
|
82
|
+
content: result[0],
|
|
72
83
|
}),
|
|
73
84
|
};
|
|
74
85
|
const inlineRules = [
|
|
@@ -78,15 +89,13 @@ const inlineRules = [
|
|
|
78
89
|
italicWithBoldRule,
|
|
79
90
|
boldRule,
|
|
80
91
|
italicRule,
|
|
92
|
+
textRule,
|
|
81
93
|
];
|
|
82
94
|
const parseForInlineRules = (rules, text, forSyntaxHighlighting) => {
|
|
83
|
-
if (text.length === 0) {
|
|
95
|
+
if (text.length === 0 || rules[0] === undefined) {
|
|
84
96
|
return [];
|
|
85
97
|
}
|
|
86
98
|
const activeRule = rules[0];
|
|
87
|
-
if (activeRule === undefined) {
|
|
88
|
-
return [{ kind: "text", content: text }];
|
|
89
|
-
}
|
|
90
99
|
const res = activeRule.pattern.exec(text);
|
|
91
100
|
if (res && (activeRule.predicate?.(res) ?? true)) {
|
|
92
101
|
const { index } = res;
|
|
@@ -96,7 +105,7 @@ const parseForInlineRules = (rules, text, forSyntaxHighlighting) => {
|
|
|
96
105
|
...(before.length > 0
|
|
97
106
|
? parseForInlineRules(rules.slice(1), before, forSyntaxHighlighting)
|
|
98
107
|
: []),
|
|
99
|
-
activeRule.map(res, text => parseForInlineRules(rules.slice(1), text, forSyntaxHighlighting)
|
|
108
|
+
(forSyntaxHighlighting ? activeRule.mapHighlighting : activeRule.map)(res, text => parseForInlineRules(rules.slice(1), text, forSyntaxHighlighting)),
|
|
100
109
|
...(after.length > 0 ? parseForInlineRules(rules, after, forSyntaxHighlighting) : []),
|
|
101
110
|
];
|
|
102
111
|
}
|
|
@@ -122,9 +131,9 @@ const listRule = {
|
|
|
122
131
|
content: /^(\d+\. |[-*] )/.exec(item)?.[1] ?? "",
|
|
123
132
|
},
|
|
124
133
|
...parseInlineMarkdown(item.replace(/^\d+\. |[-*] /, ""), true),
|
|
125
|
-
...(index < array.length - 1 ? [
|
|
134
|
+
...(index < array.length - 1 ? [textNode("\n")] : []),
|
|
126
135
|
]),
|
|
127
|
-
|
|
136
|
+
textNode(result[2] ?? ""),
|
|
128
137
|
],
|
|
129
138
|
};
|
|
130
139
|
const paragraphRule = {
|
|
@@ -135,10 +144,14 @@ const paragraphRule = {
|
|
|
135
144
|
}),
|
|
136
145
|
mapHighlighting: result => [
|
|
137
146
|
...parseInlineMarkdown(result[1] ?? "", true),
|
|
138
|
-
|
|
147
|
+
textNode(result[2] ?? ""),
|
|
139
148
|
],
|
|
140
149
|
};
|
|
141
150
|
const removeSurroundingPipes = (text) => text.replace(/^\|/, "").replace(/\|$/, "");
|
|
151
|
+
const tableMarker = (text) => ({
|
|
152
|
+
kind: "tablemarker",
|
|
153
|
+
content: text,
|
|
154
|
+
});
|
|
142
155
|
const tableRule = {
|
|
143
156
|
pattern: /^(\| *)?(.+?(?: *(?<!\\)\| *.+?)+)( *\|)?\n((?:\| *)?(?:-{3,}|:-{2,}|-{2,}:|:-+:)(?: *\| *(?:-{3,}|:-{2,}|-{2,}:|:-+:))*(?: *\|)?)((?:\n\|? *.+?(?: *(?<!\\)\| *.+?)* *(?<!\\)\|?)+)(\n{2,}|$)/,
|
|
144
157
|
map: result => ({
|
|
@@ -152,86 +165,49 @@ const tableRule = {
|
|
|
152
165
|
.map(tc => parseInlineMarkdown(tc.trim(), false))) ?? [],
|
|
153
166
|
}),
|
|
154
167
|
mapHighlighting: result => [
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
...(result[2]?.split("|").flatMap((th, i) => i === 0
|
|
168
|
+
tableMarker(result[1] ?? ""),
|
|
169
|
+
...(result[2]
|
|
170
|
+
?.split("|")
|
|
171
|
+
.flatMap((th, i) => i === 0
|
|
160
172
|
? parseInlineMarkdown(th, true)
|
|
161
|
-
: [
|
|
162
|
-
|
|
163
|
-
kind: "tablemarker",
|
|
164
|
-
content: "|",
|
|
165
|
-
},
|
|
166
|
-
...parseInlineMarkdown(th, true),
|
|
167
|
-
]) ?? []),
|
|
168
|
-
{
|
|
169
|
-
kind: "tablemarker",
|
|
170
|
-
content: (result[3] ?? "") + "\n" + (result[4] ?? ""),
|
|
171
|
-
},
|
|
173
|
+
: [tableMarker("|"), ...parseInlineMarkdown(th, true)]) ?? []),
|
|
174
|
+
tableMarker((result[3] ?? "") + "\n" + (result[4] ?? "")),
|
|
172
175
|
...(result[5]
|
|
173
176
|
?.split("\n")
|
|
174
177
|
.slice(1)
|
|
175
178
|
.flatMap((tr) => [
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
...tr.split("|").flatMap((tc, i) => i === 0
|
|
179
|
+
textNode("\n"),
|
|
180
|
+
...tr
|
|
181
|
+
.split("|")
|
|
182
|
+
.flatMap((tc, i) => i === 0
|
|
181
183
|
? parseInlineMarkdown(tc, true)
|
|
182
|
-
: [
|
|
183
|
-
{
|
|
184
|
-
kind: "tablemarker",
|
|
185
|
-
content: "|",
|
|
186
|
-
},
|
|
187
|
-
...parseInlineMarkdown(tc, true),
|
|
188
|
-
]),
|
|
184
|
+
: [tableMarker("|"), ...parseInlineMarkdown(tc, true)]),
|
|
189
185
|
]) ?? []),
|
|
190
|
-
|
|
191
|
-
kind: "text",
|
|
192
|
-
content: result[6] ?? "",
|
|
193
|
-
},
|
|
186
|
+
textNode(result[6] ?? ""),
|
|
194
187
|
],
|
|
195
188
|
};
|
|
196
189
|
const blockRules = [tableRule, listRule, paragraphRule];
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (
|
|
203
|
-
return [{ kind: "paragraph", content: [{ kind: "text", content: text }] }];
|
|
204
|
-
}
|
|
205
|
-
const res = activeRule.pattern.exec(text);
|
|
206
|
-
if (res && (activeRule.predicate?.(res) ?? true)) {
|
|
207
|
-
const { index } = res;
|
|
208
|
-
const after = text.slice(index + res[0].length);
|
|
209
|
-
return [activeRule.map(res), ...(after.length > 0 ? parseForBlockRules(rules, after) : [])];
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
return parseForBlockRules(rules, text, remainingRules.slice(1));
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
const parseForBlockRulesSyntaxHighlighting = (rules, text, remainingRules = rules) => {
|
|
216
|
-
if (text.length === 0) {
|
|
190
|
+
const parseActiveBlockRule = (rule, res) => [
|
|
191
|
+
rule.map(res),
|
|
192
|
+
];
|
|
193
|
+
const parseActiveBlockSyntaxRule = (rule, res) => rule.mapHighlighting(res);
|
|
194
|
+
const parseForBlockRules = (rules, text, ruleParser, remainingRules = rules) => {
|
|
195
|
+
if (text.length === 0 || remainingRules[0] === undefined) {
|
|
217
196
|
return [];
|
|
218
197
|
}
|
|
219
198
|
const activeRule = remainingRules[0];
|
|
220
|
-
if (activeRule === undefined) {
|
|
221
|
-
return [{ kind: "text", content: text }];
|
|
222
|
-
}
|
|
223
199
|
const res = activeRule.pattern.exec(text);
|
|
224
200
|
if (res && (activeRule.predicate?.(res) ?? true)) {
|
|
225
201
|
const { index } = res;
|
|
226
202
|
const after = text.slice(index + res[0].length);
|
|
227
203
|
return [
|
|
228
|
-
...activeRule
|
|
229
|
-
...(after.length > 0 ?
|
|
204
|
+
...ruleParser(activeRule, res),
|
|
205
|
+
...(after.length > 0 ? parseForBlockRules(rules, after, ruleParser) : []),
|
|
230
206
|
];
|
|
231
207
|
}
|
|
232
208
|
else {
|
|
233
|
-
return
|
|
209
|
+
return parseForBlockRules(rules, text, ruleParser, remainingRules.slice(1));
|
|
234
210
|
}
|
|
235
211
|
};
|
|
236
|
-
export const parseBlockMarkdown = (text) => parseForBlockRules(blockRules, text);
|
|
237
|
-
export const parseBlockMarkdownForSyntaxHighlighting = (text) =>
|
|
212
|
+
export const parseBlockMarkdown = (text) => parseForBlockRules(blockRules, text, parseActiveBlockRule);
|
|
213
|
+
export const parseBlockMarkdownForSyntaxHighlighting = (text) => parseForBlockRules(blockRules, text, parseActiveBlockSyntaxRule);
|