@scalar/highlight 0.2.1 → 0.3.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/CHANGELOG.md +12 -0
- package/dist/compat/hast.d.ts +31 -0
- package/dist/compat/hast.d.ts.map +1 -0
- package/dist/compat/hast.js +58 -0
- 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/package.json +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @scalar/highlight
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#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.
|
|
8
|
+
|
|
9
|
+
`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.
|
|
10
|
+
|
|
11
|
+
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.
|
|
12
|
+
|
|
13
|
+
No runtime dependency is added: the hast types are type-only.
|
|
14
|
+
|
|
3
15
|
## 0.2.1
|
|
4
16
|
|
|
5
17
|
### 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,IAsCnG,CAAA"}
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
const prefix = options.prefix ?? HLJS_PREFIX;
|
|
29
|
+
const out = [];
|
|
30
|
+
let className = null;
|
|
31
|
+
let buffer = '';
|
|
32
|
+
const flush = () => {
|
|
33
|
+
if (buffer === '')
|
|
34
|
+
return;
|
|
35
|
+
if (className === null) {
|
|
36
|
+
out.push({ type: 'text', value: buffer });
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
out.push({
|
|
40
|
+
type: 'element',
|
|
41
|
+
tagName: 'span',
|
|
42
|
+
properties: { className: [prefix === HLJS_PREFIX ? className : className.replace(HLJS_PREFIX, prefix)] },
|
|
43
|
+
children: [{ type: 'text', value: buffer }],
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
buffer = '';
|
|
47
|
+
};
|
|
48
|
+
for (const token of tokenizeToArray(code, grammar)) {
|
|
49
|
+
const next = token.scope === null ? null : hljsClass(token.scope);
|
|
50
|
+
if (next !== className) {
|
|
51
|
+
flush();
|
|
52
|
+
className = next;
|
|
53
|
+
}
|
|
54
|
+
buffer += token.text;
|
|
55
|
+
}
|
|
56
|
+
flush();
|
|
57
|
+
return out;
|
|
58
|
+
};
|
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';
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"code",
|
|
17
17
|
"performance"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.
|
|
19
|
+
"version": "0.3.0",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=22"
|
|
22
22
|
},
|
|
@@ -68,6 +68,9 @@
|
|
|
68
68
|
"dist/all.js",
|
|
69
69
|
"**/*.css"
|
|
70
70
|
],
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@types/hast": "^3.0.4"
|
|
73
|
+
},
|
|
71
74
|
"devDependencies": {
|
|
72
75
|
"highlight.js": "^11.11.1",
|
|
73
76
|
"rolldown": "1.1.5",
|