@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,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Uses the CSS Custom Highlight API when available --
|
|
3
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/CSS_Custom_Highlight_API
|
|
4
|
+
*
|
|
5
|
+
* Requires importing theme_highlight.css instead of theme.css.
|
|
6
|
+
*/
|
|
7
|
+
import { type Snippet } from 'svelte';
|
|
8
|
+
import type { SvelteHTMLElements } from 'svelte/elements';
|
|
9
|
+
import type { SyntaxStyler, SyntaxGrammar } from './syntax_styler.js';
|
|
10
|
+
import { type HighlightMode } from './highlight_manager.js';
|
|
11
|
+
type $$ComponentProps = SvelteHTMLElements['code'] & {
|
|
12
|
+
/** The source code to syntax highlight. */
|
|
13
|
+
content: string;
|
|
14
|
+
/**
|
|
15
|
+
* Language identifier (e.g., 'ts', 'css', 'html', 'json', 'svelte', 'md').
|
|
16
|
+
*
|
|
17
|
+
* **Purpose:**
|
|
18
|
+
* - When `grammar` is not provided, used to look up the grammar via `syntax_styler.get_lang(lang)`
|
|
19
|
+
* - Used for metadata: sets the `data-lang` attribute and determines `language_supported`
|
|
20
|
+
*
|
|
21
|
+
* **Special values:**
|
|
22
|
+
* - `null` - Explicitly disables syntax highlighting (content rendered as plain text)
|
|
23
|
+
* - `undefined` - Falls back to default ('svelte')
|
|
24
|
+
*
|
|
25
|
+
* **Relationship with `grammar`:**
|
|
26
|
+
* - If both `lang` and `grammar` are provided, `grammar` takes precedence for tokenization
|
|
27
|
+
* - However, `lang` is still used for the `data-lang` attribute and language detection
|
|
28
|
+
*
|
|
29
|
+
* @default 'svelte'
|
|
30
|
+
*/
|
|
31
|
+
lang?: string | null;
|
|
32
|
+
/**
|
|
33
|
+
* Highlighting mode for this component.
|
|
34
|
+
*
|
|
35
|
+
* **Options:**
|
|
36
|
+
* - `'auto'` - Uses CSS Custom Highlight API if supported, falls back to HTML mode
|
|
37
|
+
* - `'ranges'` - Forces CSS Custom Highlight API (requires browser support)
|
|
38
|
+
* - `'html'` - Forces HTML generation with CSS classes
|
|
39
|
+
*
|
|
40
|
+
* **Note:** CSS Custom Highlight API has limitations and limited browser support.
|
|
41
|
+
* Requires importing `theme_highlight.css` instead of `theme.css`.
|
|
42
|
+
*
|
|
43
|
+
* @default 'auto'
|
|
44
|
+
*/
|
|
45
|
+
mode?: HighlightMode;
|
|
46
|
+
/**
|
|
47
|
+
* Optional custom grammar object for syntax tokenization.
|
|
48
|
+
*
|
|
49
|
+
* **When to use:**
|
|
50
|
+
* - To provide a custom language definition not registered in `syntax_styler.langs`
|
|
51
|
+
* - To use a modified/extended version of an existing grammar
|
|
52
|
+
* - For one-off grammar variations without registering globally
|
|
53
|
+
*
|
|
54
|
+
* **Behavior:**
|
|
55
|
+
* - When provided, this grammar is used for tokenization instead of looking up via `lang`
|
|
56
|
+
* - Enables highlighting even if `lang` is not in the registry (useful for custom languages)
|
|
57
|
+
* - The `lang` parameter is still used for metadata (data-lang attribute)
|
|
58
|
+
* - When undefined, the grammar is automatically looked up via `syntax_styler.get_lang(lang)`
|
|
59
|
+
*
|
|
60
|
+
* @default undefined (uses grammar from `syntax_styler.langs[lang]`)
|
|
61
|
+
*/
|
|
62
|
+
grammar?: SyntaxGrammar | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Whether to render as inline code or block code.
|
|
65
|
+
* Controls display via CSS classes.
|
|
66
|
+
*
|
|
67
|
+
* @default false
|
|
68
|
+
*/
|
|
69
|
+
inline?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Whether to wrap long lines in block code.
|
|
72
|
+
* Sets `white-space: pre-wrap` instead of `white-space: pre`.
|
|
73
|
+
*
|
|
74
|
+
* **Behavior:**
|
|
75
|
+
* - Wraps at whitespace (spaces, newlines)
|
|
76
|
+
* - Long tokens without spaces (URLs, hashes) will still scroll horizontally
|
|
77
|
+
* - Default `false` provides traditional code block behavior
|
|
78
|
+
*
|
|
79
|
+
* Only affects block code (ignored for inline mode).
|
|
80
|
+
*
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
83
|
+
wrap?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Custom SyntaxStyler instance to use for highlighting.
|
|
86
|
+
* Allows using a different styler with custom grammars or configuration.
|
|
87
|
+
*
|
|
88
|
+
* @default syntax_styler_global
|
|
89
|
+
*/
|
|
90
|
+
syntax_styler?: SyntaxStyler;
|
|
91
|
+
/**
|
|
92
|
+
* Optional snippet to customize how the highlighted markup is rendered.
|
|
93
|
+
* - In HTML mode: receives the generated HTML string
|
|
94
|
+
* - In range mode: receives the plain text content
|
|
95
|
+
*/
|
|
96
|
+
children?: Snippet<[markup: string]>;
|
|
97
|
+
};
|
|
98
|
+
declare const CodeHighlight: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
99
|
+
type CodeHighlight = ReturnType<typeof CodeHighlight>;
|
|
100
|
+
export default CodeHighlight;
|
|
101
|
+
//# sourceMappingURL=CodeHighlight.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeHighlight.svelte.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/CodeHighlight.svelte"],"names":[],"mappings":"AAGA;;;;;OAKI;AACJ,OAAO,EAAY,KAAK,OAAO,EAAC,MAAM,QAAQ,CAAC;AAE/C,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,iBAAiB,CAAC;AAGxD,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAEpE,OAAO,EAGL,KAAK,aAAa,EAClB,MAAM,wBAAwB,CAAC;AAEhC,KAAK,gBAAgB,GAAI,kBAAkB,CAAC,MAAM,CAAC,GAAG;IACrD,2CAA2C;IAC3C,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;;;;;;;;;;;OAgBG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB;;;;;;;;;;;;OAYG;IACH,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB;;;;;;;;;;;;;;;OAeG;IACH,OAAO,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;IACpC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;;;;;;;;;;;OAYG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;IACf;;;;;OAKG;IACH,aAAa,CAAC,EAAE,YAAY,CAAC;IAC7B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACrC,CAAC;AAgGH,QAAA,MAAM,aAAa,sDAAwC,CAAC;AAC5D,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACtD,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface CodeSample {
|
|
2
|
+
name: string;
|
|
3
|
+
lang: string;
|
|
4
|
+
content: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const sample_langs: readonly ["json", "css", "ts", "html", "svelte", "md"];
|
|
7
|
+
export type SampleLang = (typeof sample_langs)[number];
|
|
8
|
+
//# sourceMappingURL=code_sample.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code_sample.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/code_sample.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CAChB;AAGD,eAAO,MAAM,YAAY,wDAAyD,CAAC;AAEnF,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AddSyntaxGrammar } from './syntax_styler.js';
|
|
2
|
+
export declare const class_keywords = "class|extends|implements|instanceof|interface|new";
|
|
3
|
+
/**
|
|
4
|
+
* Based on Prism (https://github.com/PrismJS/prism)
|
|
5
|
+
* by Lea Verou (https://lea.verou.me/)
|
|
6
|
+
*
|
|
7
|
+
* MIT license
|
|
8
|
+
*
|
|
9
|
+
* @see LICENSE
|
|
10
|
+
*/
|
|
11
|
+
export declare const add_grammar_clike: AddSyntaxGrammar;
|
|
12
|
+
//# sourceMappingURL=grammar_clike.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_clike.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_clike.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAmB,MAAM,oBAAoB,CAAC;AAE3E,eAAO,MAAM,cAAc,sDAAsD,CAAC;AAElF;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,EAAE,gBAmC/B,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const class_keywords = 'class|extends|implements|instanceof|interface|new';
|
|
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 const add_grammar_clike = (syntax_styler) => {
|
|
11
|
+
const grammar_clike = {
|
|
12
|
+
comment: [
|
|
13
|
+
{
|
|
14
|
+
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
|
|
15
|
+
lookbehind: true,
|
|
16
|
+
greedy: true,
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
pattern: /(^|[^\\:])\/\/.*/,
|
|
20
|
+
lookbehind: true,
|
|
21
|
+
greedy: true,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
string: {
|
|
25
|
+
pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
|
|
26
|
+
greedy: true,
|
|
27
|
+
},
|
|
28
|
+
class_name: {
|
|
29
|
+
pattern: new RegExp(`(\\b(?:${class_keywords}|trait)\\s+|\\bcatch\\s+\\()[\\w.\\\\]+`, 'i'),
|
|
30
|
+
lookbehind: true,
|
|
31
|
+
inside: {
|
|
32
|
+
punctuation: /[.\\]/,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
keyword: /\b(?:break|catch|continue|do|else|finally|for|function|if|in|instanceof|new|null|return|throw|try|while)\b/,
|
|
36
|
+
boolean: /\b(?:false|true)\b/,
|
|
37
|
+
function: /\b\w+(?=\()/,
|
|
38
|
+
number: /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
|
|
39
|
+
operator: /[<>]=?|[!=]=?=?|--?|\+\+?|&&?|\|\|?|[?*/~^%]/,
|
|
40
|
+
punctuation: /[{}[\];(),.:]/,
|
|
41
|
+
};
|
|
42
|
+
syntax_styler.add_lang('clike', grammar_clike);
|
|
43
|
+
};
|
|
@@ -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_css: AddSyntaxGrammar;
|
|
11
|
+
//# sourceMappingURL=grammar_css.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_css.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_css.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAmB,MAAM,oBAAoB,CAAC;AAK3E;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,EAAE,gBAsE7B,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { grammar_markup_add_attribute, grammar_markup_add_inlined } from './grammar_markup.js';
|
|
2
|
+
var string = /(?:"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n])*')/;
|
|
3
|
+
/**
|
|
4
|
+
* Based on Prism (https://github.com/PrismJS/prism)
|
|
5
|
+
* by Lea Verou (https://lea.verou.me/)
|
|
6
|
+
*
|
|
7
|
+
* MIT license
|
|
8
|
+
*
|
|
9
|
+
* @see LICENSE
|
|
10
|
+
*/
|
|
11
|
+
export const add_grammar_css = (syntax_styler) => {
|
|
12
|
+
const grammar_css = {
|
|
13
|
+
comment: /\/\*[\s\S]*?\*\//,
|
|
14
|
+
atrule: {
|
|
15
|
+
pattern: RegExp('@[\\w-](?:' +
|
|
16
|
+
/[^;{\s"']|\s+(?!\s)/.source +
|
|
17
|
+
'|' +
|
|
18
|
+
string.source +
|
|
19
|
+
')*?' +
|
|
20
|
+
/(?:;|(?=\s*\{))/.source),
|
|
21
|
+
inside: {
|
|
22
|
+
rule: /^@[\w-]+/,
|
|
23
|
+
selector_function_argument: {
|
|
24
|
+
pattern: /(\bselector\s*\(\s*(?![\s)]))(?:[^()\s]|\s+(?![\s)])|\((?:[^()]|\([^()]*\))*\))+(?=\s*\))/,
|
|
25
|
+
lookbehind: true,
|
|
26
|
+
alias: 'selector',
|
|
27
|
+
},
|
|
28
|
+
keyword: {
|
|
29
|
+
pattern: /(^|[^\w-])(?:and|not|only|or)(?![\w-])/,
|
|
30
|
+
lookbehind: true,
|
|
31
|
+
},
|
|
32
|
+
}, // see `rest` below
|
|
33
|
+
},
|
|
34
|
+
url: {
|
|
35
|
+
// https://drafts.csswg.org/css-values-3/#urls
|
|
36
|
+
pattern: RegExp('\\burl\\((?:' + string.source + '|' + /(?:[^\\\r\n()"']|\\[\s\S])*/.source + ')\\)', 'i'),
|
|
37
|
+
greedy: true,
|
|
38
|
+
inside: {
|
|
39
|
+
function: /^url/i,
|
|
40
|
+
punctuation: /^\(|\)$/,
|
|
41
|
+
string: {
|
|
42
|
+
pattern: RegExp('^' + string.source + '$'),
|
|
43
|
+
alias: 'url',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
selector: {
|
|
48
|
+
pattern: RegExp('(^|[{}\\s])[^{}\\s](?:[^{};"\'\\s]|\\s+(?![\\s{])|' + string.source + ')*(?=\\s*\\{)'),
|
|
49
|
+
lookbehind: true,
|
|
50
|
+
},
|
|
51
|
+
string: {
|
|
52
|
+
pattern: string,
|
|
53
|
+
greedy: true,
|
|
54
|
+
},
|
|
55
|
+
property: {
|
|
56
|
+
pattern: /(^|[^-\w\xA0-\uFFFF])(?!\s)[-_a-z\xA0-\uFFFF](?:(?!\s)[-\w\xA0-\uFFFF])*(?=\s*:)/i,
|
|
57
|
+
lookbehind: true,
|
|
58
|
+
},
|
|
59
|
+
important: /!important\b/i,
|
|
60
|
+
function: {
|
|
61
|
+
pattern: /(^|[^-a-z0-9])[-a-z0-9]+(?=\()/i,
|
|
62
|
+
lookbehind: true,
|
|
63
|
+
},
|
|
64
|
+
punctuation: /[(){};:,]/,
|
|
65
|
+
};
|
|
66
|
+
grammar_css.atrule.inside.rest = grammar_css;
|
|
67
|
+
syntax_styler.add_lang('css', grammar_css);
|
|
68
|
+
grammar_markup_add_inlined(syntax_styler, 'style', 'css');
|
|
69
|
+
grammar_markup_add_attribute(syntax_styler, 'style', 'css');
|
|
70
|
+
};
|
|
@@ -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_js: AddSyntaxGrammar;
|
|
11
|
+
//# sourceMappingURL=grammar_js.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_js.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_js.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AAIzD;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,EAAE,gBA0M5B,CAAC"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { grammar_markup_add_attribute, grammar_markup_add_inlined } from './grammar_markup.js';
|
|
2
|
+
import { class_keywords } from './grammar_clike.js';
|
|
3
|
+
/**
|
|
4
|
+
* Based on Prism (https://github.com/PrismJS/prism)
|
|
5
|
+
* by Lea Verou (https://lea.verou.me/)
|
|
6
|
+
*
|
|
7
|
+
* MIT license
|
|
8
|
+
*
|
|
9
|
+
* @see LICENSE
|
|
10
|
+
*/
|
|
11
|
+
export const add_grammar_js = (syntax_styler) => {
|
|
12
|
+
const grammar_clike = syntax_styler.get_lang('clike');
|
|
13
|
+
// Main JS keywords (from keyword pattern, excluding those with special lookaheads)
|
|
14
|
+
const main_keywords = 'class|const|debugger|delete|enum|extends|function|implements|in|instanceof|interface|let|new|null|of|package|private|protected|public|static|super|this|typeof|undefined|var|void|with';
|
|
15
|
+
// Keywords that are treated specially (inserted before 'function')
|
|
16
|
+
const special_keywords = 'as|await|break|case|catch|continue|default|do|else|export|finally|for|from|if|import|return|switch|throw|try|while|yield';
|
|
17
|
+
// All JS keywords (for negative lookahead in parameter pattern)
|
|
18
|
+
// Note: 'assert', 'async', 'get', 'set' have special lookahead requirements in the main keyword pattern
|
|
19
|
+
const all_js_keywords = `assert|async|${main_keywords}|get|set|${special_keywords}`;
|
|
20
|
+
const grammar_js = syntax_styler.add_extended_lang('clike', 'js', {
|
|
21
|
+
class_name: [
|
|
22
|
+
...grammar_clike.class_name,
|
|
23
|
+
{
|
|
24
|
+
pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$A-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\.(?:constructor|prototype))/,
|
|
25
|
+
lookbehind: true,
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
keyword: [
|
|
29
|
+
{
|
|
30
|
+
pattern: new RegExp(`(^|[^.]|\\.\\.\\.\\s*)\\b(?:assert(?=\\s*\\{)|async(?=\\s*(?:function\\b|\\*|\\(|[$\\w\\xA0-\\uFFFF]|$))|${main_keywords}|(?:get|set)(?=\\s*(?:[#[$\\w\\xA0-\\uFFFF]|$)))\\b`),
|
|
31
|
+
lookbehind: true,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
|
|
35
|
+
function: /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
|
|
36
|
+
number: {
|
|
37
|
+
pattern: RegExp(/(^|[^\w$])/.source +
|
|
38
|
+
'(?:' +
|
|
39
|
+
// constant
|
|
40
|
+
(/NaN|Infinity/.source +
|
|
41
|
+
'|' +
|
|
42
|
+
// binary integer
|
|
43
|
+
/0[bB][01]+(?:_[01]+)*n?/.source +
|
|
44
|
+
'|' +
|
|
45
|
+
// octal integer
|
|
46
|
+
/0[oO][0-7]+(?:_[0-7]+)*n?/.source +
|
|
47
|
+
'|' +
|
|
48
|
+
// hexadecimal integer
|
|
49
|
+
/0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
|
|
50
|
+
'|' +
|
|
51
|
+
// decimal bigint
|
|
52
|
+
/\d+(?:_\d+)*n/.source +
|
|
53
|
+
'|' +
|
|
54
|
+
// decimal number (integer or float) but no bigint
|
|
55
|
+
/(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/
|
|
56
|
+
.source) +
|
|
57
|
+
')' +
|
|
58
|
+
/(?![\w$])/.source),
|
|
59
|
+
lookbehind: true,
|
|
60
|
+
},
|
|
61
|
+
operator: /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/,
|
|
62
|
+
}, ['javascript']);
|
|
63
|
+
grammar_js.class_name[0].pattern = new RegExp(`(\\b(?:${class_keywords})\\s+)[\\w.\\\\]+`);
|
|
64
|
+
syntax_styler.grammar_insert_before('js', 'function', {
|
|
65
|
+
special_keyword: new RegExp(`\\b(?:${special_keywords})\\b`),
|
|
66
|
+
});
|
|
67
|
+
syntax_styler.grammar_insert_before('js', 'keyword', {
|
|
68
|
+
regex: {
|
|
69
|
+
pattern: RegExp(
|
|
70
|
+
// lookbehind
|
|
71
|
+
/((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)/.source +
|
|
72
|
+
// Regex pattern:
|
|
73
|
+
// There are 2 regex patterns here. The RegExp set notation proposal added support for nested character
|
|
74
|
+
// classes if the `v` flag is present. Unfortunately, nested CCs are both context-free and incompatible
|
|
75
|
+
// with the only syntax, so we have to define 2 different regex patterns.
|
|
76
|
+
/\//.source +
|
|
77
|
+
'(?:' +
|
|
78
|
+
/(?:\[(?:[^\]\\\r\n]|\\.)*\]|\\.|[^/\\[\r\n])+\/[dgimyus]{0,7}/.source +
|
|
79
|
+
'|' +
|
|
80
|
+
// `v` flag syntax. This supports 3 levels of nested character classes.
|
|
81
|
+
/(?:\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.)*\])*\])*\]|\\.|[^/\\[\r\n])+\/[dgimyus]{0,7}v[dgimyus]{0,7}/
|
|
82
|
+
.source +
|
|
83
|
+
')' +
|
|
84
|
+
// lookahead
|
|
85
|
+
/(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/.source),
|
|
86
|
+
lookbehind: true,
|
|
87
|
+
greedy: true,
|
|
88
|
+
inside: {
|
|
89
|
+
regex_source: {
|
|
90
|
+
pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
|
|
91
|
+
lookbehind: true,
|
|
92
|
+
alias: 'lang_regex',
|
|
93
|
+
inside: syntax_styler.langs.regex, // TODO use `get_lang` after adding `regex` support
|
|
94
|
+
},
|
|
95
|
+
regex_delimiter: /^\/|\/$/,
|
|
96
|
+
regex_flags: /^[a-z]+$/,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
// Arrow function and function expression variable names
|
|
100
|
+
// This must be declared before keyword because we use "function" inside the look-forward
|
|
101
|
+
function_variable: {
|
|
102
|
+
pattern: /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)\s*=>))/,
|
|
103
|
+
alias: 'function',
|
|
104
|
+
},
|
|
105
|
+
parameter: [
|
|
106
|
+
{
|
|
107
|
+
pattern: /(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/,
|
|
108
|
+
lookbehind: true,
|
|
109
|
+
inside: grammar_js,
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i,
|
|
113
|
+
lookbehind: true,
|
|
114
|
+
inside: grammar_js,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
pattern: /(\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*=>)/,
|
|
118
|
+
lookbehind: true,
|
|
119
|
+
inside: grammar_js,
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
pattern: new RegExp(`((?:\\b|\\s|^)(?!(?:${all_js_keywords})(?![$\\w\\xA0-\\uFFFF]))(?:(?!\\s)[_$a-zA-Z\\xA0-\\uFFFF](?:(?!\\s)[$\\w\\xA0-\\uFFFF])*\\s*)\\(\\s*|\\]\\s*\\(\\s*)(?!\\s)(?:[^()\\s]|\\s+(?![\\s)])|\\([^()]*\\))+(?=\\s*\\)\\s*\\{)`),
|
|
123
|
+
lookbehind: true,
|
|
124
|
+
inside: grammar_js,
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
constant: /\b[A-Z](?:[A-Z_]|\dx?)*\b/,
|
|
128
|
+
// Heuristic: treat capitalized identifiers as class names when not already matched
|
|
129
|
+
capitalized_identifier: {
|
|
130
|
+
pattern: /\b[A-Z][\w]*\b/,
|
|
131
|
+
alias: 'class_name',
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
syntax_styler.grammar_insert_before('js', 'string', {
|
|
135
|
+
hashbang: {
|
|
136
|
+
pattern: /^#!.*/,
|
|
137
|
+
greedy: true,
|
|
138
|
+
alias: 'comment',
|
|
139
|
+
},
|
|
140
|
+
template_string: {
|
|
141
|
+
pattern: /`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,
|
|
142
|
+
greedy: true,
|
|
143
|
+
inside: {
|
|
144
|
+
template_punctuation: {
|
|
145
|
+
pattern: /^`|`$/,
|
|
146
|
+
alias: 'string',
|
|
147
|
+
},
|
|
148
|
+
interpolation: {
|
|
149
|
+
pattern: /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}/,
|
|
150
|
+
lookbehind: true,
|
|
151
|
+
inside: {
|
|
152
|
+
interpolation_punctuation: {
|
|
153
|
+
pattern: /^\$\{|\}$/,
|
|
154
|
+
alias: 'punctuation',
|
|
155
|
+
},
|
|
156
|
+
rest: grammar_js, // TODO try to fix this type
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
string: /[\s\S]+/,
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
string_property: {
|
|
163
|
+
pattern: /((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m,
|
|
164
|
+
lookbehind: true,
|
|
165
|
+
greedy: true,
|
|
166
|
+
alias: 'property',
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
syntax_styler.grammar_insert_before('js', 'operator', {
|
|
170
|
+
literal_property: {
|
|
171
|
+
pattern: /((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m,
|
|
172
|
+
lookbehind: true,
|
|
173
|
+
alias: 'property',
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
grammar_markup_add_inlined(syntax_styler, 'script', 'js');
|
|
177
|
+
// add attribute support for all DOM events (on* attributes)
|
|
178
|
+
// https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events
|
|
179
|
+
grammar_markup_add_attribute(syntax_styler, 'on\\w+', 'js');
|
|
180
|
+
};
|
|
@@ -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_json: AddSyntaxGrammar;
|
|
11
|
+
//# sourceMappingURL=grammar_json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_json.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAmB,MAAM,oBAAoB,CAAC;AAE3E;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,EAAE,gBA2B9B,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
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_json = (syntax_styler) => {
|
|
10
|
+
const grammar_json = {
|
|
11
|
+
property: {
|
|
12
|
+
pattern: /(^|[^\\])"(?:\\.|[^\\"\r\n])*"(?=\s*:)/,
|
|
13
|
+
lookbehind: true,
|
|
14
|
+
greedy: true,
|
|
15
|
+
},
|
|
16
|
+
string: {
|
|
17
|
+
pattern: /(^|[^\\])"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,
|
|
18
|
+
lookbehind: true,
|
|
19
|
+
greedy: true,
|
|
20
|
+
},
|
|
21
|
+
comment: {
|
|
22
|
+
pattern: /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
|
|
23
|
+
greedy: true,
|
|
24
|
+
},
|
|
25
|
+
number: /-?\b\d+(?:\.\d+)?(?:e[+-]?\d+)?\b/i,
|
|
26
|
+
punctuation: /[{}[\],]/,
|
|
27
|
+
operator: /:/,
|
|
28
|
+
boolean: /\b(?:false|true)\b/,
|
|
29
|
+
null: {
|
|
30
|
+
pattern: /\bnull\b/,
|
|
31
|
+
alias: 'keyword',
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
syntax_styler.add_lang('json', grammar_json);
|
|
35
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AddSyntaxGrammar } from './syntax_styler.js';
|
|
2
|
+
/**
|
|
3
|
+
* Markdown grammar extending markup.
|
|
4
|
+
* Supports: headings, fenced code blocks (3/4/5 backticks with nesting), lists, blockquotes,
|
|
5
|
+
* bold, italic, strikethrough, inline code, and links.
|
|
6
|
+
*/
|
|
7
|
+
export declare const add_grammar_markdown: AddSyntaxGrammar;
|
|
8
|
+
//# sourceMappingURL=grammar_markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar_markdown.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/grammar_markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,gBAAgB,EAIhB,MAAM,oBAAoB,CAAC;AAsF5B;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,EAAE,gBAgMlC,CAAC"}
|