@fuzdev/fuz_code 0.37.0
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/LICENSE +25 -0
- package/README.md +185 -0
- package/dist/Code.svelte +146 -0
- package/dist/Code.svelte.d.ts +79 -0
- package/dist/Code.svelte.d.ts.map +1 -0
- package/dist/CodeHighlight.svelte +205 -0
- package/dist/CodeHighlight.svelte.d.ts +101 -0
- package/dist/CodeHighlight.svelte.d.ts.map +1 -0
- package/dist/code_sample.d.ts +8 -0
- package/dist/code_sample.d.ts.map +1 -0
- package/dist/code_sample.js +2 -0
- package/dist/grammar_clike.d.ts +12 -0
- package/dist/grammar_clike.d.ts.map +1 -0
- package/dist/grammar_clike.js +43 -0
- package/dist/grammar_css.d.ts +11 -0
- package/dist/grammar_css.d.ts.map +1 -0
- package/dist/grammar_css.js +70 -0
- package/dist/grammar_js.d.ts +11 -0
- package/dist/grammar_js.d.ts.map +1 -0
- package/dist/grammar_js.js +180 -0
- package/dist/grammar_json.d.ts +11 -0
- package/dist/grammar_json.d.ts.map +1 -0
- package/dist/grammar_json.js +35 -0
- package/dist/grammar_markdown.d.ts +8 -0
- package/dist/grammar_markdown.d.ts.map +1 -0
- package/dist/grammar_markdown.js +228 -0
- package/dist/grammar_markup.d.ts +31 -0
- package/dist/grammar_markup.d.ts.map +1 -0
- package/dist/grammar_markup.js +192 -0
- package/dist/grammar_svelte.d.ts +12 -0
- package/dist/grammar_svelte.d.ts.map +1 -0
- package/dist/grammar_svelte.js +150 -0
- package/dist/grammar_ts.d.ts +11 -0
- package/dist/grammar_ts.d.ts.map +1 -0
- package/dist/grammar_ts.js +95 -0
- package/dist/highlight_manager.d.ts +25 -0
- package/dist/highlight_manager.d.ts.map +1 -0
- package/dist/highlight_manager.js +139 -0
- package/dist/highlight_priorities.d.ts +3 -0
- package/dist/highlight_priorities.d.ts.map +1 -0
- package/dist/highlight_priorities.gen.d.ts +4 -0
- package/dist/highlight_priorities.gen.d.ts.map +1 -0
- package/dist/highlight_priorities.gen.js +58 -0
- package/dist/highlight_priorities.js +55 -0
- package/dist/syntax_styler.d.ts +277 -0
- package/dist/syntax_styler.d.ts.map +1 -0
- package/dist/syntax_styler.js +426 -0
- package/dist/syntax_styler_global.d.ts +3 -0
- package/dist/syntax_styler_global.d.ts.map +1 -0
- package/dist/syntax_styler_global.js +18 -0
- package/dist/syntax_token.d.ts +34 -0
- package/dist/syntax_token.d.ts.map +1 -0
- package/dist/syntax_token.js +27 -0
- package/dist/theme.css +98 -0
- package/dist/theme_highlight.css +160 -0
- package/dist/theme_variables.css +20 -0
- package/dist/tokenize_syntax.d.ts +28 -0
- package/dist/tokenize_syntax.d.ts.map +1 -0
- package/dist/tokenize_syntax.js +194 -0
- package/package.json +117 -0
- package/src/lib/code_sample.ts +10 -0
- package/src/lib/grammar_clike.ts +48 -0
- package/src/lib/grammar_css.ts +84 -0
- package/src/lib/grammar_js.ts +215 -0
- package/src/lib/grammar_json.ts +38 -0
- package/src/lib/grammar_markdown.ts +289 -0
- package/src/lib/grammar_markup.ts +225 -0
- package/src/lib/grammar_svelte.ts +165 -0
- package/src/lib/grammar_ts.ts +114 -0
- package/src/lib/highlight_manager.ts +182 -0
- package/src/lib/highlight_priorities.gen.ts +71 -0
- package/src/lib/highlight_priorities.ts +110 -0
- package/src/lib/syntax_styler.ts +583 -0
- package/src/lib/syntax_styler_global.ts +20 -0
- package/src/lib/syntax_token.ts +49 -0
- package/src/lib/tokenize_syntax.ts +270 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper to create fenced code block pattern for a language
|
|
3
|
+
*/
|
|
4
|
+
const create_fence_pattern = (backticks, aliases, lang_id, syntax_styler) => {
|
|
5
|
+
const aliases_pattern = aliases.join('|');
|
|
6
|
+
const pattern = new RegExp(`^${backticks}(?:${aliases_pattern})[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`, 'm');
|
|
7
|
+
const code_fence_pattern = new RegExp(`^${backticks}[^\\n\\r]*|^${backticks}$`, 'm');
|
|
8
|
+
return {
|
|
9
|
+
pattern,
|
|
10
|
+
greedy: true,
|
|
11
|
+
inside: {
|
|
12
|
+
code_fence: {
|
|
13
|
+
pattern: code_fence_pattern,
|
|
14
|
+
alias: 'punctuation',
|
|
15
|
+
},
|
|
16
|
+
[`lang_${lang_id}`]: {
|
|
17
|
+
pattern: /[\s\S]+/,
|
|
18
|
+
inside: syntax_styler.get_lang(lang_id),
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Helper to create catch-all fence pattern (unknown languages)
|
|
25
|
+
*/
|
|
26
|
+
const create_catchall_fence = (backticks) => {
|
|
27
|
+
const pattern = new RegExp(`^${backticks}[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`, 'm');
|
|
28
|
+
const code_fence_pattern = new RegExp(`^${backticks}[^\\n\\r]*|^${backticks}$`, 'm');
|
|
29
|
+
return {
|
|
30
|
+
pattern,
|
|
31
|
+
greedy: true,
|
|
32
|
+
inside: {
|
|
33
|
+
code_fence: {
|
|
34
|
+
pattern: code_fence_pattern,
|
|
35
|
+
alias: 'punctuation',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Helper to create md self-reference placeholder pattern
|
|
42
|
+
*/
|
|
43
|
+
const create_md_placeholder = (backticks) => {
|
|
44
|
+
const pattern = new RegExp(`^${backticks}(?:md|markdown)[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`, 'm');
|
|
45
|
+
const code_fence_pattern = new RegExp(`^${backticks}[^\\n\\r]*|^${backticks}$`, 'm');
|
|
46
|
+
return {
|
|
47
|
+
pattern,
|
|
48
|
+
greedy: true,
|
|
49
|
+
inside: {
|
|
50
|
+
code_fence: {
|
|
51
|
+
pattern: code_fence_pattern,
|
|
52
|
+
alias: 'punctuation',
|
|
53
|
+
},
|
|
54
|
+
// lang_md will be added after registration
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Markdown grammar extending markup.
|
|
60
|
+
* Supports: headings, fenced code blocks (3/4/5 backticks with nesting), lists, blockquotes,
|
|
61
|
+
* bold, italic, strikethrough, inline code, and links.
|
|
62
|
+
*/
|
|
63
|
+
export const add_grammar_markdown = (syntax_styler) => {
|
|
64
|
+
// Language definitions with aliases
|
|
65
|
+
const langs = [
|
|
66
|
+
{ aliases: ['ts', 'typescript'], id: 'ts' },
|
|
67
|
+
{ aliases: ['js', 'javascript'], id: 'js' },
|
|
68
|
+
{ aliases: ['css'], id: 'css' },
|
|
69
|
+
{ aliases: ['html', 'markup'], id: 'markup' },
|
|
70
|
+
{ aliases: ['json'], id: 'json' },
|
|
71
|
+
{ aliases: ['svelte'], id: 'svelte' },
|
|
72
|
+
];
|
|
73
|
+
// Fence types: higher counts first (for proper precedence in tokenization)
|
|
74
|
+
const fence_types = [
|
|
75
|
+
{ backticks: '`````', suffix: '5tick' },
|
|
76
|
+
{ backticks: '````', suffix: '4tick' },
|
|
77
|
+
{ backticks: '```', suffix: '' },
|
|
78
|
+
];
|
|
79
|
+
// Build grammar dynamically
|
|
80
|
+
const grammar = {};
|
|
81
|
+
const md_self_refs = []; // Track md patterns for later self-reference
|
|
82
|
+
// Generate fence patterns for each type
|
|
83
|
+
for (const { backticks, suffix } of fence_types) {
|
|
84
|
+
const token_suffix = suffix ? `_${suffix}` : '';
|
|
85
|
+
// md pattern (self-reference added after registration)
|
|
86
|
+
const md_key = `fenced_code${token_suffix}_md`;
|
|
87
|
+
grammar[md_key] = create_md_placeholder(backticks);
|
|
88
|
+
md_self_refs.push(md_key);
|
|
89
|
+
// Other language patterns (use first alias as token name for backward compatibility)
|
|
90
|
+
for (const { aliases, id } of langs) {
|
|
91
|
+
const token_name = aliases[0]; // Use first alias for token name
|
|
92
|
+
grammar[`fenced_code${token_suffix}_${token_name}`] = create_fence_pattern(backticks, aliases, id, syntax_styler);
|
|
93
|
+
}
|
|
94
|
+
// Catch-all fence
|
|
95
|
+
grammar[`fenced_code${token_suffix}`] = create_catchall_fence(backticks);
|
|
96
|
+
}
|
|
97
|
+
// Register markdown grammar first, then add self-references for md fences
|
|
98
|
+
const grammar_md = syntax_styler.add_extended_lang('markup', 'md', {
|
|
99
|
+
...grammar,
|
|
100
|
+
// Headings (# through ######)
|
|
101
|
+
heading: {
|
|
102
|
+
pattern: /^#{1,6}\s+.+$/m,
|
|
103
|
+
inside: {
|
|
104
|
+
punctuation: {
|
|
105
|
+
pattern: /^#{1,6}/,
|
|
106
|
+
alias: 'heading_punctuation',
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
// Blockquotes (> at line start)
|
|
111
|
+
blockquote: {
|
|
112
|
+
pattern: /^>\s*.+$/m,
|
|
113
|
+
inside: {
|
|
114
|
+
punctuation: {
|
|
115
|
+
pattern: /^>/,
|
|
116
|
+
alias: 'blockquote_punctuation',
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
// Lists (* or - with any preceding whitespace)
|
|
121
|
+
list: {
|
|
122
|
+
pattern: /^\s*[-*]\s+.+$/m,
|
|
123
|
+
inside: {
|
|
124
|
+
punctuation: /^\s*[-*]/,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
// Inline elements
|
|
128
|
+
// Links [text](url)
|
|
129
|
+
link: {
|
|
130
|
+
pattern: /\[[^[\]\n\r]+\]\([^)\n\r]+\)/,
|
|
131
|
+
inside: {
|
|
132
|
+
link_text_wrapper: {
|
|
133
|
+
pattern: /^\[[^\]]+\]/,
|
|
134
|
+
inside: {
|
|
135
|
+
punctuation: {
|
|
136
|
+
pattern: /^\[|\]$/,
|
|
137
|
+
alias: 'link_punctuation',
|
|
138
|
+
},
|
|
139
|
+
link_text: /[^[\]]+/,
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
url_wrapper: {
|
|
143
|
+
pattern: /\([^)]+\)$/,
|
|
144
|
+
inside: {
|
|
145
|
+
punctuation: {
|
|
146
|
+
pattern: /^\(|\)$/,
|
|
147
|
+
alias: 'link_punctuation',
|
|
148
|
+
},
|
|
149
|
+
url: /[^()]+/,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
// Inline code `text`
|
|
155
|
+
inline_code: {
|
|
156
|
+
pattern: /`[^`\n\r]+`/,
|
|
157
|
+
alias: 'code',
|
|
158
|
+
inside: {
|
|
159
|
+
punctuation: {
|
|
160
|
+
pattern: /^`|`$/,
|
|
161
|
+
alias: 'code_punctuation',
|
|
162
|
+
},
|
|
163
|
+
content: /[^`]+/,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
// Bold **text** or __text__
|
|
167
|
+
bold: [
|
|
168
|
+
{
|
|
169
|
+
// **text** - no asterisks inside
|
|
170
|
+
pattern: /\*\*[^\s*][^*]*[^\s*]\*\*|\*\*[^\s*]\*\*/,
|
|
171
|
+
inside: {
|
|
172
|
+
punctuation: /^\*\*|\*\*$/,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
// __text__ - at word boundaries, no underscores inside
|
|
177
|
+
pattern: /\b__[^\s_][^_]*[^\s_]__\b|\b__[^\s_]__\b/,
|
|
178
|
+
inside: {
|
|
179
|
+
punctuation: /^__|__$/,
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
],
|
|
183
|
+
// Italic *text* or _text_
|
|
184
|
+
italic: [
|
|
185
|
+
{
|
|
186
|
+
// *text* - no asterisks inside
|
|
187
|
+
pattern: /\*[^\s*][^*]*[^\s*]\*|\*[^\s*]\*/,
|
|
188
|
+
inside: {
|
|
189
|
+
punctuation: /^\*|\*$/,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
// _text_ - at word boundaries, no underscores inside
|
|
194
|
+
pattern: /\b_[^\s_][^_]*[^\s_]_\b|\b_[^\s_]_\b/,
|
|
195
|
+
inside: {
|
|
196
|
+
punctuation: /^_|_$/,
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
// Strikethrough ~~text~~
|
|
201
|
+
strikethrough: {
|
|
202
|
+
pattern: /~~[^\s~][^~]*[^\s~]~~|~~[^\s~]~~/,
|
|
203
|
+
inside: {
|
|
204
|
+
punctuation: /^~~|~~$/,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
}, ['markdown']);
|
|
208
|
+
// Add self-reference for markdown-in-markdown (all fence types)
|
|
209
|
+
// This must be done after registration to avoid circular dependency
|
|
210
|
+
const lang_md_inside = {
|
|
211
|
+
pattern: /[\s\S]+/,
|
|
212
|
+
inside: syntax_styler.get_lang('md'),
|
|
213
|
+
};
|
|
214
|
+
for (const key of md_self_refs) {
|
|
215
|
+
// After normalization, grammar values are arrays of SyntaxGrammarToken
|
|
216
|
+
// We need to add lang_md as a normalized array
|
|
217
|
+
const patterns = grammar_md[key];
|
|
218
|
+
// Manually normalize the lang_md pattern we're adding
|
|
219
|
+
const lang_md_token = {
|
|
220
|
+
pattern: lang_md_inside.pattern,
|
|
221
|
+
lookbehind: false,
|
|
222
|
+
greedy: false,
|
|
223
|
+
alias: [],
|
|
224
|
+
inside: lang_md_inside.inside,
|
|
225
|
+
};
|
|
226
|
+
patterns[0].inside.lang_md = [lang_md_token];
|
|
227
|
+
}
|
|
228
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { SyntaxStyler, AddSyntaxGrammar } from './syntax_styler.js';
|
|
2
|
+
/**
|
|
3
|
+
* Based on Prism (https://github.com/PrismJS/prism)
|
|
4
|
+
* by Lea Verou (https://lea.verou.me/)
|
|
5
|
+
*
|
|
6
|
+
* MIT license
|
|
7
|
+
*
|
|
8
|
+
* @see LICENSE
|
|
9
|
+
*/
|
|
10
|
+
export declare const add_grammar_markup: AddSyntaxGrammar;
|
|
11
|
+
/**
|
|
12
|
+
* Adds an inlined language to markup.
|
|
13
|
+
*
|
|
14
|
+
* An example of an inlined language is CSS with `<style>` tags.
|
|
15
|
+
*
|
|
16
|
+
* @param tag_name - The name of the tag that contains the inlined language. This name will be treated as
|
|
17
|
+
* case insensitive.
|
|
18
|
+
* @param lang - The language key.
|
|
19
|
+
*/
|
|
20
|
+
export declare const grammar_markup_add_inlined: (syntax_styler: SyntaxStyler, tag_name: string, lang: string, inside_lang?: string) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Adds an pattern to style languages embedded in HTML attributes.
|
|
23
|
+
*
|
|
24
|
+
* An example of an inlined language is CSS with `style` attributes.
|
|
25
|
+
*
|
|
26
|
+
* @param attr_name - The name of the tag that contains the inlined language. This name will be treated as
|
|
27
|
+
* case insensitive.
|
|
28
|
+
* @param lang - The language key.
|
|
29
|
+
*/
|
|
30
|
+
export declare const grammar_markup_add_attribute: (syntax_styler: SyntaxStyler, attr_name: string, lang: string) => void;
|
|
31
|
+
//# sourceMappingURL=grammar_markup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_markup.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_markup.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,YAAY,EACZ,gBAAgB,EAIhB,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,EAAE,gBA6EhC,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,0BAA0B,GACtC,eAAe,YAAY,EAC3B,UAAU,MAAM,EAChB,MAAM,MAAM,EACZ,oBAAsB,KACpB,IAmCF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,4BAA4B,GACxC,eAAe,YAAY,EAC3B,WAAW,MAAM,EACjB,MAAM,MAAM,KACV,IAiEF,CAAC"}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on Prism (https://github.com/PrismJS/prism)
|
|
3
|
+
* by Lea Verou (https://lea.verou.me/)
|
|
4
|
+
*
|
|
5
|
+
* MIT license
|
|
6
|
+
*
|
|
7
|
+
* @see LICENSE
|
|
8
|
+
*/
|
|
9
|
+
export const add_grammar_markup = (syntax_styler) => {
|
|
10
|
+
const grammar_markup = {
|
|
11
|
+
comment: {
|
|
12
|
+
pattern: /<!--(?:(?!<!--)[\s\S])*?-->/,
|
|
13
|
+
greedy: true,
|
|
14
|
+
},
|
|
15
|
+
processing_instruction: {
|
|
16
|
+
pattern: /<\?[\s\S]+?\?>/,
|
|
17
|
+
greedy: true,
|
|
18
|
+
},
|
|
19
|
+
// https://www.w3.org/TR/xml/#NT-doctypedecl
|
|
20
|
+
doctype: {
|
|
21
|
+
pattern: /<!DOCTYPE[^>]*>/i,
|
|
22
|
+
greedy: true,
|
|
23
|
+
},
|
|
24
|
+
cdata: {
|
|
25
|
+
pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
|
|
26
|
+
greedy: true,
|
|
27
|
+
},
|
|
28
|
+
tag: {
|
|
29
|
+
pattern: /<\/?(?!\d)[^\s>/=$<%]+(?:\s(?:\s*[^\s>/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/,
|
|
30
|
+
greedy: true,
|
|
31
|
+
inside: {
|
|
32
|
+
tag: {
|
|
33
|
+
pattern: /^<\/?[^\s>/]+/,
|
|
34
|
+
inside: {
|
|
35
|
+
punctuation: {
|
|
36
|
+
pattern: /^<\/?/,
|
|
37
|
+
alias: 'tag_punctuation',
|
|
38
|
+
},
|
|
39
|
+
namespace: /^[^\s>/:]+:/,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
special_attr: [],
|
|
43
|
+
attr_value: {
|
|
44
|
+
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
|
|
45
|
+
inside: {
|
|
46
|
+
punctuation: [
|
|
47
|
+
{
|
|
48
|
+
pattern: /^=/,
|
|
49
|
+
alias: 'attr_equals',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
pattern: /^(\s*)["']|["']$/,
|
|
53
|
+
lookbehind: true,
|
|
54
|
+
alias: 'attr_quote',
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
entity: undefined, // see below
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
punctuation: {
|
|
61
|
+
pattern: /\/?>/,
|
|
62
|
+
alias: 'tag_punctuation',
|
|
63
|
+
},
|
|
64
|
+
attr_name: {
|
|
65
|
+
pattern: /[^\s>/]+/,
|
|
66
|
+
inside: {
|
|
67
|
+
namespace: /^[^\s>/:]+:/,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
entity: [
|
|
73
|
+
{
|
|
74
|
+
pattern: /&[\da-z]{1,8};/i,
|
|
75
|
+
alias: 'named_entity',
|
|
76
|
+
},
|
|
77
|
+
/&#x?[\da-f]{1,8};/i,
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
grammar_markup.tag.inside.attr_value.inside.entity = grammar_markup.entity;
|
|
81
|
+
syntax_styler.add_lang('markup', grammar_markup, ['html', 'mathml', 'svg']);
|
|
82
|
+
syntax_styler.add_extended_lang('markup', 'xml', {}, ['ssml', 'atom', 'rss']);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Adds an inlined language to markup.
|
|
86
|
+
*
|
|
87
|
+
* An example of an inlined language is CSS with `<style>` tags.
|
|
88
|
+
*
|
|
89
|
+
* @param tag_name - The name of the tag that contains the inlined language. This name will be treated as
|
|
90
|
+
* case insensitive.
|
|
91
|
+
* @param lang - The language key.
|
|
92
|
+
*/
|
|
93
|
+
export const grammar_markup_add_inlined = (syntax_styler, tag_name, lang, inside_lang = 'markup') => {
|
|
94
|
+
const lang_key = 'lang_' + lang;
|
|
95
|
+
syntax_styler.grammar_insert_before(inside_lang, 'cdata', {
|
|
96
|
+
[tag_name]: {
|
|
97
|
+
pattern: RegExp(/(<__[^>]*>)(?:<!\[CDATA\[(?:[^\]]|\](?!\]>))*\]\]>|(?!<!\[CDATA\[)[\s\S])*?(?=<\/__>)/.source.replace(/__/g, () => {
|
|
98
|
+
return tag_name;
|
|
99
|
+
}), 'i'),
|
|
100
|
+
lookbehind: true,
|
|
101
|
+
greedy: true,
|
|
102
|
+
inside: {
|
|
103
|
+
included_cdata: {
|
|
104
|
+
pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
|
|
105
|
+
inside: {
|
|
106
|
+
cdata: /^<!\[CDATA\[|\]\]>$/i,
|
|
107
|
+
[lang_key]: {
|
|
108
|
+
pattern: /(^<!\[CDATA\[)[\s\S]+?(?=\]\]>$)/i,
|
|
109
|
+
lookbehind: true,
|
|
110
|
+
inside: syntax_styler.get_lang(lang),
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
[lang_key]: {
|
|
115
|
+
pattern: /[\s\S]+/,
|
|
116
|
+
inside: syntax_styler.get_lang(lang),
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Adds an pattern to style languages embedded in HTML attributes.
|
|
124
|
+
*
|
|
125
|
+
* An example of an inlined language is CSS with `style` attributes.
|
|
126
|
+
*
|
|
127
|
+
* @param attr_name - The name of the tag that contains the inlined language. This name will be treated as
|
|
128
|
+
* case insensitive.
|
|
129
|
+
* @param lang - The language key.
|
|
130
|
+
*/
|
|
131
|
+
export const grammar_markup_add_attribute = (syntax_styler, attr_name, lang) => {
|
|
132
|
+
// After normalization, grammar.tag is an array of SyntaxGrammarToken
|
|
133
|
+
const markup_grammar = syntax_styler.get_lang('markup');
|
|
134
|
+
const tag_patterns = markup_grammar.tag;
|
|
135
|
+
const tag_inside = tag_patterns[0].inside;
|
|
136
|
+
tag_inside.special_attr.push({
|
|
137
|
+
pattern: RegExp(/(^|["'\s])/.source +
|
|
138
|
+
'(?:' +
|
|
139
|
+
attr_name +
|
|
140
|
+
')' +
|
|
141
|
+
/\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))/.source, 'i'),
|
|
142
|
+
lookbehind: true,
|
|
143
|
+
greedy: false,
|
|
144
|
+
alias: [],
|
|
145
|
+
inside: {
|
|
146
|
+
attr_name: [
|
|
147
|
+
{
|
|
148
|
+
pattern: /^[^\s=]+/,
|
|
149
|
+
lookbehind: false,
|
|
150
|
+
greedy: false,
|
|
151
|
+
alias: [],
|
|
152
|
+
inside: null,
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
attr_value: [
|
|
156
|
+
{
|
|
157
|
+
pattern: /=[\s\S]+/,
|
|
158
|
+
lookbehind: false,
|
|
159
|
+
greedy: false,
|
|
160
|
+
alias: [],
|
|
161
|
+
inside: {
|
|
162
|
+
value: [
|
|
163
|
+
{
|
|
164
|
+
pattern: /(^=\s*(["']|(?!["'])))\S[\s\S]*(?=\2$)/,
|
|
165
|
+
lookbehind: true,
|
|
166
|
+
greedy: false,
|
|
167
|
+
alias: [lang, 'lang_' + lang], // TODO remove this alias?
|
|
168
|
+
inside: syntax_styler.get_lang(lang),
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
punctuation: [
|
|
172
|
+
{
|
|
173
|
+
pattern: /^=/,
|
|
174
|
+
lookbehind: false,
|
|
175
|
+
greedy: false,
|
|
176
|
+
alias: ['attr_equals'],
|
|
177
|
+
inside: null,
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
pattern: /"|'/,
|
|
181
|
+
lookbehind: false,
|
|
182
|
+
greedy: false,
|
|
183
|
+
alias: ['attr_quote'],
|
|
184
|
+
inside: null,
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AddSyntaxGrammar, SyntaxStyler } from './syntax_styler.js';
|
|
2
|
+
/**
|
|
3
|
+
* Based on `prism-svelte` (https://github.com/pngwn/prism-svelte)
|
|
4
|
+
* by pngwn (https://github.com/pngwn)
|
|
5
|
+
*
|
|
6
|
+
* MIT license
|
|
7
|
+
*
|
|
8
|
+
* @see LICENSE
|
|
9
|
+
*/
|
|
10
|
+
export declare const add_grammar_svelte: AddSyntaxGrammar;
|
|
11
|
+
export declare const grammar_svelte_add_inlined: (syntax_styler: SyntaxStyler, tag_name: string, lang: string) => void;
|
|
12
|
+
//# sourceMappingURL=grammar_svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_svelte.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_svelte.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAE,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAKvE;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,EAAE,gBA+IhC,CAAC;AAEF,eAAO,MAAM,0BAA0B,GACtC,eAAe,YAAY,EAC3B,UAAU,MAAM,EAChB,MAAM,MAAM,KACV,IAEF,CAAC"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { grammar_markup_add_inlined } from './grammar_markup.js';
|
|
2
|
+
const blocks = '(if|else if|else|await|then|catch|each|html|debug|snippet)';
|
|
3
|
+
/**
|
|
4
|
+
* Based on `prism-svelte` (https://github.com/pngwn/prism-svelte)
|
|
5
|
+
* by pngwn (https://github.com/pngwn)
|
|
6
|
+
*
|
|
7
|
+
* MIT license
|
|
8
|
+
*
|
|
9
|
+
* @see LICENSE
|
|
10
|
+
*/
|
|
11
|
+
export const add_grammar_svelte = (syntax_styler) => {
|
|
12
|
+
const grammar_ts = syntax_styler.get_lang('ts');
|
|
13
|
+
// Define the at_directive pattern once for reuse (matches any @word)
|
|
14
|
+
const at_directive_pattern = {
|
|
15
|
+
pattern: /^{(@\w+)(\s+[\s\S]*)?}$/,
|
|
16
|
+
inside: {
|
|
17
|
+
lang_ts: {
|
|
18
|
+
pattern: /(@\w+\s+)([\s\S]+)/, // Fixed: removed incorrect (?=}$)
|
|
19
|
+
lookbehind: true,
|
|
20
|
+
inside: grammar_ts,
|
|
21
|
+
},
|
|
22
|
+
at_keyword: /@\w+/,
|
|
23
|
+
punctuation: /[{}]/,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
// Full expression patterns for top-level contexts
|
|
27
|
+
const svelte_expression_inside_full = {
|
|
28
|
+
// Generic @ directive - matches any @word
|
|
29
|
+
at_directive: at_directive_pattern,
|
|
30
|
+
// {#each items as item} with special syntax
|
|
31
|
+
each: {
|
|
32
|
+
pattern: /^{([#/]each)\s+([\s\S]*)}$/,
|
|
33
|
+
inside: {
|
|
34
|
+
special_keyword: /[#/]each/,
|
|
35
|
+
lang_ts: [
|
|
36
|
+
{
|
|
37
|
+
pattern: /(#each\s+)[\s\S]+(?=\s+as)/, // Expression before 'as'
|
|
38
|
+
lookbehind: true,
|
|
39
|
+
inside: grammar_ts,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
pattern: /(as\s+)[\s\S]+/, // Everything after 'as' (including key)
|
|
43
|
+
lookbehind: true,
|
|
44
|
+
inside: grammar_ts,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
keyword: /as/,
|
|
48
|
+
punctuation: /[{}]/,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
// {#block ...} and {/block}
|
|
52
|
+
block: {
|
|
53
|
+
pattern: new RegExp('^\\{([#:/@]\\s*' + blocks + ')\\s*([\\s\\S]*)\\}$'),
|
|
54
|
+
inside: {
|
|
55
|
+
special_keyword: new RegExp('[#:/@]' + blocks),
|
|
56
|
+
keyword: [/as/, /then/],
|
|
57
|
+
lang_ts: {
|
|
58
|
+
pattern: /[\s\S]+(?=}$)/,
|
|
59
|
+
inside: grammar_ts,
|
|
60
|
+
},
|
|
61
|
+
punctuation: /[{}]/,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
// Default: plain TS expression
|
|
65
|
+
punctuation: /[{}]/,
|
|
66
|
+
lang_ts: {
|
|
67
|
+
pattern: /[\s\S]+/,
|
|
68
|
+
inside: grammar_ts,
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
// Simplified patterns for tag contexts (no block directives)
|
|
72
|
+
const svelte_expression_inside_simple = {
|
|
73
|
+
// Generic @ directive
|
|
74
|
+
at_directive: at_directive_pattern,
|
|
75
|
+
// Default: plain TS expression
|
|
76
|
+
punctuation: /[{}]/,
|
|
77
|
+
lang_ts: {
|
|
78
|
+
pattern: /[\s\S]+/,
|
|
79
|
+
inside: grammar_ts,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
const grammar_svelte = syntax_styler.add_extended_lang('markup', 'svelte', {
|
|
83
|
+
svelte_expression: {
|
|
84
|
+
pattern: /\{(?:[^{}]|\{[^}]*\})*\}/,
|
|
85
|
+
greedy: true,
|
|
86
|
+
inside: svelte_expression_inside_full,
|
|
87
|
+
},
|
|
88
|
+
tag: {
|
|
89
|
+
pattern: /<\/?(?!\d)[^\s>/=$<%]+(?:\s(?:\s*(?:\{(?:[^{}]|\{[^}]*\})*\}|[^\s>/=]+)(?:\s*=\s*(?:(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?:"[^"]*"|'[^']*'|{[\s\S]+?}(?=[\s/>])))|(?=[\s/>])))+)?\s*\/?>/i,
|
|
90
|
+
greedy: true,
|
|
91
|
+
inside: {
|
|
92
|
+
tag: {
|
|
93
|
+
pattern: /^<\/?[^\s>/]+/i,
|
|
94
|
+
inside: {
|
|
95
|
+
punctuation: {
|
|
96
|
+
pattern: /^<\/?/,
|
|
97
|
+
alias: 'tag_punctuation',
|
|
98
|
+
},
|
|
99
|
+
namespace: /^[^\s>/:]+:/,
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
svelte_expression: {
|
|
103
|
+
pattern: /\{(?:[^{}]|\{[^}]*\})*\}/,
|
|
104
|
+
inside: svelte_expression_inside_simple,
|
|
105
|
+
},
|
|
106
|
+
attr_value: {
|
|
107
|
+
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/i,
|
|
108
|
+
inside: {
|
|
109
|
+
punctuation: [
|
|
110
|
+
{
|
|
111
|
+
pattern: /^=/,
|
|
112
|
+
alias: 'attr_equals',
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
pattern: /^(\s*)["']|["']$/,
|
|
116
|
+
lookbehind: true,
|
|
117
|
+
alias: 'attr_quote',
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
svelte_expression: {
|
|
121
|
+
pattern: /\{(?:[^{}]|\{[^}]*\})*\}/,
|
|
122
|
+
inside: svelte_expression_inside_simple,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
punctuation: {
|
|
127
|
+
pattern: /\/?>/,
|
|
128
|
+
alias: 'tag_punctuation',
|
|
129
|
+
},
|
|
130
|
+
attr_name: {
|
|
131
|
+
pattern: /[^\s>/]+/,
|
|
132
|
+
inside: {
|
|
133
|
+
namespace: /^[^\s>/:]+:/,
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
// oof lol
|
|
140
|
+
// After normalization, grammar.tag is an array of SyntaxGrammarToken
|
|
141
|
+
const tag_patterns = grammar_svelte.tag;
|
|
142
|
+
const tag_inside = tag_patterns[0].inside;
|
|
143
|
+
tag_inside.attr_value[0].inside.entity = grammar_svelte.entity;
|
|
144
|
+
grammar_svelte_add_inlined(syntax_styler, 'style', 'css');
|
|
145
|
+
// Assume TypeScript for all Svelte script tags (no plain JS)
|
|
146
|
+
grammar_svelte_add_inlined(syntax_styler, 'script', 'ts');
|
|
147
|
+
};
|
|
148
|
+
export const grammar_svelte_add_inlined = (syntax_styler, tag_name, lang) => {
|
|
149
|
+
grammar_markup_add_inlined(syntax_styler, tag_name, lang, 'svelte');
|
|
150
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AddSyntaxGrammar } from './syntax_styler.js';
|
|
2
|
+
/**
|
|
3
|
+
* Based on Prism (https://github.com/PrismJS/prism)
|
|
4
|
+
* by Lea Verou (https://lea.verou.me/)
|
|
5
|
+
*
|
|
6
|
+
* MIT license
|
|
7
|
+
*
|
|
8
|
+
* @see LICENSE
|
|
9
|
+
*/
|
|
10
|
+
export declare const add_grammar_ts: AddSyntaxGrammar;
|
|
11
|
+
//# sourceMappingURL=grammar_ts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_ts.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_ts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AAGzD;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,EAAE,gBAsG5B,CAAC"}
|