@shikijs/rehype 4.0.1 → 4.1.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.
@@ -0,0 +1,116 @@
1
+ import { isSpecialLang } from "shiki/core";
2
+ import { visit } from "unist-util-visit";
3
+ import { toString } from "hast-util-to-string";
4
+ //#region src/handlers.ts
5
+ const RE_TAILING_CURLY_COLON = /(.+)\{:([\w-]+)\}$/;
6
+ const InlineCodeHandlers = { "tailing-curly-colon": (_tree, node) => {
7
+ const raw = toString(node);
8
+ const match = raw.match(RE_TAILING_CURLY_COLON);
9
+ if (!match) return;
10
+ return {
11
+ type: "inline",
12
+ code: match[1] ?? raw,
13
+ lang: match.at(2)
14
+ };
15
+ } };
16
+ const languagePrefix$1 = "language-";
17
+ const PreHandler = (_tree, node) => {
18
+ const head = node.children[0];
19
+ if (!head || head.type !== "element" || head.tagName !== "code" || !head.properties) return;
20
+ const classes = head.properties.className;
21
+ const languageClass = Array.isArray(classes) ? classes.find((d) => typeof d === "string" && d.startsWith(languagePrefix$1)) : void 0;
22
+ return {
23
+ type: "pre",
24
+ lang: typeof languageClass === "string" ? languageClass.slice(9) : void 0,
25
+ code: toString(head),
26
+ meta: head.data?.meta ?? head.properties.metastring?.toString() ?? ""
27
+ };
28
+ };
29
+ //#endregion
30
+ //#region src/core.ts
31
+ const languagePrefix = "language-";
32
+ function rehypeShikiFromHighlighter(highlighter, options) {
33
+ const { addLanguageClass = false, parseMetaString, cache, defaultLanguage, fallbackLanguage, onError, stripEndNewline = true, inline = false, lazy = false, ...rest } = options;
34
+ function highlight(lang, code, metaString = "", meta = {}) {
35
+ const cacheKey = `${lang}:${metaString}:${code}`;
36
+ const cachedValue = cache?.get(cacheKey);
37
+ if (cachedValue) return cachedValue;
38
+ const codeOptions = {
39
+ ...rest,
40
+ lang,
41
+ meta: {
42
+ ...rest.meta,
43
+ ...meta,
44
+ __raw: metaString
45
+ }
46
+ };
47
+ if (addLanguageClass) codeOptions.transformers = [...codeOptions.transformers ?? [], {
48
+ name: "rehype-shiki:code-language-class",
49
+ code(node) {
50
+ this.addClassToHast(node, `${languagePrefix}${lang}`);
51
+ return node;
52
+ }
53
+ }];
54
+ if (stripEndNewline && code.endsWith("\n")) code = code.slice(0, -1);
55
+ try {
56
+ const fragment = highlighter.codeToHast(code, codeOptions);
57
+ cache?.set(cacheKey, fragment);
58
+ return fragment;
59
+ } catch (error) {
60
+ if (onError) onError(error);
61
+ else throw error;
62
+ }
63
+ }
64
+ return (tree) => {
65
+ const queue = [];
66
+ visit(tree, "element", (node, index, parent) => {
67
+ let handler;
68
+ if (!parent || index == null) return;
69
+ if (node.tagName === "pre") handler = PreHandler;
70
+ else if (node.tagName === "code" && inline) handler = InlineCodeHandlers[inline];
71
+ else return;
72
+ const parsed = handler(tree, node);
73
+ if (!parsed) return;
74
+ let lang;
75
+ let lazyLoad = false;
76
+ if (!parsed.lang) lang = defaultLanguage;
77
+ else if (highlighter.getLoadedLanguages().includes(parsed.lang) || isSpecialLang(parsed.lang)) lang = parsed.lang;
78
+ else if (lazy) {
79
+ lazyLoad = true;
80
+ lang = parsed.lang;
81
+ } else if (fallbackLanguage) lang = fallbackLanguage;
82
+ if (!lang) return;
83
+ const meta = parsed.meta ? parseMetaString?.(parsed.meta, node, tree) : void 0;
84
+ const processNode = (targetLang) => {
85
+ const fragment = highlight(targetLang, parsed.code, parsed.meta, meta ?? {});
86
+ if (!fragment) return;
87
+ if (parsed.type === "inline") {
88
+ const head = fragment.children[0];
89
+ if (head.type === "element" && head.tagName === "pre") head.tagName = "span";
90
+ }
91
+ parent.children[index] = fragment;
92
+ };
93
+ if (lazyLoad) try {
94
+ queue.push(highlighter.loadLanguage(lang).then(() => processNode(lang)).catch((error) => {
95
+ if (fallbackLanguage) processNode(fallbackLanguage);
96
+ else if (onError) onError(error);
97
+ else throw error;
98
+ }));
99
+ } catch (error) {
100
+ if (fallbackLanguage) return processNode(fallbackLanguage);
101
+ else if (onError) onError(error);
102
+ else throw error;
103
+ }
104
+ else processNode(lang);
105
+ return "skip";
106
+ });
107
+ if (queue.length > 0) {
108
+ async function run() {
109
+ await Promise.all(queue);
110
+ }
111
+ return run();
112
+ }
113
+ };
114
+ }
115
+ //#endregion
116
+ export { rehypeShikiFromHighlighter as t };
package/dist/core.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as RehypeShikiCoreOptions, r as RehypeShikiExtraOptions, t as MapLike } from "./types-Wi2EaRpk.mjs";
1
+ import { n as RehypeShikiCoreOptions, r as RehypeShikiExtraOptions, t as MapLike } from "./types-BfH8cGz-.mjs";
2
2
  import { HighlighterGeneric } from "@shikijs/types";
3
3
  import { Root } from "hast";
4
4
  import { Transformer } from "unified";
package/dist/core.mjs CHANGED
@@ -1,92 +1,2 @@
1
- import { n as PreHandler, t as InlineCodeHandlers } from "./handlers-DyniaZhL.mjs";
2
- import { isSpecialLang } from "shiki/core";
3
- import { visit } from "unist-util-visit";
4
-
5
- //#region src/core.ts
6
- const languagePrefix = "language-";
7
- function rehypeShikiFromHighlighter(highlighter, options) {
8
- const { addLanguageClass = false, parseMetaString, cache, defaultLanguage, fallbackLanguage, onError, stripEndNewline = true, inline = false, lazy = false, ...rest } = options;
9
- function highlight(lang, code, metaString = "", meta = {}) {
10
- const cacheKey = `${lang}:${metaString}:${code}`;
11
- const cachedValue = cache?.get(cacheKey);
12
- if (cachedValue) return cachedValue;
13
- const codeOptions = {
14
- ...rest,
15
- lang,
16
- meta: {
17
- ...rest.meta,
18
- ...meta,
19
- __raw: metaString
20
- }
21
- };
22
- if (addLanguageClass) codeOptions.transformers = [...codeOptions.transformers ?? [], {
23
- name: "rehype-shiki:code-language-class",
24
- code(node) {
25
- this.addClassToHast(node, `${languagePrefix}${lang}`);
26
- return node;
27
- }
28
- }];
29
- if (stripEndNewline && code.endsWith("\n")) code = code.slice(0, -1);
30
- try {
31
- const fragment = highlighter.codeToHast(code, codeOptions);
32
- cache?.set(cacheKey, fragment);
33
- return fragment;
34
- } catch (error) {
35
- if (onError) onError(error);
36
- else throw error;
37
- }
38
- }
39
- return (tree) => {
40
- const queue = [];
41
- visit(tree, "element", (node, index, parent) => {
42
- let handler;
43
- if (!parent || index == null) return;
44
- if (node.tagName === "pre") handler = PreHandler;
45
- else if (node.tagName === "code" && inline) handler = InlineCodeHandlers[inline];
46
- else return;
47
- const parsed = handler(tree, node);
48
- if (!parsed) return;
49
- let lang;
50
- let lazyLoad = false;
51
- if (!parsed.lang) lang = defaultLanguage;
52
- else if (highlighter.getLoadedLanguages().includes(parsed.lang) || isSpecialLang(parsed.lang)) lang = parsed.lang;
53
- else if (lazy) {
54
- lazyLoad = true;
55
- lang = parsed.lang;
56
- } else if (fallbackLanguage) lang = fallbackLanguage;
57
- if (!lang) return;
58
- const meta = parsed.meta ? parseMetaString?.(parsed.meta, node, tree) : void 0;
59
- const processNode = (targetLang) => {
60
- const fragment = highlight(targetLang, parsed.code, parsed.meta, meta ?? {});
61
- if (!fragment) return;
62
- if (parsed.type === "inline") {
63
- const head = fragment.children[0];
64
- if (head.type === "element" && head.tagName === "pre") head.tagName = "span";
65
- }
66
- parent.children[index] = fragment;
67
- };
68
- if (lazyLoad) try {
69
- queue.push(highlighter.loadLanguage(lang).then(() => processNode(lang)).catch((error) => {
70
- if (fallbackLanguage) processNode(fallbackLanguage);
71
- else if (onError) onError(error);
72
- else throw error;
73
- }));
74
- } catch (error) {
75
- if (fallbackLanguage) return processNode(fallbackLanguage);
76
- else if (onError) onError(error);
77
- else throw error;
78
- }
79
- else processNode(lang);
80
- return "skip";
81
- });
82
- if (queue.length > 0) {
83
- async function run() {
84
- await Promise.all(queue);
85
- }
86
- return run();
87
- }
88
- };
89
- }
90
-
91
- //#endregion
92
- export { rehypeShikiFromHighlighter as default };
1
+ import { t as rehypeShikiFromHighlighter } from "./core-BUhjvszS.mjs";
2
+ export { rehypeShikiFromHighlighter as default };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as RehypeShikiCoreOptions } from "./types-Wi2EaRpk.mjs";
1
+ import { n as RehypeShikiCoreOptions } from "./types-BfH8cGz-.mjs";
2
2
  import { BuiltinLanguage } from "shiki";
3
3
  import { LanguageInput } from "@shikijs/types";
4
4
  import { Root } from "hast";
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
- import rehypeShikiFromHighlighter from "./core.mjs";
1
+ import { t as rehypeShikiFromHighlighter } from "./core-BUhjvszS.mjs";
2
2
  import { bundledLanguages, getSingletonHighlighter } from "shiki";
3
-
4
3
  //#region src/index.ts
5
4
  const rehypeShiki = function(options = {}) {
6
5
  const themeNames = ("themes" in options ? Object.values(options.themes) : [options.theme]).filter(Boolean);
@@ -16,6 +15,5 @@ const rehypeShiki = function(options = {}) {
16
15
  return (await getHandler)(tree);
17
16
  };
18
17
  };
19
-
20
18
  //#endregion
21
- export { rehypeShiki as default };
19
+ export { rehypeShiki as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/rehype",
3
3
  "type": "module",
4
- "version": "4.0.1",
4
+ "version": "4.1.0",
5
5
  "description": "rehype integration for shiki",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -35,15 +35,15 @@
35
35
  "hast-util-to-string": "^3.0.1",
36
36
  "unified": "^11.0.5",
37
37
  "unist-util-visit": "^5.1.0",
38
- "shiki": "4.0.1",
39
- "@shikijs/types": "4.0.1"
38
+ "@shikijs/types": "4.1.0",
39
+ "shiki": "4.1.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "rehype-raw": "^7.0.0",
43
43
  "rehype-stringify": "^10.0.1",
44
44
  "remark-parse": "^11.0.0",
45
45
  "remark-rehype": "^11.1.2",
46
- "@shikijs/transformers": "4.0.1"
46
+ "@shikijs/transformers": "4.1.0"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "tsdown",
@@ -1,29 +0,0 @@
1
- import { toString } from "hast-util-to-string";
2
-
3
- //#region src/handlers.ts
4
- const InlineCodeHandlers = { "tailing-curly-colon": (_tree, node) => {
5
- const raw = toString(node);
6
- const match = raw.match(/(.+)\{:([\w-]+)\}$/);
7
- if (!match) return;
8
- return {
9
- type: "inline",
10
- code: match[1] ?? raw,
11
- lang: match.at(2)
12
- };
13
- } };
14
- const languagePrefix = "language-";
15
- const PreHandler = (_tree, node) => {
16
- const head = node.children[0];
17
- if (!head || head.type !== "element" || head.tagName !== "code" || !head.properties) return;
18
- const classes = head.properties.className;
19
- const languageClass = Array.isArray(classes) ? classes.find((d) => typeof d === "string" && d.startsWith(languagePrefix)) : void 0;
20
- return {
21
- type: "pre",
22
- lang: typeof languageClass === "string" ? languageClass.slice(9) : void 0,
23
- code: toString(head),
24
- meta: head.data?.meta ?? head.properties.metastring?.toString() ?? ""
25
- };
26
- };
27
-
28
- //#endregion
29
- export { PreHandler as n, InlineCodeHandlers as t };