@scalar/highlight 0.2.1 → 0.3.1
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/CHANGELOG.md +18 -0
- package/dist/compat/hast.d.ts +31 -0
- package/dist/compat/hast.d.ts.map +1 -0
- package/dist/compat/hast.js +60 -0
- package/dist/compat/hljs.d.ts.map +1 -1
- package/dist/compat/hljs.js +2 -1
- package/dist/compat/index.d.ts +2 -0
- package/dist/compat/index.d.ts.map +1 -1
- package/dist/compat/index.js +1 -0
- package/dist/compat/syntax-highlight.d.ts.map +1 -1
- package/dist/compat/syntax-highlight.js +8 -4
- package/dist/core/compile.d.ts.map +1 -1
- package/dist/core/compile.js +8 -4
- package/dist/core/registry.d.ts.map +1 -1
- package/dist/core/registry.js +8 -4
- package/dist/core/render.d.ts.map +1 -1
- package/dist/core/render.js +12 -6
- package/dist/core/tokenize.d.ts.map +1 -1
- package/dist/core/tokenize.js +19 -11
- package/dist/lazy.d.ts.map +1 -1
- package/dist/lazy.js +4 -2
- package/package.json +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @scalar/highlight
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#10559](https://github.com/scalar/scalar/pull/10559): Clean up Biome warnings in package code and tests. Handle rejected debounced callbacks and await streaming writes.
|
|
8
|
+
|
|
9
|
+
## 0.3.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#10033](https://github.com/scalar/scalar/pull/10033): Add `toHast` to `@scalar/highlight/compat`, which highlights into hast element content rather than an HTML string.
|
|
14
|
+
|
|
15
|
+
`syntaxHighlight` is the right tool when the destination is HTML. A rehype plugin is the case it does not serve: handing back markup there means parsing it straight back into nodes. `toHast` returns the nodes directly, and `null` for a language with no registered grammar, so a caller can tell "nothing to highlight" from "highlighted to nothing" and leave the block alone.
|
|
16
|
+
|
|
17
|
+
Adjacent ranges resolving to the same class merge into one span, matching the string renderer — our scope vocabulary is finer than highlight.js's, so `keyword.declaration` followed by `keyword` has to arrive as a single `hljs-keyword` element rather than two siblings lowlight would never have produced. The tests hold both renderers to the same output span for span across the whole sample corpus, so the two cannot drift.
|
|
18
|
+
|
|
19
|
+
No runtime dependency is added: the hast types are type-only.
|
|
20
|
+
|
|
3
21
|
## 0.2.1
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ElementContent } from 'hast';
|
|
2
|
+
export type ToHastOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Class prefix, `'hljs-'` by default.
|
|
5
|
+
*
|
|
6
|
+
* The scope table already spells its classes with that prefix because that is
|
|
7
|
+
* what `code.css` targets, so a custom prefix is a substitution rather than a
|
|
8
|
+
* concatenation.
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string | null | undefined;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Highlights `code` into hast element content, the shape a rehype plugin needs.
|
|
14
|
+
*
|
|
15
|
+
* The string renderer in `./syntax-highlight` is the right tool when the
|
|
16
|
+
* destination is HTML. This exists for the rehype half of the pipeline, where
|
|
17
|
+
* handing back markup would mean parsing it straight back into nodes.
|
|
18
|
+
*
|
|
19
|
+
* Returns `null` for a language with no registered grammar, so a caller can
|
|
20
|
+
* tell "nothing to highlight" from "highlighted to nothing" and leave the
|
|
21
|
+
* block alone — `rehype-highlight` reports that as a message rather than
|
|
22
|
+
* silently emitting unhighlighted output.
|
|
23
|
+
*
|
|
24
|
+
* Adjacent ranges resolving to the same class are merged into one span, for the
|
|
25
|
+
* same reason the string renderer merges them: our scope vocabulary is finer
|
|
26
|
+
* than highlight.js's, so `keyword.declaration` followed by `keyword` has to
|
|
27
|
+
* arrive as a single `hljs-keyword` element rather than two siblings lowlight
|
|
28
|
+
* would never have produced.
|
|
29
|
+
*/
|
|
30
|
+
export declare const toHast: (code: string, lang: string, options?: ToHastOptions) => ElementContent[] | null;
|
|
31
|
+
//# sourceMappingURL=hast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hast.d.ts","sourceRoot":"","sources":["../../src/compat/hast.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAS1C,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;CACnC,CAAA;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,MAAM,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,EAAE,UAAS,aAAkB,KAAG,cAAc,EAAE,GAAG,IA0CnG,CAAA"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { tokenizeToArray } from '../core/tokenize.js';
|
|
2
|
+
import { hljsClass } from './hljs.js';
|
|
3
|
+
import { canonicalName, grammarFor } from './languages.js';
|
|
4
|
+
/** Default class prefix, matching highlight.js and `code.css`. */
|
|
5
|
+
const HLJS_PREFIX = 'hljs-';
|
|
6
|
+
/**
|
|
7
|
+
* Highlights `code` into hast element content, the shape a rehype plugin needs.
|
|
8
|
+
*
|
|
9
|
+
* The string renderer in `./syntax-highlight` is the right tool when the
|
|
10
|
+
* destination is HTML. This exists for the rehype half of the pipeline, where
|
|
11
|
+
* handing back markup would mean parsing it straight back into nodes.
|
|
12
|
+
*
|
|
13
|
+
* Returns `null` for a language with no registered grammar, so a caller can
|
|
14
|
+
* tell "nothing to highlight" from "highlighted to nothing" and leave the
|
|
15
|
+
* block alone — `rehype-highlight` reports that as a message rather than
|
|
16
|
+
* silently emitting unhighlighted output.
|
|
17
|
+
*
|
|
18
|
+
* Adjacent ranges resolving to the same class are merged into one span, for the
|
|
19
|
+
* same reason the string renderer merges them: our scope vocabulary is finer
|
|
20
|
+
* than highlight.js's, so `keyword.declaration` followed by `keyword` has to
|
|
21
|
+
* arrive as a single `hljs-keyword` element rather than two siblings lowlight
|
|
22
|
+
* would never have produced.
|
|
23
|
+
*/
|
|
24
|
+
export const toHast = (code, lang, options = {}) => {
|
|
25
|
+
const grammar = grammarFor(canonicalName(lang));
|
|
26
|
+
if (grammar === undefined) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const prefix = options.prefix ?? HLJS_PREFIX;
|
|
30
|
+
const out = [];
|
|
31
|
+
let className = null;
|
|
32
|
+
let buffer = '';
|
|
33
|
+
const flush = () => {
|
|
34
|
+
if (buffer === '') {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (className === null) {
|
|
38
|
+
out.push({ type: 'text', value: buffer });
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
out.push({
|
|
42
|
+
type: 'element',
|
|
43
|
+
tagName: 'span',
|
|
44
|
+
properties: { className: [prefix === HLJS_PREFIX ? className : className.replace(HLJS_PREFIX, prefix)] },
|
|
45
|
+
children: [{ type: 'text', value: buffer }],
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
buffer = '';
|
|
49
|
+
};
|
|
50
|
+
for (const token of tokenizeToArray(code, grammar)) {
|
|
51
|
+
const next = token.scope === null ? null : hljsClass(token.scope);
|
|
52
|
+
if (next !== className) {
|
|
53
|
+
flush();
|
|
54
|
+
className = next;
|
|
55
|
+
}
|
|
56
|
+
buffer += token.text;
|
|
57
|
+
}
|
|
58
|
+
flush();
|
|
59
|
+
return out;
|
|
60
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hljs.d.ts","sourceRoot":"","sources":["../../src/compat/hljs.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,QAAA,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAmE/C,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,MAAM,KAAG,MAAM,GAAG,
|
|
1
|
+
{"version":3,"file":"hljs.d.ts","sourceRoot":"","sources":["../../src/compat/hljs.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,QAAA,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAmE/C,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,MAAM,KAAG,MAAM,GAAG,IAYlD,CAAA;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
package/dist/compat/hljs.js
CHANGED
|
@@ -87,8 +87,9 @@ export const hljsClass = (scope) => {
|
|
|
87
87
|
// falling through to the next, less specific scope. An owned value is
|
|
88
88
|
// `string | null` (null means bare text); `?? null` only satisfies the
|
|
89
89
|
// index-access type, it never fires for a key we have confirmed we own.
|
|
90
|
-
if (Object.hasOwn(HLJS_CLASSES, name))
|
|
90
|
+
if (Object.hasOwn(HLJS_CLASSES, name)) {
|
|
91
91
|
return HLJS_CLASSES[name] ?? null;
|
|
92
|
+
}
|
|
92
93
|
}
|
|
93
94
|
return null;
|
|
94
95
|
};
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -16,6 +16,8 @@
|
|
|
16
16
|
* `rehypeAlert`, `rehypeHighlight`. Those are a remark/rehype pipeline that has
|
|
17
17
|
* nothing to do with tokenizing code, and they stay where they are.
|
|
18
18
|
*/
|
|
19
|
+
export type { ToHastOptions } from './hast.js';
|
|
20
|
+
export { toHast } from './hast.js';
|
|
19
21
|
export { HLJS_CLASSES, hljsClass } from './hljs.js';
|
|
20
22
|
export type { StandardLanguageKey } from './languages.js';
|
|
21
23
|
export { STANDARD_LANGUAGE_NAMES, canonicalName, grammarFor, lowlightLanguageMappings, standardLanguages, unsupportedLanguages, } from './languages.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compat/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAChD,YAAY,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,EACL,uBAAuB,EACvB,aAAa,EACb,UAAU,EACV,wBAAwB,EACxB,iBAAiB,EACjB,oBAAoB,GACrB,MAAM,aAAa,CAAA;AACpB,YAAY,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compat/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,YAAY,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAChD,YAAY,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,EACL,uBAAuB,EACvB,aAAa,EACb,UAAU,EACV,wBAAwB,EACxB,iBAAiB,EACjB,oBAAoB,GACrB,MAAM,aAAa,CAAA;AACpB,YAAY,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA"}
|
package/dist/compat/index.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
* `rehypeAlert`, `rehypeHighlight`. Those are a remark/rehype pipeline that has
|
|
17
17
|
* nothing to do with tokenizing code, and they stay where they are.
|
|
18
18
|
*/
|
|
19
|
+
export { toHast } from './hast.js';
|
|
19
20
|
export { HLJS_CLASSES, hljsClass } from './hljs.js';
|
|
20
21
|
export { STANDARD_LANGUAGE_NAMES, canonicalName, grammarFor, lowlightLanguageMappings, standardLanguages, unsupportedLanguages, } from './languages.js';
|
|
21
22
|
export { syntaxHighlight } from './syntax-highlight.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syntax-highlight.d.ts","sourceRoot":"","sources":["../../src/compat/syntax-highlight.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,sBAAsB,GAAG;IACnC,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAA;IACZ;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,4EAA4E;IAC5E,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,2DAA2D;IAC3D,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACpC,CAAA;
|
|
1
|
+
{"version":3,"file":"syntax-highlight.d.ts","sourceRoot":"","sources":["../../src/compat/syntax-highlight.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,sBAAsB,GAAG;IACnC,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAA;IACZ;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,4EAA4E;IAC5E,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,2DAA2D;IAC3D,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACpC,CAAA;AAuID;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,eAAe,GAAI,YAAY,MAAM,EAAE,SAAS,sBAAsB,KAAG,MAYrF,CAAA"}
|
|
@@ -58,8 +58,9 @@ const render = (code, grammar, lineMode) => {
|
|
|
58
58
|
}
|
|
59
59
|
end = stop;
|
|
60
60
|
}
|
|
61
|
-
if (stop === e)
|
|
61
|
+
if (stop === e) {
|
|
62
62
|
break;
|
|
63
|
+
}
|
|
63
64
|
// Landed on a newline: close the line and restart after it. The newline
|
|
64
65
|
// itself is written by the wrapper, not carried as token text.
|
|
65
66
|
endLine();
|
|
@@ -78,11 +79,13 @@ const render = (code, grammar, lineMode) => {
|
|
|
78
79
|
emit(null, 0, code.length);
|
|
79
80
|
}
|
|
80
81
|
endLine();
|
|
81
|
-
if (!lineMode)
|
|
82
|
+
if (!lineMode) {
|
|
82
83
|
return { html: lines.join(''), count: lines.length };
|
|
84
|
+
}
|
|
83
85
|
let html = '';
|
|
84
|
-
for (const content of lines)
|
|
86
|
+
for (const content of lines) {
|
|
85
87
|
html += `<span class="line">${content}\n</span>`;
|
|
88
|
+
}
|
|
86
89
|
return { html, count: lines.length };
|
|
87
90
|
};
|
|
88
91
|
/**
|
|
@@ -100,8 +103,9 @@ const MIN_CREDENTIAL_LENGTH = 3;
|
|
|
100
103
|
*/
|
|
101
104
|
const applyCredentialMask = (html, credentials) => {
|
|
102
105
|
const list = (typeof credentials === 'string' ? [credentials] : (credentials ?? [])).filter((credential) => credential.length >= MIN_CREDENTIAL_LENGTH);
|
|
103
|
-
if (!list.length)
|
|
106
|
+
if (!list.length) {
|
|
104
107
|
return html;
|
|
108
|
+
}
|
|
105
109
|
return list.reduce((acc, credential) => acc.split(credential).join(`<span class="credential"><span class="credential-value">${credential}</span></span>`), html);
|
|
106
110
|
};
|
|
107
111
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/core/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAA+B,OAAO,EAAmB,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/core/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAA+B,OAAO,EAAmB,MAAM,SAAS,CAAA;AA4IrG,eAAO,MAAM,OAAO,GAAI,SAAS,OAAO,KAAG,eA8B1C,CAAA"}
|
package/dist/core/compile.js
CHANGED
|
@@ -21,24 +21,27 @@ const countGroups = (src) => {
|
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
23
|
if (inClass) {
|
|
24
|
-
if (c === 93 /* ] */)
|
|
24
|
+
if (c === 93 /* ] */) {
|
|
25
25
|
inClass = false;
|
|
26
|
+
}
|
|
26
27
|
continue;
|
|
27
28
|
}
|
|
28
29
|
if (c === 91 /* [ */) {
|
|
29
30
|
inClass = true;
|
|
30
31
|
continue;
|
|
31
32
|
}
|
|
32
|
-
if (c !== 40 /* ( */)
|
|
33
|
+
if (c !== 40 /* ( */) {
|
|
33
34
|
continue;
|
|
35
|
+
}
|
|
34
36
|
if (src.charCodeAt(i + 1) !== 63 /* ? */) {
|
|
35
37
|
count++;
|
|
36
38
|
}
|
|
37
39
|
else if (src.charCodeAt(i + 2) === 60 /* < */) {
|
|
38
40
|
// (?<name> captures; (?<= and (?<! are lookbehind.
|
|
39
41
|
const after = src.charCodeAt(i + 3);
|
|
40
|
-
if (after !== 61 /* = */ && after !== 33 /* ! */)
|
|
42
|
+
if (after !== 61 /* = */ && after !== 33 /* ! */) {
|
|
41
43
|
count++;
|
|
44
|
+
}
|
|
42
45
|
}
|
|
43
46
|
}
|
|
44
47
|
return count;
|
|
@@ -56,8 +59,9 @@ const flatten = (grammarName, states, name, seen) => {
|
|
|
56
59
|
// `constructor` would otherwise resolve off Object.prototype and sail past
|
|
57
60
|
// this check, failing later with something far less legible.
|
|
58
61
|
const state = Object.hasOwn(states, name) ? states[name] : undefined;
|
|
59
|
-
if (!state)
|
|
62
|
+
if (!state) {
|
|
60
63
|
throw new Error(`[${grammarName}] include of unknown state "${name}"`);
|
|
64
|
+
}
|
|
61
65
|
const out = [];
|
|
62
66
|
for (const entry of state.rules) {
|
|
63
67
|
if ('include' in entry) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/core/registry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAYvD,eAAO,MAAM,gBAAgB,GAAI,GAAG,UAAU,OAAO,EAAE,KAAG,
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/core/registry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAYvD,eAAO,MAAM,gBAAgB,GAAI,GAAG,UAAU,OAAO,EAAE,KAAG,IAyBzD,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,OAE3C,CAAA;AAED,gEAAgE;AAChE,eAAO,MAAM,aAAa,QAAO,MAAM,EAEtC,CAAA;AAMD,qEAAqE;AACrE,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,SAkB5D,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,GAAG,OAAO,GAAG,eAAe,KAAG,eAgBzE,CAAA"}
|
package/dist/core/registry.js
CHANGED
|
@@ -19,8 +19,9 @@ export const registerLanguage = (...grammars) => {
|
|
|
19
19
|
if (previous) {
|
|
20
20
|
for (const key of [previous.name, ...(previous.aliases ?? [])]) {
|
|
21
21
|
const lower = key.toLowerCase();
|
|
22
|
-
if (sources.get(lower) === previous)
|
|
22
|
+
if (sources.get(lower) === previous) {
|
|
23
23
|
sources.delete(lower);
|
|
24
|
+
}
|
|
24
25
|
cache.delete(lower);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
@@ -45,11 +46,13 @@ const isCompiled = (value) => {
|
|
|
45
46
|
export const getLanguage = (name) => {
|
|
46
47
|
const key = name.toLowerCase();
|
|
47
48
|
const hit = cache.get(key);
|
|
48
|
-
if (hit)
|
|
49
|
+
if (hit) {
|
|
49
50
|
return hit;
|
|
51
|
+
}
|
|
50
52
|
const grammar = sources.get(key);
|
|
51
|
-
if (!grammar)
|
|
53
|
+
if (!grammar) {
|
|
52
54
|
return undefined;
|
|
55
|
+
}
|
|
53
56
|
const built = compile(grammar);
|
|
54
57
|
// Cache under every alias so repeat lookups skip the alias hop.
|
|
55
58
|
for (const alias of [grammar.name, ...(grammar.aliases ?? [])]) {
|
|
@@ -67,8 +70,9 @@ export const resolveGrammar = (lang) => {
|
|
|
67
70
|
return isCompiled(lang) ? lang : compile(lang);
|
|
68
71
|
}
|
|
69
72
|
const found = getLanguage(lang);
|
|
70
|
-
if (found)
|
|
73
|
+
if (found) {
|
|
71
74
|
return found;
|
|
75
|
+
}
|
|
72
76
|
const known = listLanguages();
|
|
73
77
|
throw new Error(`Language "${lang}" is not registered. ` +
|
|
74
78
|
(known.length
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../../src/core/render.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAE9C,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,qDAAqD;IACrD,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACnC,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,SAAS,CAAA;AAMpC;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,GAAG,MAAM,KAAG,
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../../src/core/render.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAE9C,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,qDAAqD;IACrD,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACnC,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,SAAS,CAAA;AAMpC;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,GAAG,MAAM,KAAG,MAatC,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,GAAG,MAAM,KAAG,MAE3C,CAAA;AA4BD;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,UAAS,aAAkB,KAAG,MAgC/F,CAAA;AA0FD,MAAM,MAAM,YAAY,GAAG,aAAa,GAAG;IACzC,oDAAoD;IACpD,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,oCAAoC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,uEAAuE;IACvE,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,UAAS,YAAiB,KAAG,MAkCnG,CAAA"}
|
package/dist/core/render.js
CHANGED
|
@@ -25,9 +25,11 @@ export const escapeHtml = (s) => {
|
|
|
25
25
|
let last = 0;
|
|
26
26
|
for (let i = 0; i < s.length; i++) {
|
|
27
27
|
const c = s.charCodeAt(i);
|
|
28
|
-
if (c !== AMP && c !== LT && c !== GT)
|
|
28
|
+
if (c !== AMP && c !== LT && c !== GT) {
|
|
29
29
|
continue;
|
|
30
|
-
|
|
30
|
+
}
|
|
31
|
+
const angleEntity = c === LT ? '<' : '>';
|
|
32
|
+
out += s.slice(last, i) + (c === AMP ? '&' : angleEntity);
|
|
31
33
|
last = i + 1;
|
|
32
34
|
}
|
|
33
35
|
return last === 0 ? s : out + s.slice(last);
|
|
@@ -75,8 +77,9 @@ const openTag = (tags, prefix, scope) => {
|
|
|
75
77
|
*/
|
|
76
78
|
export const highlight = (code, grammar, options = {}) => {
|
|
77
79
|
const prefix = escapeAttribute(options.classPrefix ?? DEFAULT_PREFIX);
|
|
78
|
-
if (options.lines)
|
|
80
|
+
if (options.lines) {
|
|
79
81
|
return renderLines(code, grammar, prefix, options.highlightLines).html;
|
|
82
|
+
}
|
|
80
83
|
const tags = tagsFor(prefix);
|
|
81
84
|
let out = '';
|
|
82
85
|
// Pending run of same-scoped ranges, flushed when the scope changes.
|
|
@@ -128,8 +131,9 @@ const renderLines = (code, grammar, prefix, highlightLines) => {
|
|
|
128
131
|
let from = 0;
|
|
129
132
|
let to = 0;
|
|
130
133
|
const flush = () => {
|
|
131
|
-
if (to > from)
|
|
134
|
+
if (to > from) {
|
|
132
135
|
line += span(code, scope, from, to, prefix);
|
|
136
|
+
}
|
|
133
137
|
from = to;
|
|
134
138
|
};
|
|
135
139
|
const endLine = () => {
|
|
@@ -150,8 +154,9 @@ const renderLines = (code, grammar, prefix, highlightLines) => {
|
|
|
150
154
|
}
|
|
151
155
|
to = stop;
|
|
152
156
|
}
|
|
153
|
-
if (stop === end)
|
|
157
|
+
if (stop === end) {
|
|
154
158
|
break;
|
|
159
|
+
}
|
|
155
160
|
endLine();
|
|
156
161
|
at = stop + 1;
|
|
157
162
|
scope = null;
|
|
@@ -163,8 +168,9 @@ const renderLines = (code, grammar, prefix, highlightLines) => {
|
|
|
163
168
|
// A trailing newline in the source closes the last line rather than starting
|
|
164
169
|
// an empty one.
|
|
165
170
|
const endsWithNewline = code.endsWith('\n');
|
|
166
|
-
if (endsWithNewline && lines[lines.length - 1] === '')
|
|
171
|
+
if (endsWithNewline && lines[lines.length - 1] === '') {
|
|
167
172
|
lines.pop();
|
|
173
|
+
}
|
|
168
174
|
let out = '';
|
|
169
175
|
for (let i = 0; i < lines.length; i++) {
|
|
170
176
|
const hl = marked?.has(i + 1) ? ' data-hl' : '';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenize.d.ts","sourceRoot":"","sources":["../../src/core/tokenize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAA+B,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAExF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,MAAM,IAAI,KAAG,
|
|
1
|
+
{"version":3,"file":"tokenize.d.ts","sourceRoot":"","sources":["../../src/core/tokenize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAA+B,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAExF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,MAAM,IAAI,KAAG,IAyE7E,CAAA;AAuDD;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,KAAG,KAAK,EAM7E,CAAA"}
|
package/dist/core/tokenize.js
CHANGED
|
@@ -17,8 +17,9 @@ export const tokenize = (code, grammar, emit) => {
|
|
|
17
17
|
const re = top.re;
|
|
18
18
|
re.lastIndex = pos;
|
|
19
19
|
const m = re.exec(code);
|
|
20
|
-
if (m === null)
|
|
20
|
+
if (m === null) {
|
|
21
21
|
break;
|
|
22
|
+
}
|
|
22
23
|
const start = m.index;
|
|
23
24
|
if (start > pos) {
|
|
24
25
|
emit(top.default, pos, start);
|
|
@@ -26,15 +27,15 @@ export const tokenize = (code, grammar, emit) => {
|
|
|
26
27
|
}
|
|
27
28
|
const rules = top.rules;
|
|
28
29
|
let rule;
|
|
29
|
-
for (
|
|
30
|
-
const r = rules[i];
|
|
30
|
+
for (const r of rules) {
|
|
31
31
|
if (m[r.group] !== undefined) {
|
|
32
32
|
rule = r;
|
|
33
33
|
break;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
if (rule === undefined)
|
|
36
|
+
if (rule === undefined) {
|
|
37
37
|
break; // unreachable: some alternative matched
|
|
38
|
+
}
|
|
38
39
|
const end = start + m[0].length;
|
|
39
40
|
if (end > start) {
|
|
40
41
|
if (rule.captures !== null) {
|
|
@@ -69,8 +70,9 @@ export const tokenize = (code, grammar, emit) => {
|
|
|
69
70
|
spin = 0;
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
|
-
if (pos < len)
|
|
73
|
+
if (pos < len) {
|
|
73
74
|
emit(top.default, pos, len);
|
|
75
|
+
}
|
|
74
76
|
};
|
|
75
77
|
/**
|
|
76
78
|
* Re-runs the winning rule on its own to find where its capture groups sit.
|
|
@@ -90,8 +92,9 @@ const emitCaptures = (code, start, rule, state, emit) => {
|
|
|
90
92
|
const own = rule.own;
|
|
91
93
|
own.lastIndex = start;
|
|
92
94
|
const m = own.exec(code);
|
|
93
|
-
if (m === null)
|
|
95
|
+
if (m === null) {
|
|
94
96
|
return; // unreachable: the merged regex matched this rule here
|
|
97
|
+
}
|
|
95
98
|
const indices = m.indices;
|
|
96
99
|
const whole = indices[0];
|
|
97
100
|
const captures = rule.captures;
|
|
@@ -100,20 +103,25 @@ const emitCaptures = (code, start, rule, state, emit) => {
|
|
|
100
103
|
const limit = whole[1];
|
|
101
104
|
for (let i = 0; i < captures.length; i++) {
|
|
102
105
|
const span = indices[i + 1];
|
|
103
|
-
if (span === undefined)
|
|
106
|
+
if (span === undefined) {
|
|
104
107
|
continue; // group did not participate
|
|
108
|
+
}
|
|
105
109
|
const to = span[1] > limit ? limit : span[1]; // a lookahead group can overrun
|
|
106
|
-
if (to <= cursor)
|
|
110
|
+
if (to <= cursor) {
|
|
107
111
|
continue; // already covered by an enclosing group
|
|
112
|
+
}
|
|
108
113
|
const from = span[0] > cursor ? span[0] : cursor;
|
|
109
|
-
if (from > cursor)
|
|
114
|
+
if (from > cursor) {
|
|
110
115
|
emit(fallback, cursor, from);
|
|
111
|
-
|
|
116
|
+
}
|
|
117
|
+
if (to > from) {
|
|
112
118
|
emit(captures[i] ?? fallback, from, to); // a group can match empty
|
|
119
|
+
}
|
|
113
120
|
cursor = to;
|
|
114
121
|
}
|
|
115
|
-
if (limit > cursor)
|
|
122
|
+
if (limit > cursor) {
|
|
116
123
|
emit(fallback, cursor, limit);
|
|
124
|
+
}
|
|
117
125
|
};
|
|
118
126
|
/**
|
|
119
127
|
* Collects tokens into an array.
|
package/dist/lazy.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,QAAA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyCkD,CAAA;AAE/D,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,OAAO,CAAA;AAqGlD,eAAO,MAAM,gBAAgB,EAA2B,eAAe,EAAE,CAAA;AAEzE,sEAAsE;AACtE,eAAO,MAAM,mBAAmB,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,
|
|
1
|
+
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,QAAA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyCkD,CAAA;AAE/D,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,OAAO,CAAA;AAqGlD,eAAO,MAAM,gBAAgB,EAA2B,eAAe,EAAE,CAAA;AAEzE,sEAAsE;AACtE,eAAO,MAAM,mBAAmB,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,SASpE,CAAA;AAID;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,OAAO,CAAC,eAAe,CA4BlE,CAAA;AAED,uCAAuC;AACvC,eAAO,MAAM,aAAa,GAAI,GAAG,OAAO,MAAM,EAAE,KAAG,OAAO,CAAC,eAAe,EAAE,CAE3E,CAAA"}
|
package/dist/lazy.js
CHANGED
|
@@ -157,8 +157,9 @@ export const resolveLanguageName = (name) => {
|
|
|
157
157
|
// `hasOwn` rather than `in`: `in` walks the prototype chain, so a caller
|
|
158
158
|
// asking for `constructor` would get past this check and then try to call
|
|
159
159
|
// `Object.prototype.constructor` as a loader.
|
|
160
|
-
if (Object.hasOwn(loaders, key))
|
|
160
|
+
if (Object.hasOwn(loaders, key)) {
|
|
161
161
|
return key;
|
|
162
|
+
}
|
|
162
163
|
return Object.hasOwn(aliases, key) ? aliases[key] : undefined;
|
|
163
164
|
};
|
|
164
165
|
const pending = new Map();
|
|
@@ -174,8 +175,9 @@ export const loadLanguage = (name) => {
|
|
|
174
175
|
return Promise.reject(new Error(`Unknown language "${name}". Available: ${bundledLanguages.join(', ')}.`));
|
|
175
176
|
}
|
|
176
177
|
const registered = getLanguage(resolved);
|
|
177
|
-
if (registered)
|
|
178
|
+
if (registered) {
|
|
178
179
|
return Promise.resolve(registered);
|
|
180
|
+
}
|
|
179
181
|
let inFlight = pending.get(resolved);
|
|
180
182
|
if (!inFlight) {
|
|
181
183
|
inFlight = loaders[resolved]()
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"code",
|
|
17
17
|
"performance"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.
|
|
19
|
+
"version": "0.3.1",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=22"
|
|
22
22
|
},
|
|
@@ -68,11 +68,14 @@
|
|
|
68
68
|
"dist/all.js",
|
|
69
69
|
"**/*.css"
|
|
70
70
|
],
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@types/hast": "^3.0.4"
|
|
73
|
+
},
|
|
71
74
|
"devDependencies": {
|
|
75
|
+
"@scalar/code-highlight": "0.4.9",
|
|
72
76
|
"highlight.js": "^11.11.1",
|
|
73
77
|
"rolldown": "1.1.5",
|
|
74
|
-
"vitest": "4.1.10"
|
|
75
|
-
"@scalar/code-highlight": "0.4.5"
|
|
78
|
+
"vitest": "4.1.10"
|
|
76
79
|
},
|
|
77
80
|
"scripts": {
|
|
78
81
|
"bench": "tsx bench/bench.ts",
|