-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar_markdown.ts
More file actions
290 lines (264 loc) · 6.83 KB
/
grammar_markdown.ts
File metadata and controls
290 lines (264 loc) · 6.83 KB
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
import type {
AddSyntaxGrammar,
SyntaxGrammarTokenRaw,
SyntaxGrammarValueRaw,
SyntaxStyler,
} from './syntax_styler.js';
interface LangDefinition {
aliases: Array<string>;
id: string;
}
interface FenceType {
backticks: string;
suffix: string;
}
/**
* Helper to create fenced code block pattern for a language.
*/
const create_fence_pattern = (
backticks: string,
aliases: Array<string>,
lang_id: string,
syntax_styler: SyntaxStyler,
): SyntaxGrammarTokenRaw => {
const aliases_pattern = aliases.join('|');
const pattern = new RegExp(
`^${backticks}(?:${aliases_pattern})[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`,
'm',
);
const code_fence_pattern = new RegExp(`^${backticks}[^\\n\\r]*|^${backticks}$`, 'm');
return {
pattern,
greedy: true,
inside: {
code_fence: {
pattern: code_fence_pattern,
alias: 'punctuation',
},
[`lang_${lang_id}`]: {
pattern: /[\s\S]+/,
inside: syntax_styler.get_lang(lang_id),
},
},
};
};
/**
* Helper to create catch-all fence pattern (unknown languages).
*/
const create_catchall_fence = (backticks: string): SyntaxGrammarTokenRaw => {
const pattern = new RegExp(`^${backticks}[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`, 'm');
const code_fence_pattern = new RegExp(`^${backticks}[^\\n\\r]*|^${backticks}$`, 'm');
return {
pattern,
greedy: true,
inside: {
code_fence: {
pattern: code_fence_pattern,
alias: 'punctuation',
},
},
};
};
/**
* Helper to create md self-reference placeholder pattern.
*/
const create_md_placeholder = (backticks: string): SyntaxGrammarTokenRaw => {
const pattern = new RegExp(
`^${backticks}(?:md|markdown)[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`,
'm',
);
const code_fence_pattern = new RegExp(`^${backticks}[^\\n\\r]*|^${backticks}$`, 'm');
return {
pattern,
greedy: true,
inside: {
code_fence: {
pattern: code_fence_pattern,
alias: 'punctuation',
},
// lang_md will be added after registration
},
};
};
/**
* Markdown grammar extending markup.
* Supports: headings, fenced code blocks (3/4/5 backticks with nesting), lists, blockquotes,
* bold, italic, strikethrough, inline code, and links.
*/
export const add_grammar_markdown: AddSyntaxGrammar = (syntax_styler) => {
// Language definitions with aliases
const langs: Array<LangDefinition> = [
{aliases: ['ts', 'typescript'], id: 'ts'},
{aliases: ['js', 'javascript'], id: 'js'},
{aliases: ['css'], id: 'css'},
{aliases: ['html', 'markup'], id: 'markup'},
{aliases: ['json'], id: 'json'},
{aliases: ['svelte'], id: 'svelte'},
{aliases: ['bash', 'sh', 'shell'], id: 'bash'},
];
// Fence types: higher counts first (for proper precedence in tokenization)
const fence_types: Array<FenceType> = [
{backticks: '`````', suffix: '5tick'},
{backticks: '````', suffix: '4tick'},
{backticks: '```', suffix: ''},
];
// Build grammar dynamically
const grammar: Record<string, SyntaxGrammarValueRaw> = {};
const md_self_refs: Array<string> = []; // Track md patterns for later self-reference
// Generate fence patterns for each type
for (const {backticks, suffix} of fence_types) {
const token_suffix = suffix ? `_${suffix}` : '';
// md pattern (self-reference added after registration)
const md_key = `fenced_code${token_suffix}_md`;
grammar[md_key] = create_md_placeholder(backticks);
md_self_refs.push(md_key);
// Other language patterns (use first alias as token name for backward compatibility)
for (const {aliases, id} of langs) {
const token_name = aliases[0]; // Use first alias for token name
grammar[`fenced_code${token_suffix}_${token_name}`] = create_fence_pattern(
backticks,
aliases,
id,
syntax_styler,
);
}
// Catch-all fence
grammar[`fenced_code${token_suffix}`] = create_catchall_fence(backticks);
}
// Register markdown grammar first, then add self-references for md fences
const grammar_md = syntax_styler.add_extended_lang(
'markup',
'md',
{
...grammar,
// Headings (# through ######)
heading: {
pattern: /^#{1,6}\s+.+$/m,
inside: {
punctuation: {
pattern: /^#{1,6}/,
alias: 'heading_punctuation',
},
},
},
// Blockquotes (> at line start)
blockquote: {
pattern: /^>\s*.+$/m,
inside: {
punctuation: {
pattern: /^>/,
alias: 'blockquote_punctuation',
},
},
},
// Lists (* or - with any preceding whitespace)
list: {
pattern: /^\s*[-*]\s+.+$/m,
inside: {
punctuation: /^\s*[-*]/,
},
},
// Inline elements
// Links [text](url)
link: {
pattern: /\[[^[\]\n\r]+\]\([^)\n\r]+\)/,
inside: {
link_text_wrapper: {
pattern: /^\[[^\]]+\]/,
inside: {
punctuation: {
pattern: /^\[|\]$/,
alias: 'link_punctuation',
},
link_text: /[^[\]]+/,
},
},
url_wrapper: {
pattern: /\([^)]+\)$/,
inside: {
punctuation: {
pattern: /^\(|\)$/,
alias: 'link_punctuation',
},
url: /[^()]+/,
},
},
},
},
// Inline code `text`
inline_code: {
pattern: /`[^`\n\r]+`/,
alias: 'code',
inside: {
punctuation: {
pattern: /^`|`$/,
alias: 'code_punctuation',
},
content: /[^`]+/,
},
},
// Bold **text** or __text__
bold: [
{
// **text** - no asterisks inside
pattern: /\*\*[^\s*][^*]*[^\s*]\*\*|\*\*[^\s*]\*\*/,
inside: {
punctuation: /^\*\*|\*\*$/,
},
},
{
// __text__ - at word boundaries, no underscores inside
pattern: /\b__[^\s_][^_]*[^\s_]__\b|\b__[^\s_]__\b/,
inside: {
punctuation: /^__|__$/,
},
},
],
// Italic *text* or _text_
italic: [
{
// *text* - no asterisks inside
pattern: /\*[^\s*][^*]*[^\s*]\*|\*[^\s*]\*/,
inside: {
punctuation: /^\*|\*$/,
},
},
{
// _text_ - at word boundaries, no underscores inside
pattern: /\b_[^\s_][^_]*[^\s_]_\b|\b_[^\s_]_\b/,
inside: {
punctuation: /^_|_$/,
},
},
],
// Strikethrough ~~text~~
strikethrough: {
pattern: /~~[^\s~][^~]*[^\s~]~~|~~[^\s~]~~/,
inside: {
punctuation: /^~~|~~$/,
},
},
},
['markdown'],
);
// Add self-reference for markdown-in-markdown (all fence types)
// This must be done after registration to avoid circular dependency
const lang_md_inside = {
pattern: /[\s\S]+/,
inside: syntax_styler.get_lang('md'),
};
for (const key of md_self_refs) {
// After normalization, grammar values are arrays of SyntaxGrammarToken
// We need to add lang_md as a normalized array
const patterns = grammar_md[key]!;
// Manually normalize the lang_md pattern we're adding
const lang_md_token = {
pattern: lang_md_inside.pattern,
lookbehind: false,
greedy: false,
alias: [],
inside: lang_md_inside.inside,
};
patterns[0]!.inside!.lang_md = [lang_md_token];
}
};