@scalar/highlight 0.2.0 → 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 CHANGED
@@ -1,5 +1,23 @@
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
+
15
+ ## 0.2.1
16
+
17
+ ### Patch Changes
18
+
19
+ - [#9941](https://github.com/scalar/scalar/pull/9941): Republish every package through npm trusted publishing. No functional changes.
20
+
3
21
  ## 0.2.0
4
22
 
5
23
  ### Minor Changes
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Scalar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -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
+ };
@@ -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"}
@@ -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,19 +16,10 @@
16
16
  "code",
17
17
  "performance"
18
18
  ],
19
- "version": "0.2.0",
19
+ "version": "0.3.0",
20
20
  "engines": {
21
21
  "node": ">=22"
22
22
  },
23
- "scripts": {
24
- "bench": "tsx bench/bench.ts",
25
- "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json && shx cp src/style.css dist/style.css",
26
- "compat:report": "tsx bench/compat-report.ts",
27
- "size": "tsx bench/size.ts",
28
- "test": "vitest --run",
29
- "test:watch": "vitest",
30
- "types:check": "tsgo --noEmit"
31
- },
32
23
  "type": "module",
33
24
  "main": "dist/index.js",
34
25
  "module": "dist/index.js",
@@ -77,10 +68,22 @@
77
68
  "dist/all.js",
78
69
  "**/*.css"
79
70
  ],
71
+ "dependencies": {
72
+ "@types/hast": "^3.0.4"
73
+ },
80
74
  "devDependencies": {
81
- "@scalar/code-highlight": "workspace:*",
82
- "highlight.js": "catalog:*",
83
- "rolldown": "catalog:*",
84
- "vitest": "catalog:*"
75
+ "highlight.js": "^11.11.1",
76
+ "rolldown": "1.1.5",
77
+ "vitest": "4.1.10",
78
+ "@scalar/code-highlight": "0.4.5"
79
+ },
80
+ "scripts": {
81
+ "bench": "tsx bench/bench.ts",
82
+ "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json && shx cp src/style.css dist/style.css",
83
+ "compat:report": "tsx bench/compat-report.ts",
84
+ "size": "tsx bench/size.ts",
85
+ "test": "vitest --run",
86
+ "test:watch": "vitest",
87
+ "types:check": "tsgo --noEmit"
85
88
  }
86
- }
89
+ }