@shikijs/rehype 1.27.2 → 1.29.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.
Files changed (2) hide show
  1. package/dist/core.mjs +22 -22
  2. package/package.json +4 -4
package/dist/core.mjs CHANGED
@@ -88,20 +88,7 @@ function rehypeShikiFromHighlighter(highlighter, options) {
88
88
  }
89
89
  }
90
90
  return (tree) => {
91
- const languageQueue = [];
92
91
  const queue = [];
93
- function getLanguage(lang) {
94
- if (!lang)
95
- return defaultLanguage;
96
- if (highlighter.getLoadedLanguages().includes(lang) || isSpecialLang(lang))
97
- return lang;
98
- if (lazy) {
99
- languageQueue.push(lang);
100
- return lang;
101
- }
102
- if (fallbackLanguage)
103
- return fallbackLanguage;
104
- }
105
92
  visit(tree, "element", (node, index, parent) => {
106
93
  let handler;
107
94
  if (!parent || index == null)
@@ -117,7 +104,18 @@ function rehypeShikiFromHighlighter(highlighter, options) {
117
104
  const res = handler(tree, node);
118
105
  if (!res)
119
106
  return;
120
- const lang = getLanguage(res.lang);
107
+ let lang;
108
+ let lazyLoad = false;
109
+ if (!res.lang) {
110
+ lang = defaultLanguage;
111
+ } else if (highlighter.getLoadedLanguages().includes(res.lang) || isSpecialLang(res.lang)) {
112
+ lang = res.lang;
113
+ } else if (lazy) {
114
+ lazyLoad = true;
115
+ lang = res.lang;
116
+ } else if (fallbackLanguage) {
117
+ lang = fallbackLanguage;
118
+ }
121
119
  if (!lang)
122
120
  return;
123
121
  const processNode = () => {
@@ -131,18 +129,20 @@ function rehypeShikiFromHighlighter(highlighter, options) {
131
129
  head.tagName = "span";
132
130
  }
133
131
  }
134
- parent.children.splice(index, 1, ...fragment.children);
132
+ parent.children[index] = fragment;
135
133
  };
136
- if (lazy)
137
- queue.push(processNode);
138
- else
134
+ if (lazyLoad) {
135
+ queue.push(highlighter.loadLanguage(lang).then(() => processNode()));
136
+ } else {
139
137
  processNode();
138
+ }
140
139
  return "skip";
141
140
  });
142
- if (lazy) {
143
- return highlighter.loadLanguage(...languageQueue).then(() => {
144
- queue.forEach((fn) => fn());
145
- });
141
+ if (queue.length > 0) {
142
+ async function run() {
143
+ await Promise.all(queue);
144
+ }
145
+ return run();
146
146
  }
147
147
  };
148
148
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/rehype",
3
3
  "type": "module",
4
- "version": "1.27.2",
4
+ "version": "1.29.0",
5
5
  "description": "rehype integration for shiki",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -49,15 +49,15 @@
49
49
  "hast-util-to-string": "^3.0.1",
50
50
  "unified": "^11.0.5",
51
51
  "unist-util-visit": "^5.0.0",
52
- "shiki": "1.27.2",
53
- "@shikijs/types": "1.27.2"
52
+ "@shikijs/types": "1.29.0",
53
+ "shiki": "1.29.0"
54
54
  },
55
55
  "devDependencies": {
56
56
  "rehype-raw": "^7.0.0",
57
57
  "rehype-stringify": "^10.0.1",
58
58
  "remark-parse": "^11.0.0",
59
59
  "remark-rehype": "^11.1.1",
60
- "@shikijs/transformers": "1.27.2"
60
+ "@shikijs/transformers": "1.29.0"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "unbuild",