zenn-markdown-html 0.2.12-alpha.0 → 0.2.12-alpha.2

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.
@@ -26,10 +26,22 @@ var _shiki = require("shiki");
26
26
  */
27
27
 
28
28
  /**
29
- * Shiki ハイライターのシングルトンインスタンス
30
- * getHighlighter() で初期化され、以降は再利用される
29
+ * Shiki ハイライターの初期化 Promise をキャッシュ
30
+ *
31
+ * 注: インスタンスではなく Promise をキャッシュすることで、
32
+ * Promise.all による並列処理時の競合状態を防ぐ。
33
+ *
34
+ * 問題のあるパターン:
35
+ * let instance = null;
36
+ * if (instance) return instance;
37
+ * instance = await createHighlighter(); // ← await 中に他の呼び出しが来る
38
+ *
39
+ * 正しいパターン:
40
+ * let promise = null;
41
+ * if (!promise) promise = createHighlighter(); // ← await しない
42
+ * return promise; // ← 同じ Promise を返す
31
43
  */
32
- let highlighterInstance = null;
44
+ let highlighterPromise = null;
33
45
  const SHIKI_THEME = 'github-dark';
34
46
 
35
47
  /**
@@ -37,16 +49,14 @@ const SHIKI_THEME = 'github-dark';
37
49
  * 最初は最低限のセットで初期化し、必要に応じて言語をロードする
38
50
  */
39
51
  async function getHighlighter() {
40
- if (highlighterInstance) {
41
- return highlighterInstance;
52
+ if (!highlighterPromise) {
53
+ // Promise をキャッシュ(await しない)
54
+ highlighterPromise = (0, _shiki.createHighlighter)({
55
+ themes: [SHIKI_THEME],
56
+ langs: []
57
+ });
42
58
  }
43
-
44
- // 最初は空の言語セットで初期化(高速)
45
- highlighterInstance = await (0, _shiki.createHighlighter)({
46
- themes: [SHIKI_THEME],
47
- langs: []
48
- });
49
- return highlighterInstance;
59
+ return highlighterPromise;
50
60
  }
51
61
 
52
62
  /**
@@ -248,9 +248,11 @@ async function applyHighlighting(html, codeBlocks) {
248
248
  }));
249
249
 
250
250
  // プレースホルダーを置換
251
+ // 注: replace の第2引数を関数にすることで、$' や $` などの
252
+ // 特殊パターン解釈を防ぐ(コードブロック内に $ が含まれる場合の対策)
251
253
  let result = html;
252
254
  for (let i = 0; i < highlightedBlocks.length; i++) {
253
- result = result.replace(codeBlocks[i].placeholder, highlightedBlocks[i]);
255
+ result = result.replace(codeBlocks[i].placeholder, () => highlightedBlocks[i]);
254
256
  }
255
257
  return result;
256
258
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zenn-markdown-html",
3
- "version": "0.2.12-alpha.0",
3
+ "version": "0.2.12-alpha.2",
4
4
  "license": "MIT",
5
5
  "description": "Convert markdown to zenn flavor html.",
6
6
  "main": "lib/index.js",
@@ -62,7 +62,7 @@
62
62
  "sanitize-html": "^2.17.0",
63
63
  "shiki": "^1.24.0"
64
64
  },
65
- "gitHead": "3d5f1d52c73b986b5a0ec911c7078d7a4822566a",
65
+ "gitHead": "b661fe7473d9c56fa6b456ea471ab4d40f5eada8",
66
66
  "publishConfig": {
67
67
  "access": "public"
68
68
  }