@fuzdev/fuz_code 0.45.1 → 0.46.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/README.md +1 -0
- package/dist/Code.svelte +2 -2
- package/dist/Code.svelte.d.ts +2 -2
- package/dist/CodeHighlight.svelte +4 -4
- package/dist/CodeHighlight.svelte.d.ts +3 -3
- package/dist/grammar_markdown.js +3 -3
- package/dist/grammar_markup.d.ts +8 -7
- package/dist/grammar_markup.d.ts.map +1 -1
- package/dist/grammar_markup.js +8 -7
- package/dist/highlight_manager.d.ts +4 -4
- package/dist/highlight_manager.js +6 -6
- package/dist/svelte_preprocess_fuz_code.d.ts +4 -4
- package/dist/svelte_preprocess_fuz_code.d.ts.map +1 -1
- package/dist/svelte_preprocess_fuz_code.js +3 -3
- package/dist/syntax_styler.d.ts +28 -32
- package/dist/syntax_styler.d.ts.map +1 -1
- package/dist/syntax_styler.js +32 -36
- package/dist/syntax_token.d.ts +4 -4
- package/dist/syntax_token.js +2 -2
- package/dist/tokenize_syntax.d.ts +2 -4
- package/dist/tokenize_syntax.d.ts.map +1 -1
- package/dist/tokenize_syntax.js +2 -4
- package/package.json +26 -29
- package/src/lib/grammar_markdown.ts +3 -3
- package/src/lib/grammar_markup.ts +8 -7
- package/src/lib/highlight_manager.ts +6 -6
- package/src/lib/svelte_preprocess_fuz_code.ts +6 -6
- package/src/lib/syntax_styler.ts +35 -39
- package/src/lib/syntax_token.ts +4 -4
- package/src/lib/tokenize_syntax.ts +2 -4
package/README.md
CHANGED
package/dist/Code.svelte
CHANGED
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
*/
|
|
56
56
|
lang?: string | null;
|
|
57
57
|
/**
|
|
58
|
-
* Optional custom
|
|
58
|
+
* Optional custom `SyntaxGrammar` object for syntax tokenization.
|
|
59
59
|
*
|
|
60
60
|
* **When to use:**
|
|
61
61
|
* - To provide a custom language definition not registered in `syntax_styler.langs`
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
*/
|
|
101
101
|
nomargin?: boolean;
|
|
102
102
|
/**
|
|
103
|
-
* Custom SyntaxStyler instance to use for highlighting.
|
|
103
|
+
* Custom `SyntaxStyler` instance to use for highlighting.
|
|
104
104
|
* Allows using a different styler with custom grammars or configuration.
|
|
105
105
|
*
|
|
106
106
|
* @default syntax_styler_global
|
package/dist/Code.svelte.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ type $$ComponentProps = SvelteHTMLElements['code'] & ({
|
|
|
35
35
|
*/
|
|
36
36
|
lang?: string | null;
|
|
37
37
|
/**
|
|
38
|
-
* Optional custom
|
|
38
|
+
* Optional custom `SyntaxGrammar` object for syntax tokenization.
|
|
39
39
|
*
|
|
40
40
|
* **When to use:**
|
|
41
41
|
* - To provide a custom language definition not registered in `syntax_styler.langs`
|
|
@@ -80,7 +80,7 @@ type $$ComponentProps = SvelteHTMLElements['code'] & ({
|
|
|
80
80
|
*/
|
|
81
81
|
nomargin?: boolean;
|
|
82
82
|
/**
|
|
83
|
-
* Custom SyntaxStyler instance to use for highlighting.
|
|
83
|
+
* Custom `SyntaxStyler` instance to use for highlighting.
|
|
84
84
|
* Allows using a different styler with custom grammars or configuration.
|
|
85
85
|
*
|
|
86
86
|
* @default syntax_styler_global
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Uses the CSS Custom Highlight API when available --
|
|
4
4
|
* https://developer.mozilla.org/en-US/docs/Web/API/CSS_Custom_Highlight_API
|
|
5
5
|
*
|
|
6
|
-
* Requires importing theme_highlight.css instead of theme.css
|
|
6
|
+
* Requires importing `theme_highlight.css` instead of `theme.css`.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import {onDestroy, type Snippet} from 'svelte';
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
*/
|
|
66
66
|
mode?: HighlightMode;
|
|
67
67
|
/**
|
|
68
|
-
* Optional custom
|
|
68
|
+
* Optional custom `SyntaxGrammar` object for syntax tokenization.
|
|
69
69
|
*
|
|
70
70
|
* **When to use:**
|
|
71
71
|
* - To provide a custom language definition not registered in `syntax_styler.langs`
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
*/
|
|
104
104
|
wrap?: boolean;
|
|
105
105
|
/**
|
|
106
|
-
* Custom SyntaxStyler instance to use for highlighting.
|
|
106
|
+
* Custom `SyntaxStyler` instance to use for highlighting.
|
|
107
107
|
* Allows using a different styler with custom grammars or configuration.
|
|
108
108
|
*
|
|
109
109
|
* @default syntax_styler_global
|
|
@@ -117,7 +117,7 @@
|
|
|
117
117
|
children?: Snippet<[markup: string]>;
|
|
118
118
|
} = $props();
|
|
119
119
|
|
|
120
|
-
let code_element: HTMLElement | undefined = $state();
|
|
120
|
+
let code_element: HTMLElement | undefined = $state.raw();
|
|
121
121
|
|
|
122
122
|
const supports_ranges = supports_css_highlight_api();
|
|
123
123
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Uses the CSS Custom Highlight API when available --
|
|
3
3
|
* https://developer.mozilla.org/en-US/docs/Web/API/CSS_Custom_Highlight_API
|
|
4
4
|
*
|
|
5
|
-
* Requires importing theme_highlight.css instead of theme.css
|
|
5
|
+
* Requires importing `theme_highlight.css` instead of `theme.css`.
|
|
6
6
|
*/
|
|
7
7
|
import { type Snippet } from 'svelte';
|
|
8
8
|
import type { SvelteHTMLElements } from 'svelte/elements';
|
|
@@ -44,7 +44,7 @@ type $$ComponentProps = SvelteHTMLElements['code'] & {
|
|
|
44
44
|
*/
|
|
45
45
|
mode?: HighlightMode;
|
|
46
46
|
/**
|
|
47
|
-
* Optional custom
|
|
47
|
+
* Optional custom `SyntaxGrammar` object for syntax tokenization.
|
|
48
48
|
*
|
|
49
49
|
* **When to use:**
|
|
50
50
|
* - To provide a custom language definition not registered in `syntax_styler.langs`
|
|
@@ -82,7 +82,7 @@ type $$ComponentProps = SvelteHTMLElements['code'] & {
|
|
|
82
82
|
*/
|
|
83
83
|
wrap?: boolean;
|
|
84
84
|
/**
|
|
85
|
-
* Custom SyntaxStyler instance to use for highlighting.
|
|
85
|
+
* Custom `SyntaxStyler` instance to use for highlighting.
|
|
86
86
|
* Allows using a different styler with custom grammars or configuration.
|
|
87
87
|
*
|
|
88
88
|
* @default syntax_styler_global
|
package/dist/grammar_markdown.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Helper to create fenced code block pattern for a language
|
|
2
|
+
* Helper to create fenced code block pattern for a language.
|
|
3
3
|
*/
|
|
4
4
|
const create_fence_pattern = (backticks, aliases, lang_id, syntax_styler) => {
|
|
5
5
|
const aliases_pattern = aliases.join('|');
|
|
@@ -21,7 +21,7 @@ const create_fence_pattern = (backticks, aliases, lang_id, syntax_styler) => {
|
|
|
21
21
|
};
|
|
22
22
|
};
|
|
23
23
|
/**
|
|
24
|
-
* Helper to create catch-all fence pattern (unknown languages)
|
|
24
|
+
* Helper to create catch-all fence pattern (unknown languages).
|
|
25
25
|
*/
|
|
26
26
|
const create_catchall_fence = (backticks) => {
|
|
27
27
|
const pattern = new RegExp(`^${backticks}[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`, 'm');
|
|
@@ -38,7 +38,7 @@ const create_catchall_fence = (backticks) => {
|
|
|
38
38
|
};
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
|
-
* Helper to create md self-reference placeholder pattern
|
|
41
|
+
* Helper to create md self-reference placeholder pattern.
|
|
42
42
|
*/
|
|
43
43
|
const create_md_placeholder = (backticks) => {
|
|
44
44
|
const pattern = new RegExp(`^${backticks}(?:md|markdown)[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`, 'm');
|
package/dist/grammar_markup.d.ts
CHANGED
|
@@ -13,19 +13,20 @@ export declare const add_grammar_markup: AddSyntaxGrammar;
|
|
|
13
13
|
*
|
|
14
14
|
* An example of an inlined language is CSS with `<style>` tags.
|
|
15
15
|
*
|
|
16
|
-
* @param
|
|
17
|
-
* case insensitive
|
|
18
|
-
* @param lang -
|
|
16
|
+
* @param syntax_styler - the `SyntaxStyler` instance to modify
|
|
17
|
+
* @param tag_name - the name of the tag that contains the inlined language, treated as case insensitive
|
|
18
|
+
* @param lang - the language key
|
|
19
|
+
* @param inside_lang - the language to insert into, defaults to `'markup'`
|
|
19
20
|
*/
|
|
20
21
|
export declare const grammar_markup_add_inlined: (syntax_styler: SyntaxStyler, tag_name: string, lang: string, inside_lang?: string) => void;
|
|
21
22
|
/**
|
|
22
|
-
* Adds
|
|
23
|
+
* Adds a pattern to style languages embedded in HTML attributes.
|
|
23
24
|
*
|
|
24
25
|
* An example of an inlined language is CSS with `style` attributes.
|
|
25
26
|
*
|
|
26
|
-
* @param
|
|
27
|
-
* case insensitive
|
|
28
|
-
* @param lang -
|
|
27
|
+
* @param syntax_styler - the `SyntaxStyler` instance to modify
|
|
28
|
+
* @param attr_name - the name of the attribute that contains the inlined language, treated as case insensitive
|
|
29
|
+
* @param lang - the language key
|
|
29
30
|
*/
|
|
30
31
|
export declare const grammar_markup_add_attribute: (syntax_styler: SyntaxStyler, attr_name: string, lang: string) => void;
|
|
31
32
|
//# sourceMappingURL=grammar_markup.d.ts.map
|
|
@@ -1 +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
|
|
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;;;;;;;;;GASG;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"}
|
package/dist/grammar_markup.js
CHANGED
|
@@ -86,9 +86,10 @@ export const add_grammar_markup = (syntax_styler) => {
|
|
|
86
86
|
*
|
|
87
87
|
* An example of an inlined language is CSS with `<style>` tags.
|
|
88
88
|
*
|
|
89
|
-
* @param
|
|
90
|
-
* case insensitive
|
|
91
|
-
* @param lang -
|
|
89
|
+
* @param syntax_styler - the `SyntaxStyler` instance to modify
|
|
90
|
+
* @param tag_name - the name of the tag that contains the inlined language, treated as case insensitive
|
|
91
|
+
* @param lang - the language key
|
|
92
|
+
* @param inside_lang - the language to insert into, defaults to `'markup'`
|
|
92
93
|
*/
|
|
93
94
|
export const grammar_markup_add_inlined = (syntax_styler, tag_name, lang, inside_lang = 'markup') => {
|
|
94
95
|
const lang_key = 'lang_' + lang;
|
|
@@ -120,13 +121,13 @@ export const grammar_markup_add_inlined = (syntax_styler, tag_name, lang, inside
|
|
|
120
121
|
});
|
|
121
122
|
};
|
|
122
123
|
/**
|
|
123
|
-
* Adds
|
|
124
|
+
* Adds a pattern to style languages embedded in HTML attributes.
|
|
124
125
|
*
|
|
125
126
|
* An example of an inlined language is CSS with `style` attributes.
|
|
126
127
|
*
|
|
127
|
-
* @param
|
|
128
|
-
* case insensitive
|
|
129
|
-
* @param lang -
|
|
128
|
+
* @param syntax_styler - the `SyntaxStyler` instance to modify
|
|
129
|
+
* @param attr_name - the name of the attribute that contains the inlined language, treated as case insensitive
|
|
130
|
+
* @param lang - the language key
|
|
130
131
|
*/
|
|
131
132
|
export const grammar_markup_add_attribute = (syntax_styler, attr_name, lang) => {
|
|
132
133
|
// After normalization, grammar.tag is an array of SyntaxGrammarToken
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { SyntaxTokenStream } from './syntax_token.js';
|
|
2
2
|
export type HighlightMode = 'auto' | 'ranges' | 'html';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Checks for CSS Highlights API support.
|
|
5
5
|
*/
|
|
6
6
|
export declare const supports_css_highlight_api: () => boolean;
|
|
7
7
|
/**
|
|
8
8
|
* Manages CSS Custom Highlight API ranges for a single element.
|
|
9
9
|
* Tracks ranges per element and only removes its own ranges when clearing.
|
|
10
10
|
*
|
|
11
|
-
* **Experimental** — limited browser support. Use `Code` for production.
|
|
11
|
+
* **Experimental** — limited browser support. Use `Code.svelte` for production.
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```ts
|
|
@@ -21,11 +21,11 @@ export declare class HighlightManager {
|
|
|
21
21
|
element_ranges: Map<string, Array<Range>>;
|
|
22
22
|
constructor();
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
24
|
+
* Highlights from a `SyntaxTokenStream` produced by `tokenize_syntax`.
|
|
25
25
|
*/
|
|
26
26
|
highlight_from_syntax_tokens(element: Element, tokens: SyntaxTokenStream): void;
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Clears only this element's ranges from highlights.
|
|
29
29
|
*/
|
|
30
30
|
clear_element_ranges(): void;
|
|
31
31
|
destroy(): void;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { highlight_priorities } from './highlight_priorities.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Checks for CSS Highlights API support.
|
|
4
4
|
*/
|
|
5
|
-
export const supports_css_highlight_api = () => !!(globalThis.CSS?.highlights && globalThis.Highlight);
|
|
5
|
+
export const supports_css_highlight_api = () => !!(globalThis.CSS?.highlights && globalThis.Highlight);
|
|
6
6
|
/**
|
|
7
7
|
* Manages CSS Custom Highlight API ranges for a single element.
|
|
8
8
|
* Tracks ranges per element and only removes its own ranges when clearing.
|
|
9
9
|
*
|
|
10
|
-
* **Experimental** — limited browser support. Use `Code` for production.
|
|
10
|
+
* **Experimental** — limited browser support. Use `Code.svelte` for production.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* ```ts
|
|
@@ -24,7 +24,7 @@ export class HighlightManager {
|
|
|
24
24
|
this.element_ranges = new Map();
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* Highlights from a `SyntaxTokenStream` produced by `tokenize_syntax`.
|
|
28
28
|
*/
|
|
29
29
|
highlight_from_syntax_tokens(element, tokens) {
|
|
30
30
|
// Find the text node (it might not be firstChild due to Svelte comment nodes)
|
|
@@ -67,7 +67,7 @@ export class HighlightManager {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
|
-
*
|
|
70
|
+
* Clears only this element's ranges from highlights.
|
|
71
71
|
*/
|
|
72
72
|
clear_element_ranges() {
|
|
73
73
|
for (const [name, ranges] of this.element_ranges) {
|
|
@@ -88,7 +88,7 @@ export class HighlightManager {
|
|
|
88
88
|
this.clear_element_ranges();
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
|
-
*
|
|
91
|
+
* Creates ranges for all tokens in the tree.
|
|
92
92
|
*/
|
|
93
93
|
#create_all_ranges(tokens, text_node, ranges_by_type, offset) {
|
|
94
94
|
const text_length = text_node.textContent?.length ?? 0;
|
|
@@ -6,13 +6,13 @@ import type { SyntaxStyler } from './syntax_styler.js';
|
|
|
6
6
|
export interface PreprocessFuzCodeOptions {
|
|
7
7
|
/** File patterns to exclude. */
|
|
8
8
|
exclude?: Array<string | RegExp>;
|
|
9
|
-
/** Custom
|
|
9
|
+
/** Custom `SyntaxStyler` instance. @default syntax_styler_global */
|
|
10
10
|
syntax_styler?: SyntaxStyler;
|
|
11
11
|
/** Enable in-memory caching. @default true */
|
|
12
12
|
cache?: boolean;
|
|
13
13
|
/**
|
|
14
|
-
* Import sources that resolve to the Code component.
|
|
15
|
-
* Used to verify that `<Code>` in templates actually refers to
|
|
14
|
+
* Import sources that resolve to the `Code` component.
|
|
15
|
+
* Used to verify that `<Code>` in templates actually refers to `Code.svelte`.
|
|
16
16
|
*
|
|
17
17
|
* @default ['@fuzdev/fuz_code/Code.svelte']
|
|
18
18
|
*/
|
|
@@ -27,7 +27,7 @@ export interface PreprocessFuzCodeOptions {
|
|
|
27
27
|
* Svelte preprocessor that compiles static `Code` component content at build time,
|
|
28
28
|
* replacing runtime syntax highlighting with pre-rendered HTML.
|
|
29
29
|
*
|
|
30
|
-
* @param options
|
|
30
|
+
* @param options - `PreprocessFuzCodeOptions` configuration
|
|
31
31
|
* @returns a Svelte preprocessor group
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"svelte_preprocess_fuz_code.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/svelte_preprocess_fuz_code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,iBAAiB,EAAW,MAAM,iBAAiB,CAAC;AAgBxE,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACxC,gCAAgC;IAChC,OAAO,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;IAEjC,
|
|
1
|
+
{"version":3,"file":"svelte_preprocess_fuz_code.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/svelte_preprocess_fuz_code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,iBAAiB,EAAW,MAAM,iBAAiB,CAAC;AAgBxE,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACxC,gCAAgC;IAChC,OAAO,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;IAEjC,oEAAoE;IACpE,aAAa,CAAC,EAAE,YAAY,CAAC;IAE7B,8CAA8C;IAC9C,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAElC;;;OAGG;IACH,QAAQ,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC;CAC3B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,0BAA0B,GACtC,UAAS,wBAA6B,KACpC,iBA6DF,CAAC"}
|
|
@@ -9,7 +9,7 @@ import { syntax_styler_global } from './syntax_styler_global.js';
|
|
|
9
9
|
* Svelte preprocessor that compiles static `Code` component content at build time,
|
|
10
10
|
* replacing runtime syntax highlighting with pre-rendered HTML.
|
|
11
11
|
*
|
|
12
|
-
* @param options
|
|
12
|
+
* @param options - `PreprocessFuzCodeOptions` configuration
|
|
13
13
|
* @returns a Svelte preprocessor group
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -68,7 +68,7 @@ export const svelte_preprocess_fuz_code = (options = {}) => {
|
|
|
68
68
|
};
|
|
69
69
|
};
|
|
70
70
|
/**
|
|
71
|
-
*
|
|
71
|
+
* Attempts to highlight content, using cache if available.
|
|
72
72
|
* Returns the highlighted HTML, or `null` on error.
|
|
73
73
|
*/
|
|
74
74
|
const try_highlight = (text, lang, syntax_styler, options) => {
|
|
@@ -87,7 +87,7 @@ const try_highlight = (text, lang, syntax_styler, options) => {
|
|
|
87
87
|
return html;
|
|
88
88
|
};
|
|
89
89
|
/**
|
|
90
|
-
* Walks the AST to find Code component usages with static `content` props
|
|
90
|
+
* Walks the AST to find `Code` component usages with static `content` props
|
|
91
91
|
* and generates transformations to replace them with `dangerous_raw_html`.
|
|
92
92
|
*/
|
|
93
93
|
const find_code_usages = (ast, syntax_styler, code_names, options) => {
|
package/dist/syntax_styler.d.ts
CHANGED
|
@@ -34,15 +34,14 @@ export declare class SyntaxStyler {
|
|
|
34
34
|
* - Custom grammar: `stylize(code, 'ts', customGrammar)` - uses custom grammar but keeps 'ts' label
|
|
35
35
|
* - Extended grammar: `stylize(code, 'custom', this.extend_grammar('ts', extension))` - new language variant
|
|
36
36
|
*
|
|
37
|
-
* @param text -
|
|
38
|
-
* @param lang -
|
|
39
|
-
* -
|
|
40
|
-
* -
|
|
41
|
-
* -
|
|
42
|
-
* @param grammar -
|
|
43
|
-
* looks up the grammar via `this.get_lang(lang)
|
|
44
|
-
* or modified grammar instead of the registered one
|
|
45
|
-
*
|
|
37
|
+
* @param text - the source code to syntax highlight
|
|
38
|
+
* @param lang - language identifier (e.g., 'ts', 'css', 'html'), used for:
|
|
39
|
+
* - grammar lookup when `grammar` is undefined
|
|
40
|
+
* - hook context (`lang` field passed to hooks)
|
|
41
|
+
* - language identification in output
|
|
42
|
+
* @param grammar - optional custom `SyntaxGrammar` object; when undefined, automatically
|
|
43
|
+
* looks up the grammar via `this.get_lang(lang)`; provide this to use a custom
|
|
44
|
+
* or modified grammar instead of the registered one
|
|
46
45
|
* @returns HTML string with syntax highlighting using CSS classes (`.token_*`)
|
|
47
46
|
*
|
|
48
47
|
* @example
|
|
@@ -83,7 +82,7 @@ export declare class SyntaxStyler {
|
|
|
83
82
|
* };
|
|
84
83
|
* ```
|
|
85
84
|
*
|
|
86
|
-
* then the `style` token will be added (and processed) at the end. `
|
|
85
|
+
* then the `style` token will be added (and processed) at the end. `grammar_insert_before` allows you to insert tokens
|
|
87
86
|
* before existing tokens. For the CSS example above, you would use it like this:
|
|
88
87
|
*
|
|
89
88
|
* ```js
|
|
@@ -110,12 +109,12 @@ export declare class SyntaxStyler {
|
|
|
110
109
|
*
|
|
111
110
|
* ## Limitations
|
|
112
111
|
*
|
|
113
|
-
* The main problem `
|
|
112
|
+
* The main problem `grammar_insert_before` has to solve is iteration order. Since ES2015, the iteration order for object
|
|
114
113
|
* properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
|
|
115
|
-
* differently when keys are deleted and re-inserted. So `
|
|
114
|
+
* differently when keys are deleted and re-inserted. So `grammar_insert_before` can't be implemented by temporarily
|
|
116
115
|
* deleting properties which is necessary to insert at arbitrary positions.
|
|
117
116
|
*
|
|
118
|
-
* To solve this problem, `
|
|
117
|
+
* To solve this problem, `grammar_insert_before` doesn't actually insert the given tokens into the target object.
|
|
119
118
|
* Instead, it will create a new object and replace all references to the target object with the new one. This
|
|
120
119
|
* can be done without temporarily deleting properties, so the iteration order is well-defined.
|
|
121
120
|
*
|
|
@@ -130,16 +129,13 @@ export declare class SyntaxStyler {
|
|
|
130
129
|
* assert(newMarkup === syntax_styler.get_lang('markup'));
|
|
131
130
|
* ```
|
|
132
131
|
*
|
|
133
|
-
* @param inside -
|
|
134
|
-
* object to be modified
|
|
135
|
-
* @param before -
|
|
136
|
-
* @param insert -
|
|
137
|
-
* @param root -
|
|
138
|
-
* object to be modified.
|
|
139
|
-
*
|
|
140
|
-
* Defaults to `syntax_styler.langs`.
|
|
141
|
-
*
|
|
142
|
-
* @returns the new grammar object
|
|
132
|
+
* @param inside - the property of `root` (e.g. a language id in `syntax_styler.langs`) that contains the
|
|
133
|
+
* object to be modified
|
|
134
|
+
* @param before - the key to insert before
|
|
135
|
+
* @param insert - an object containing the key-value pairs to be inserted
|
|
136
|
+
* @param root - the object containing `inside`, i.e. the object that contains the
|
|
137
|
+
* object to be modified; defaults to `syntax_styler.langs`
|
|
138
|
+
* @returns the new `SyntaxGrammar` object
|
|
143
139
|
*/
|
|
144
140
|
grammar_insert_before(inside: string, before: string, insert: SyntaxGrammarRaw, root?: Record<string, any>): SyntaxGrammar;
|
|
145
141
|
/**
|
|
@@ -147,9 +143,9 @@ export declare class SyntaxStyler {
|
|
|
147
143
|
*
|
|
148
144
|
* Runs the `wrap` hook on each `SyntaxToken`.
|
|
149
145
|
*
|
|
150
|
-
* @param o -
|
|
151
|
-
* @param lang -
|
|
152
|
-
* @returns
|
|
146
|
+
* @param o - the token or `SyntaxTokenStream` to be converted
|
|
147
|
+
* @param lang - the name of current language
|
|
148
|
+
* @returns HTML representation of the token or token stream
|
|
153
149
|
*/
|
|
154
150
|
stringify_token(o: string | SyntaxToken | SyntaxTokenStream, lang: string): string;
|
|
155
151
|
/**
|
|
@@ -167,9 +163,9 @@ export declare class SyntaxStyler {
|
|
|
167
163
|
* Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
|
|
168
164
|
* Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
|
|
169
165
|
*
|
|
170
|
-
* @param base_id -
|
|
171
|
-
* @param extension -
|
|
172
|
-
* @returns the new
|
|
166
|
+
* @param base_id - the id of the language to extend, must be a key in `syntax_styler.langs`
|
|
167
|
+
* @param extension - the new tokens to append
|
|
168
|
+
* @returns the new `SyntaxGrammar`
|
|
173
169
|
*/
|
|
174
170
|
extend_grammar(base_id: string, extension: SyntaxGrammarRaw): SyntaxGrammar;
|
|
175
171
|
plugins: Record<string, any>;
|
|
@@ -221,13 +217,13 @@ export interface SyntaxGrammarTokenRaw {
|
|
|
221
217
|
*/
|
|
222
218
|
alias?: string | Array<string>;
|
|
223
219
|
/**
|
|
224
|
-
* The nested
|
|
220
|
+
* The nested `SyntaxGrammarRaw` of this token.
|
|
225
221
|
*/
|
|
226
222
|
inside?: SyntaxGrammarRaw | null;
|
|
227
223
|
}
|
|
228
224
|
/**
|
|
229
225
|
* Grammar token with all properties required.
|
|
230
|
-
* This is the normalized representation used at runtime.
|
|
226
|
+
* This is the normalized representation of `SyntaxGrammarTokenRaw` used at runtime.
|
|
231
227
|
*/
|
|
232
228
|
export interface SyntaxGrammarToken {
|
|
233
229
|
pattern: RegExp;
|
|
@@ -238,7 +234,7 @@ export interface SyntaxGrammarToken {
|
|
|
238
234
|
}
|
|
239
235
|
/**
|
|
240
236
|
* A grammar after normalization.
|
|
241
|
-
* All values are arrays of normalized
|
|
237
|
+
* All values are arrays of normalized `SyntaxGrammarToken` with consistent shapes.
|
|
242
238
|
*/
|
|
243
239
|
export type SyntaxGrammar = Record<string, Array<SyntaxGrammarToken>>;
|
|
244
240
|
export type HookBeforeTokenizeCallback = (ctx: HookBeforeTokenizeCallbackContext) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syntax_styler.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/syntax_styler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,KAAK,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAGtE,MAAM,MAAM,gBAAgB,GAAG,CAAC,aAAa,EAAE,YAAY,KAAK,IAAI,CAAC;AAErE;;;;;;;GAOG;AACH,qBAAa,YAAY;;IACxB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,SAAS,CAAC,CAE9C;IAkBF,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI;IAc9E,iBAAiB,CAChB,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,gBAAgB,EAC3B,OAAO,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,GACrB,aAAa;IAahB,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,aAAa;IAQnC
|
|
1
|
+
{"version":3,"file":"syntax_styler.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/syntax_styler.ts"],"names":[],"mappings":"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"}
|
package/dist/syntax_styler.js
CHANGED
|
@@ -79,15 +79,14 @@ export class SyntaxStyler {
|
|
|
79
79
|
* - Custom grammar: `stylize(code, 'ts', customGrammar)` - uses custom grammar but keeps 'ts' label
|
|
80
80
|
* - Extended grammar: `stylize(code, 'custom', this.extend_grammar('ts', extension))` - new language variant
|
|
81
81
|
*
|
|
82
|
-
* @param text -
|
|
83
|
-
* @param lang -
|
|
84
|
-
* -
|
|
85
|
-
* -
|
|
86
|
-
* -
|
|
87
|
-
* @param grammar -
|
|
88
|
-
* looks up the grammar via `this.get_lang(lang)
|
|
89
|
-
* or modified grammar instead of the registered one
|
|
90
|
-
*
|
|
82
|
+
* @param text - the source code to syntax highlight
|
|
83
|
+
* @param lang - language identifier (e.g., 'ts', 'css', 'html'), used for:
|
|
84
|
+
* - grammar lookup when `grammar` is undefined
|
|
85
|
+
* - hook context (`lang` field passed to hooks)
|
|
86
|
+
* - language identification in output
|
|
87
|
+
* @param grammar - optional custom `SyntaxGrammar` object; when undefined, automatically
|
|
88
|
+
* looks up the grammar via `this.get_lang(lang)`; provide this to use a custom
|
|
89
|
+
* or modified grammar instead of the registered one
|
|
91
90
|
* @returns HTML string with syntax highlighting using CSS classes (`.token_*`)
|
|
92
91
|
*
|
|
93
92
|
* @example
|
|
@@ -140,7 +139,7 @@ export class SyntaxStyler {
|
|
|
140
139
|
* };
|
|
141
140
|
* ```
|
|
142
141
|
*
|
|
143
|
-
* then the `style` token will be added (and processed) at the end. `
|
|
142
|
+
* then the `style` token will be added (and processed) at the end. `grammar_insert_before` allows you to insert tokens
|
|
144
143
|
* before existing tokens. For the CSS example above, you would use it like this:
|
|
145
144
|
*
|
|
146
145
|
* ```js
|
|
@@ -167,12 +166,12 @@ export class SyntaxStyler {
|
|
|
167
166
|
*
|
|
168
167
|
* ## Limitations
|
|
169
168
|
*
|
|
170
|
-
* The main problem `
|
|
169
|
+
* The main problem `grammar_insert_before` has to solve is iteration order. Since ES2015, the iteration order for object
|
|
171
170
|
* properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
|
|
172
|
-
* differently when keys are deleted and re-inserted. So `
|
|
171
|
+
* differently when keys are deleted and re-inserted. So `grammar_insert_before` can't be implemented by temporarily
|
|
173
172
|
* deleting properties which is necessary to insert at arbitrary positions.
|
|
174
173
|
*
|
|
175
|
-
* To solve this problem, `
|
|
174
|
+
* To solve this problem, `grammar_insert_before` doesn't actually insert the given tokens into the target object.
|
|
176
175
|
* Instead, it will create a new object and replace all references to the target object with the new one. This
|
|
177
176
|
* can be done without temporarily deleting properties, so the iteration order is well-defined.
|
|
178
177
|
*
|
|
@@ -187,16 +186,13 @@ export class SyntaxStyler {
|
|
|
187
186
|
* assert(newMarkup === syntax_styler.get_lang('markup'));
|
|
188
187
|
* ```
|
|
189
188
|
*
|
|
190
|
-
* @param inside -
|
|
191
|
-
* object to be modified
|
|
192
|
-
* @param before -
|
|
193
|
-
* @param insert -
|
|
194
|
-
* @param root -
|
|
195
|
-
* object to be modified.
|
|
196
|
-
*
|
|
197
|
-
* Defaults to `syntax_styler.langs`.
|
|
198
|
-
*
|
|
199
|
-
* @returns the new grammar object
|
|
189
|
+
* @param inside - the property of `root` (e.g. a language id in `syntax_styler.langs`) that contains the
|
|
190
|
+
* object to be modified
|
|
191
|
+
* @param before - the key to insert before
|
|
192
|
+
* @param insert - an object containing the key-value pairs to be inserted
|
|
193
|
+
* @param root - the object containing `inside`, i.e. the object that contains the
|
|
194
|
+
* object to be modified; defaults to `syntax_styler.langs`
|
|
195
|
+
* @returns the new `SyntaxGrammar` object
|
|
200
196
|
*/
|
|
201
197
|
grammar_insert_before(inside, before, insert, root = this.langs) {
|
|
202
198
|
var grammar = root[inside];
|
|
@@ -231,9 +227,9 @@ export class SyntaxStyler {
|
|
|
231
227
|
*
|
|
232
228
|
* Runs the `wrap` hook on each `SyntaxToken`.
|
|
233
229
|
*
|
|
234
|
-
* @param o -
|
|
235
|
-
* @param lang -
|
|
236
|
-
* @returns
|
|
230
|
+
* @param o - the token or `SyntaxTokenStream` to be converted
|
|
231
|
+
* @param lang - the name of current language
|
|
232
|
+
* @returns HTML representation of the token or token stream
|
|
237
233
|
*/
|
|
238
234
|
stringify_token(o, lang) {
|
|
239
235
|
if (typeof o === 'string') {
|
|
@@ -294,9 +290,9 @@ export class SyntaxStyler {
|
|
|
294
290
|
* Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
|
|
295
291
|
* Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
|
|
296
292
|
*
|
|
297
|
-
* @param base_id -
|
|
298
|
-
* @param extension -
|
|
299
|
-
* @returns the new
|
|
293
|
+
* @param base_id - the id of the language to extend, must be a key in `syntax_styler.langs`
|
|
294
|
+
* @param extension - the new tokens to append
|
|
295
|
+
* @returns the new `SyntaxGrammar`
|
|
300
296
|
*/
|
|
301
297
|
extend_grammar(base_id, extension) {
|
|
302
298
|
// Merge normalized base with un-normalized extension
|
|
@@ -307,7 +303,7 @@ export class SyntaxStyler {
|
|
|
307
303
|
return extended;
|
|
308
304
|
}
|
|
309
305
|
/**
|
|
310
|
-
*
|
|
306
|
+
* Normalizes a single pattern to have consistent shape.
|
|
311
307
|
* This ensures all patterns have the same object shape for V8 optimization.
|
|
312
308
|
*/
|
|
313
309
|
#normalize_pattern(pattern, visited) {
|
|
@@ -339,15 +335,15 @@ export class SyntaxStyler {
|
|
|
339
335
|
};
|
|
340
336
|
}
|
|
341
337
|
/**
|
|
342
|
-
*
|
|
338
|
+
* Normalizes a grammar to have consistent object shapes.
|
|
343
339
|
* This performs several optimizations:
|
|
344
|
-
* 1. Merges `rest` property into main grammar
|
|
345
|
-
* 2. Ensures all pattern values are arrays
|
|
346
|
-
* 3. Normalizes all pattern objects to have consistent shapes
|
|
347
|
-
* 4. Adds global flag to greedy patterns
|
|
340
|
+
* 1. Merges `rest` property into main grammar.
|
|
341
|
+
* 2. Ensures all pattern values are arrays.
|
|
342
|
+
* 3. Normalizes all pattern objects to have consistent shapes.
|
|
343
|
+
* 4. Adds global flag to greedy patterns.
|
|
348
344
|
*
|
|
349
345
|
* This is called once at registration time to avoid runtime overhead.
|
|
350
|
-
* @param visited -
|
|
346
|
+
* @param visited - set of grammar object IDs already normalized (for circular references)
|
|
351
347
|
*/
|
|
352
348
|
#normalize_grammar(grammar, visited) {
|
|
353
349
|
// Check if we've already normalized this grammar (circular reference)
|
package/dist/syntax_token.d.ts
CHANGED
|
@@ -2,13 +2,13 @@ export declare class SyntaxToken {
|
|
|
2
2
|
/**
|
|
3
3
|
* The type of the token.
|
|
4
4
|
*
|
|
5
|
-
* This is usually the key of a pattern in a `
|
|
5
|
+
* This is usually the key of a pattern in a `SyntaxGrammar`.
|
|
6
6
|
*/
|
|
7
7
|
type: string;
|
|
8
8
|
/**
|
|
9
9
|
* The strings or tokens contained by this token.
|
|
10
10
|
*
|
|
11
|
-
* This will be a
|
|
11
|
+
* This will be a `SyntaxTokenStream` if the pattern matched also defined an `inside` grammar.
|
|
12
12
|
*/
|
|
13
13
|
content: string | SyntaxTokenStream;
|
|
14
14
|
/**
|
|
@@ -22,8 +22,8 @@ export declare class SyntaxToken {
|
|
|
22
22
|
/**
|
|
23
23
|
* A token stream is an array of strings and `SyntaxToken` objects.
|
|
24
24
|
*
|
|
25
|
-
*
|
|
26
|
-
* them.
|
|
25
|
+
* `SyntaxTokenStream` values have to fulfill a few properties that are assumed by most functions
|
|
26
|
+
* (mostly internal ones) that process them.
|
|
27
27
|
*
|
|
28
28
|
* 1. No adjacent strings.
|
|
29
29
|
* 2. No empty strings.
|
package/dist/syntax_token.js
CHANGED
|
@@ -2,13 +2,13 @@ export class SyntaxToken {
|
|
|
2
2
|
/**
|
|
3
3
|
* The type of the token.
|
|
4
4
|
*
|
|
5
|
-
* This is usually the key of a pattern in a `
|
|
5
|
+
* This is usually the key of a pattern in a `SyntaxGrammar`.
|
|
6
6
|
*/
|
|
7
7
|
type;
|
|
8
8
|
/**
|
|
9
9
|
* The strings or tokens contained by this token.
|
|
10
10
|
*
|
|
11
|
-
* This will be a
|
|
11
|
+
* This will be a `SyntaxTokenStream` if the pattern matched also defined an `inside` grammar.
|
|
12
12
|
*/
|
|
13
13
|
content;
|
|
14
14
|
/**
|
|
@@ -9,11 +9,9 @@ import { type SyntaxTokenStream } from './syntax_token.js';
|
|
|
9
9
|
* This method could be useful in other contexts as well, as a very crude parser.
|
|
10
10
|
*
|
|
11
11
|
* @param text - a string with the code to be styled
|
|
12
|
-
* @param grammar -
|
|
13
|
-
*
|
|
12
|
+
* @param grammar - a `SyntaxGrammar` object containing the tokens to use.
|
|
14
13
|
* Usually a language definition like `syntax_styler.get_lang('markup')`.
|
|
15
|
-
*
|
|
16
|
-
* @returns an array of strings and tokens, a token stream
|
|
14
|
+
* @returns a `SyntaxTokenStream` array of strings and tokens
|
|
17
15
|
*
|
|
18
16
|
* @example
|
|
19
17
|
* ```ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenize_syntax.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/tokenize_syntax.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAc,KAAK,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAEtE
|
|
1
|
+
{"version":3,"file":"tokenize_syntax.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/tokenize_syntax.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAc,KAAK,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,SAAS,aAAa,KAAG,iBAQtE,CAAC"}
|
package/dist/tokenize_syntax.js
CHANGED
|
@@ -8,11 +8,9 @@ import { SyntaxToken } from './syntax_token.js';
|
|
|
8
8
|
* This method could be useful in other contexts as well, as a very crude parser.
|
|
9
9
|
*
|
|
10
10
|
* @param text - a string with the code to be styled
|
|
11
|
-
* @param grammar -
|
|
12
|
-
*
|
|
11
|
+
* @param grammar - a `SyntaxGrammar` object containing the tokens to use.
|
|
13
12
|
* Usually a language definition like `syntax_styler.get_lang('markup')`.
|
|
14
|
-
*
|
|
15
|
-
* @returns an array of strings and tokens, a token stream
|
|
13
|
+
* @returns a `SyntaxTokenStream` array of strings and tokens
|
|
16
14
|
*
|
|
17
15
|
* @example
|
|
18
16
|
* ```ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fuzdev/fuz_code",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0",
|
|
4
4
|
"description": "syntax styling utilities and components for TypeScript, Svelte, Markdown, and more",
|
|
5
5
|
"glyph": "🎨",
|
|
6
6
|
"logo": "logo.svg",
|
|
@@ -24,48 +24,45 @@
|
|
|
24
24
|
"preview": "vite preview",
|
|
25
25
|
"deploy": "gro deploy",
|
|
26
26
|
"benchmark": "gro run benchmark/run_benchmarks.ts",
|
|
27
|
-
"benchmark:
|
|
27
|
+
"benchmark:save": "gro run benchmark/run_benchmarks.ts --save",
|
|
28
|
+
"benchmark:clean": "rm -f benchmark/baseline.json",
|
|
29
|
+
"benchmark:vs": "gro run benchmark/compare/run_compare.ts",
|
|
30
|
+
"benchmark:vs:write": "gro run benchmark/compare/run_compare.ts --write",
|
|
28
31
|
"fixtures:update": "gro src/test/fixtures/update"
|
|
29
32
|
},
|
|
30
33
|
"type": "module",
|
|
31
34
|
"engines": {
|
|
32
|
-
"node": ">=
|
|
35
|
+
"node": ">=24.14"
|
|
33
36
|
},
|
|
34
37
|
"peerDependencies": {
|
|
35
|
-
"@fuzdev/fuz_css": ">=0.
|
|
36
|
-
"@fuzdev/fuz_util": ">=0.
|
|
38
|
+
"@fuzdev/fuz_css": ">=0.62.0",
|
|
39
|
+
"@fuzdev/fuz_util": ">=0.65.1",
|
|
37
40
|
"esm-env": "^1",
|
|
38
|
-
"
|
|
39
|
-
"svelte": "^5",
|
|
40
|
-
"zimmerframe": "^1"
|
|
41
|
+
"svelte": "^5"
|
|
41
42
|
},
|
|
42
43
|
"peerDependenciesMeta": {
|
|
43
44
|
"@fuzdev/fuz_css": {
|
|
44
45
|
"optional": true
|
|
45
46
|
},
|
|
46
|
-
"@fuzdev/fuz_util": {
|
|
47
|
-
"optional": true
|
|
48
|
-
},
|
|
49
|
-
"magic-string": {
|
|
50
|
-
"optional": true
|
|
51
|
-
},
|
|
52
47
|
"svelte": {
|
|
53
48
|
"optional": true
|
|
54
|
-
},
|
|
55
|
-
"zimmerframe": {
|
|
56
|
-
"optional": true
|
|
57
49
|
}
|
|
58
50
|
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"magic-string": "^0.30.21",
|
|
53
|
+
"zimmerframe": "^1.1.4"
|
|
54
|
+
},
|
|
59
55
|
"devDependencies": {
|
|
60
56
|
"@changesets/changelog-git": "^0.2.1",
|
|
61
|
-
"@fuzdev/
|
|
62
|
-
"@fuzdev/
|
|
63
|
-
"@fuzdev/
|
|
64
|
-
"@fuzdev/
|
|
65
|
-
"@
|
|
66
|
-
"@ryanatkn/eslint-config": "^0.
|
|
57
|
+
"@fuzdev/blake3_wasm": "^0.1.1",
|
|
58
|
+
"@fuzdev/fuz_css": "^0.62.0",
|
|
59
|
+
"@fuzdev/fuz_ui": "^0.199.0",
|
|
60
|
+
"@fuzdev/fuz_util": "^0.65.1",
|
|
61
|
+
"@fuzdev/gro": "^0.203.0",
|
|
62
|
+
"@ryanatkn/eslint-config": "^0.12.1",
|
|
63
|
+
"@sveltejs/acorn-typescript": "^1.0.9",
|
|
67
64
|
"@sveltejs/adapter-static": "^3.0.10",
|
|
68
|
-
"@sveltejs/kit": "^2.
|
|
65
|
+
"@sveltejs/kit": "^2.63.0",
|
|
69
66
|
"@sveltejs/package": "^2.5.7",
|
|
70
67
|
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
|
71
68
|
"@types/estree": "^1.0.8",
|
|
@@ -74,17 +71,17 @@
|
|
|
74
71
|
"eslint": "^9.39.1",
|
|
75
72
|
"eslint-plugin-svelte": "^3.13.1",
|
|
76
73
|
"esm-env": "^1.2.2",
|
|
77
|
-
"magic-string": "^0.30.21",
|
|
78
74
|
"prettier": "^3.7.4",
|
|
79
75
|
"prettier-plugin-svelte": "^3.4.1",
|
|
80
|
-
"svelte": "^5.
|
|
81
|
-
"svelte-check": "^4.
|
|
82
|
-
"
|
|
76
|
+
"svelte": "^5.56.2",
|
|
77
|
+
"svelte-check": "^4.6.0",
|
|
78
|
+
"svelte-docinfo": "^0.3.0",
|
|
79
|
+
"svelte2tsx": "^0.7.55",
|
|
83
80
|
"tslib": "^2.8.1",
|
|
84
81
|
"typescript": "^5.9.3",
|
|
85
82
|
"typescript-eslint": "^8.48.1",
|
|
83
|
+
"vite": "^7.3.1",
|
|
86
84
|
"vitest": "^4.0.15",
|
|
87
|
-
"zimmerframe": "^1.1.4",
|
|
88
85
|
"zod": "^4.3.6"
|
|
89
86
|
},
|
|
90
87
|
"prettier": {
|
|
@@ -16,7 +16,7 @@ interface FenceType {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Helper to create fenced code block pattern for a language
|
|
19
|
+
* Helper to create fenced code block pattern for a language.
|
|
20
20
|
*/
|
|
21
21
|
const create_fence_pattern = (
|
|
22
22
|
backticks: string,
|
|
@@ -48,7 +48,7 @@ const create_fence_pattern = (
|
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
|
-
* Helper to create catch-all fence pattern (unknown languages)
|
|
51
|
+
* Helper to create catch-all fence pattern (unknown languages).
|
|
52
52
|
*/
|
|
53
53
|
const create_catchall_fence = (backticks: string): SyntaxGrammarTokenRaw => {
|
|
54
54
|
const pattern = new RegExp(`^${backticks}[^\\n\\r]*(?:\\r?\\n|\\r)[\\s\\S]*?^${backticks}$`, 'm');
|
|
@@ -67,7 +67,7 @@ const create_catchall_fence = (backticks: string): SyntaxGrammarTokenRaw => {
|
|
|
67
67
|
};
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
|
-
* Helper to create md self-reference placeholder pattern
|
|
70
|
+
* Helper to create md self-reference placeholder pattern.
|
|
71
71
|
*/
|
|
72
72
|
const create_md_placeholder = (backticks: string): SyntaxGrammarTokenRaw => {
|
|
73
73
|
const pattern = new RegExp(
|
|
@@ -98,9 +98,10 @@ export const add_grammar_markup: AddSyntaxGrammar = (syntax_styler) => {
|
|
|
98
98
|
*
|
|
99
99
|
* An example of an inlined language is CSS with `<style>` tags.
|
|
100
100
|
*
|
|
101
|
-
* @param
|
|
102
|
-
* case insensitive
|
|
103
|
-
* @param lang -
|
|
101
|
+
* @param syntax_styler - the `SyntaxStyler` instance to modify
|
|
102
|
+
* @param tag_name - the name of the tag that contains the inlined language, treated as case insensitive
|
|
103
|
+
* @param lang - the language key
|
|
104
|
+
* @param inside_lang - the language to insert into, defaults to `'markup'`
|
|
104
105
|
*/
|
|
105
106
|
export const grammar_markup_add_inlined = (
|
|
106
107
|
syntax_styler: SyntaxStyler,
|
|
@@ -145,13 +146,13 @@ export const grammar_markup_add_inlined = (
|
|
|
145
146
|
};
|
|
146
147
|
|
|
147
148
|
/**
|
|
148
|
-
* Adds
|
|
149
|
+
* Adds a pattern to style languages embedded in HTML attributes.
|
|
149
150
|
*
|
|
150
151
|
* An example of an inlined language is CSS with `style` attributes.
|
|
151
152
|
*
|
|
152
|
-
* @param
|
|
153
|
-
* case insensitive
|
|
154
|
-
* @param lang -
|
|
153
|
+
* @param syntax_styler - the `SyntaxStyler` instance to modify
|
|
154
|
+
* @param attr_name - the name of the attribute that contains the inlined language, treated as case insensitive
|
|
155
|
+
* @param lang - the language key
|
|
155
156
|
*/
|
|
156
157
|
export const grammar_markup_add_attribute = (
|
|
157
158
|
syntax_styler: SyntaxStyler,
|
|
@@ -4,16 +4,16 @@ import {highlight_priorities} from './highlight_priorities.js';
|
|
|
4
4
|
export type HighlightMode = 'auto' | 'ranges' | 'html';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* Checks for CSS Highlights API support.
|
|
8
8
|
*/
|
|
9
9
|
export const supports_css_highlight_api = (): boolean =>
|
|
10
|
-
!!(globalThis.CSS?.highlights && globalThis.Highlight);
|
|
10
|
+
!!(globalThis.CSS?.highlights && globalThis.Highlight);
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Manages CSS Custom Highlight API ranges for a single element.
|
|
14
14
|
* Tracks ranges per element and only removes its own ranges when clearing.
|
|
15
15
|
*
|
|
16
|
-
* **Experimental** — limited browser support. Use `Code` for production.
|
|
16
|
+
* **Experimental** — limited browser support. Use `Code.svelte` for production.
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* ```ts
|
|
@@ -32,7 +32,7 @@ export class HighlightManager {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* Highlights from a `SyntaxTokenStream` produced by `tokenize_syntax`.
|
|
36
36
|
*/
|
|
37
37
|
highlight_from_syntax_tokens(element: Element, tokens: SyntaxTokenStream): void {
|
|
38
38
|
// Find the text node (it might not be firstChild due to Svelte comment nodes)
|
|
@@ -85,7 +85,7 @@ export class HighlightManager {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
|
-
*
|
|
88
|
+
* Clears only this element's ranges from highlights.
|
|
89
89
|
*/
|
|
90
90
|
clear_element_ranges(): void {
|
|
91
91
|
for (const [name, ranges] of this.element_ranges) {
|
|
@@ -111,7 +111,7 @@ export class HighlightManager {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
/**
|
|
114
|
-
*
|
|
114
|
+
* Creates ranges for all tokens in the tree.
|
|
115
115
|
*/
|
|
116
116
|
#create_all_ranges(
|
|
117
117
|
tokens: SyntaxTokenStream,
|
|
@@ -23,15 +23,15 @@ export interface PreprocessFuzCodeOptions {
|
|
|
23
23
|
/** File patterns to exclude. */
|
|
24
24
|
exclude?: Array<string | RegExp>;
|
|
25
25
|
|
|
26
|
-
/** Custom
|
|
26
|
+
/** Custom `SyntaxStyler` instance. @default syntax_styler_global */
|
|
27
27
|
syntax_styler?: SyntaxStyler;
|
|
28
28
|
|
|
29
29
|
/** Enable in-memory caching. @default true */
|
|
30
30
|
cache?: boolean;
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* Import sources that resolve to the Code component.
|
|
34
|
-
* Used to verify that `<Code>` in templates actually refers to
|
|
33
|
+
* Import sources that resolve to the `Code` component.
|
|
34
|
+
* Used to verify that `<Code>` in templates actually refers to `Code.svelte`.
|
|
35
35
|
*
|
|
36
36
|
* @default ['@fuzdev/fuz_code/Code.svelte']
|
|
37
37
|
*/
|
|
@@ -48,7 +48,7 @@ export interface PreprocessFuzCodeOptions {
|
|
|
48
48
|
* Svelte preprocessor that compiles static `Code` component content at build time,
|
|
49
49
|
* replacing runtime syntax highlighting with pre-rendered HTML.
|
|
50
50
|
*
|
|
51
|
-
* @param options
|
|
51
|
+
* @param options - `PreprocessFuzCodeOptions` configuration
|
|
52
52
|
* @returns a Svelte preprocessor group
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
@@ -141,7 +141,7 @@ interface FindCodeUsagesOptions {
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
/**
|
|
144
|
-
*
|
|
144
|
+
* Attempts to highlight content, using cache if available.
|
|
145
145
|
* Returns the highlighted HTML, or `null` on error.
|
|
146
146
|
*/
|
|
147
147
|
const try_highlight = (
|
|
@@ -165,7 +165,7 @@ const try_highlight = (
|
|
|
165
165
|
};
|
|
166
166
|
|
|
167
167
|
/**
|
|
168
|
-
* Walks the AST to find Code component usages with static `content` props
|
|
168
|
+
* Walks the AST to find `Code` component usages with static `content` props
|
|
169
169
|
* and generates transformations to replace them with `dangerous_raw_html`.
|
|
170
170
|
*/
|
|
171
171
|
const find_code_usages = (
|
package/src/lib/syntax_styler.ts
CHANGED
|
@@ -92,15 +92,14 @@ export class SyntaxStyler {
|
|
|
92
92
|
* - Custom grammar: `stylize(code, 'ts', customGrammar)` - uses custom grammar but keeps 'ts' label
|
|
93
93
|
* - Extended grammar: `stylize(code, 'custom', this.extend_grammar('ts', extension))` - new language variant
|
|
94
94
|
*
|
|
95
|
-
* @param text -
|
|
96
|
-
* @param lang -
|
|
97
|
-
* -
|
|
98
|
-
* -
|
|
99
|
-
* -
|
|
100
|
-
* @param grammar -
|
|
101
|
-
* looks up the grammar via `this.get_lang(lang)
|
|
102
|
-
* or modified grammar instead of the registered one
|
|
103
|
-
*
|
|
95
|
+
* @param text - the source code to syntax highlight
|
|
96
|
+
* @param lang - language identifier (e.g., 'ts', 'css', 'html'), used for:
|
|
97
|
+
* - grammar lookup when `grammar` is undefined
|
|
98
|
+
* - hook context (`lang` field passed to hooks)
|
|
99
|
+
* - language identification in output
|
|
100
|
+
* @param grammar - optional custom `SyntaxGrammar` object; when undefined, automatically
|
|
101
|
+
* looks up the grammar via `this.get_lang(lang)`; provide this to use a custom
|
|
102
|
+
* or modified grammar instead of the registered one
|
|
104
103
|
* @returns HTML string with syntax highlighting using CSS classes (`.token_*`)
|
|
105
104
|
*
|
|
106
105
|
* @example
|
|
@@ -158,7 +157,7 @@ export class SyntaxStyler {
|
|
|
158
157
|
* };
|
|
159
158
|
* ```
|
|
160
159
|
*
|
|
161
|
-
* then the `style` token will be added (and processed) at the end. `
|
|
160
|
+
* then the `style` token will be added (and processed) at the end. `grammar_insert_before` allows you to insert tokens
|
|
162
161
|
* before existing tokens. For the CSS example above, you would use it like this:
|
|
163
162
|
*
|
|
164
163
|
* ```js
|
|
@@ -185,12 +184,12 @@ export class SyntaxStyler {
|
|
|
185
184
|
*
|
|
186
185
|
* ## Limitations
|
|
187
186
|
*
|
|
188
|
-
* The main problem `
|
|
187
|
+
* The main problem `grammar_insert_before` has to solve is iteration order. Since ES2015, the iteration order for object
|
|
189
188
|
* properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
|
|
190
|
-
* differently when keys are deleted and re-inserted. So `
|
|
189
|
+
* differently when keys are deleted and re-inserted. So `grammar_insert_before` can't be implemented by temporarily
|
|
191
190
|
* deleting properties which is necessary to insert at arbitrary positions.
|
|
192
191
|
*
|
|
193
|
-
* To solve this problem, `
|
|
192
|
+
* To solve this problem, `grammar_insert_before` doesn't actually insert the given tokens into the target object.
|
|
194
193
|
* Instead, it will create a new object and replace all references to the target object with the new one. This
|
|
195
194
|
* can be done without temporarily deleting properties, so the iteration order is well-defined.
|
|
196
195
|
*
|
|
@@ -205,16 +204,13 @@ export class SyntaxStyler {
|
|
|
205
204
|
* assert(newMarkup === syntax_styler.get_lang('markup'));
|
|
206
205
|
* ```
|
|
207
206
|
*
|
|
208
|
-
* @param inside -
|
|
209
|
-
* object to be modified
|
|
210
|
-
* @param before -
|
|
211
|
-
* @param insert -
|
|
212
|
-
* @param root -
|
|
213
|
-
* object to be modified.
|
|
214
|
-
*
|
|
215
|
-
* Defaults to `syntax_styler.langs`.
|
|
216
|
-
*
|
|
217
|
-
* @returns the new grammar object
|
|
207
|
+
* @param inside - the property of `root` (e.g. a language id in `syntax_styler.langs`) that contains the
|
|
208
|
+
* object to be modified
|
|
209
|
+
* @param before - the key to insert before
|
|
210
|
+
* @param insert - an object containing the key-value pairs to be inserted
|
|
211
|
+
* @param root - the object containing `inside`, i.e. the object that contains the
|
|
212
|
+
* object to be modified; defaults to `syntax_styler.langs`
|
|
213
|
+
* @returns the new `SyntaxGrammar` object
|
|
218
214
|
*/
|
|
219
215
|
grammar_insert_before(
|
|
220
216
|
inside: string,
|
|
@@ -261,9 +257,9 @@ export class SyntaxStyler {
|
|
|
261
257
|
*
|
|
262
258
|
* Runs the `wrap` hook on each `SyntaxToken`.
|
|
263
259
|
*
|
|
264
|
-
* @param o -
|
|
265
|
-
* @param lang -
|
|
266
|
-
* @returns
|
|
260
|
+
* @param o - the token or `SyntaxTokenStream` to be converted
|
|
261
|
+
* @param lang - the name of current language
|
|
262
|
+
* @returns HTML representation of the token or token stream
|
|
267
263
|
*/
|
|
268
264
|
stringify_token(o: string | SyntaxToken | SyntaxTokenStream, lang: string): string {
|
|
269
265
|
if (typeof o === 'string') {
|
|
@@ -332,9 +328,9 @@ export class SyntaxStyler {
|
|
|
332
328
|
* Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
|
|
333
329
|
* Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
|
|
334
330
|
*
|
|
335
|
-
* @param base_id -
|
|
336
|
-
* @param extension -
|
|
337
|
-
* @returns the new
|
|
331
|
+
* @param base_id - the id of the language to extend, must be a key in `syntax_styler.langs`
|
|
332
|
+
* @param extension - the new tokens to append
|
|
333
|
+
* @returns the new `SyntaxGrammar`
|
|
338
334
|
*/
|
|
339
335
|
extend_grammar(base_id: string, extension: SyntaxGrammarRaw): SyntaxGrammar {
|
|
340
336
|
// Merge normalized base with un-normalized extension
|
|
@@ -346,7 +342,7 @@ export class SyntaxStyler {
|
|
|
346
342
|
}
|
|
347
343
|
|
|
348
344
|
/**
|
|
349
|
-
*
|
|
345
|
+
* Normalizes a single pattern to have consistent shape.
|
|
350
346
|
* This ensures all patterns have the same object shape for V8 optimization.
|
|
351
347
|
*/
|
|
352
348
|
#normalize_pattern(
|
|
@@ -387,15 +383,15 @@ export class SyntaxStyler {
|
|
|
387
383
|
}
|
|
388
384
|
|
|
389
385
|
/**
|
|
390
|
-
*
|
|
386
|
+
* Normalizes a grammar to have consistent object shapes.
|
|
391
387
|
* This performs several optimizations:
|
|
392
|
-
* 1. Merges `rest` property into main grammar
|
|
393
|
-
* 2. Ensures all pattern values are arrays
|
|
394
|
-
* 3. Normalizes all pattern objects to have consistent shapes
|
|
395
|
-
* 4. Adds global flag to greedy patterns
|
|
388
|
+
* 1. Merges `rest` property into main grammar.
|
|
389
|
+
* 2. Ensures all pattern values are arrays.
|
|
390
|
+
* 3. Normalizes all pattern objects to have consistent shapes.
|
|
391
|
+
* 4. Adds global flag to greedy patterns.
|
|
396
392
|
*
|
|
397
393
|
* This is called once at registration time to avoid runtime overhead.
|
|
398
|
-
* @param visited -
|
|
394
|
+
* @param visited - set of grammar object IDs already normalized (for circular references)
|
|
399
395
|
*/
|
|
400
396
|
#normalize_grammar(grammar: SyntaxGrammarRaw, visited: Set<number>): void {
|
|
401
397
|
// Check if we've already normalized this grammar (circular reference)
|
|
@@ -510,14 +506,14 @@ export interface SyntaxGrammarTokenRaw {
|
|
|
510
506
|
*/
|
|
511
507
|
alias?: string | Array<string>;
|
|
512
508
|
/**
|
|
513
|
-
* The nested
|
|
509
|
+
* The nested `SyntaxGrammarRaw` of this token.
|
|
514
510
|
*/
|
|
515
511
|
inside?: SyntaxGrammarRaw | null;
|
|
516
512
|
}
|
|
517
513
|
|
|
518
514
|
/**
|
|
519
515
|
* Grammar token with all properties required.
|
|
520
|
-
* This is the normalized representation used at runtime.
|
|
516
|
+
* This is the normalized representation of `SyntaxGrammarTokenRaw` used at runtime.
|
|
521
517
|
*/
|
|
522
518
|
export interface SyntaxGrammarToken {
|
|
523
519
|
pattern: RegExp;
|
|
@@ -529,7 +525,7 @@ export interface SyntaxGrammarToken {
|
|
|
529
525
|
|
|
530
526
|
/**
|
|
531
527
|
* A grammar after normalization.
|
|
532
|
-
* All values are arrays of normalized
|
|
528
|
+
* All values are arrays of normalized `SyntaxGrammarToken` with consistent shapes.
|
|
533
529
|
*/
|
|
534
530
|
export type SyntaxGrammar = Record<string, Array<SyntaxGrammarToken>>;
|
|
535
531
|
|
package/src/lib/syntax_token.ts
CHANGED
|
@@ -2,14 +2,14 @@ export class SyntaxToken {
|
|
|
2
2
|
/**
|
|
3
3
|
* The type of the token.
|
|
4
4
|
*
|
|
5
|
-
* This is usually the key of a pattern in a `
|
|
5
|
+
* This is usually the key of a pattern in a `SyntaxGrammar`.
|
|
6
6
|
*/
|
|
7
7
|
type: string;
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* The strings or tokens contained by this token.
|
|
11
11
|
*
|
|
12
|
-
* This will be a
|
|
12
|
+
* This will be a `SyntaxTokenStream` if the pattern matched also defined an `inside` grammar.
|
|
13
13
|
*/
|
|
14
14
|
content: string | SyntaxTokenStream;
|
|
15
15
|
|
|
@@ -38,8 +38,8 @@ export class SyntaxToken {
|
|
|
38
38
|
/**
|
|
39
39
|
* A token stream is an array of strings and `SyntaxToken` objects.
|
|
40
40
|
*
|
|
41
|
-
*
|
|
42
|
-
* them.
|
|
41
|
+
* `SyntaxTokenStream` values have to fulfill a few properties that are assumed by most functions
|
|
42
|
+
* (mostly internal ones) that process them.
|
|
43
43
|
*
|
|
44
44
|
* 1. No adjacent strings.
|
|
45
45
|
* 2. No empty strings.
|
|
@@ -10,11 +10,9 @@ import {SyntaxToken, type SyntaxTokenStream} from './syntax_token.js';
|
|
|
10
10
|
* This method could be useful in other contexts as well, as a very crude parser.
|
|
11
11
|
*
|
|
12
12
|
* @param text - a string with the code to be styled
|
|
13
|
-
* @param grammar -
|
|
14
|
-
*
|
|
13
|
+
* @param grammar - a `SyntaxGrammar` object containing the tokens to use.
|
|
15
14
|
* Usually a language definition like `syntax_styler.get_lang('markup')`.
|
|
16
|
-
*
|
|
17
|
-
* @returns an array of strings and tokens, a token stream
|
|
15
|
+
* @returns a `SyntaxTokenStream` array of strings and tokens
|
|
18
16
|
*
|
|
19
17
|
* @example
|
|
20
18
|
* ```ts
|