@astrojs/markdoc 0.5.1 → 0.6.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/dist/extensions/shiki.js +22 -15
- package/package.json +9 -6
package/dist/extensions/shiki.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Markdoc from "@markdoc/markdoc";
|
|
2
2
|
import { unescapeHTML } from "astro/runtime/server/index.js";
|
|
3
|
-
import { getHighlighter } from "
|
|
3
|
+
import { bundledLanguages, getHighlighter } from "shikiji";
|
|
4
4
|
const ASTRO_COLOR_REPLACEMENTS = {
|
|
5
5
|
"#000001": "var(--astro-code-color-text)",
|
|
6
6
|
"#000002": "var(--astro-code-color-background)",
|
|
@@ -14,28 +14,28 @@ const ASTRO_COLOR_REPLACEMENTS = {
|
|
|
14
14
|
"#000011": "var(--astro-code-token-punctuation)",
|
|
15
15
|
"#000012": "var(--astro-code-token-link)"
|
|
16
16
|
};
|
|
17
|
+
const COLOR_REPLACEMENT_REGEX = new RegExp(
|
|
18
|
+
`(${Object.keys(ASTRO_COLOR_REPLACEMENTS).join("|")})`,
|
|
19
|
+
"g"
|
|
20
|
+
);
|
|
17
21
|
const PRE_SELECTOR = /<pre class="(.*?)shiki(.*?)"/;
|
|
18
22
|
const LINE_SELECTOR = /<span class="line"><span style="(.*?)">([\+|\-])/g;
|
|
19
23
|
const INLINE_STYLE_SELECTOR = /style="(.*?)"/;
|
|
24
|
+
const INLINE_STYLE_SELECTOR_GLOBAL = /style="(.*?)"/g;
|
|
20
25
|
const highlighterCache = /* @__PURE__ */ new Map();
|
|
21
26
|
async function shiki({
|
|
22
27
|
langs = [],
|
|
23
28
|
theme = "github-dark",
|
|
24
29
|
wrap = false
|
|
25
30
|
} = {}) {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
const highlighter = highlighterCache.get(cacheID);
|
|
37
|
-
for (const lang of langs) {
|
|
38
|
-
await highlighter.loadLanguage(lang);
|
|
31
|
+
const cacheId = typeof theme === "string" ? theme : theme.name || "";
|
|
32
|
+
let highlighter = highlighterCache.get(cacheId);
|
|
33
|
+
if (!highlighter) {
|
|
34
|
+
highlighter = await getHighlighter({
|
|
35
|
+
langs: langs.length ? langs : Object.keys(bundledLanguages),
|
|
36
|
+
themes: [theme]
|
|
37
|
+
});
|
|
38
|
+
highlighterCache.set(cacheId, highlighter);
|
|
39
39
|
}
|
|
40
40
|
return {
|
|
41
41
|
nodes: {
|
|
@@ -56,7 +56,7 @@ async function shiki({
|
|
|
56
56
|
} else {
|
|
57
57
|
lang = "plaintext";
|
|
58
58
|
}
|
|
59
|
-
let html = highlighter.codeToHtml(attributes.content, { lang });
|
|
59
|
+
let html = highlighter.codeToHtml(attributes.content, { lang, theme });
|
|
60
60
|
html = html.replace(PRE_SELECTOR, `<pre class="$1astro-code$2"`);
|
|
61
61
|
if (attributes.language === "diff") {
|
|
62
62
|
html = html.replace(
|
|
@@ -72,12 +72,19 @@ async function shiki({
|
|
|
72
72
|
'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
|
+
const themeName = typeof theme === "string" ? theme : theme.name;
|
|
76
|
+
if (themeName === "css-variables") {
|
|
77
|
+
html = html.replace(INLINE_STYLE_SELECTOR_GLOBAL, (m) => replaceCssVariables(m));
|
|
78
|
+
}
|
|
75
79
|
return unescapeHTML(html);
|
|
76
80
|
}
|
|
77
81
|
}
|
|
78
82
|
}
|
|
79
83
|
};
|
|
80
84
|
}
|
|
85
|
+
function replaceCssVariables(str) {
|
|
86
|
+
return str.replace(COLOR_REPLACEMENT_REGEX, (match) => ASTRO_COLOR_REPLACEMENTS[match] || match);
|
|
87
|
+
}
|
|
81
88
|
export {
|
|
82
89
|
shiki as default
|
|
83
90
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/markdoc",
|
|
3
3
|
"description": "Add support for Markdoc in your Astro site",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -62,13 +62,13 @@
|
|
|
62
62
|
"gray-matter": "^4.0.3",
|
|
63
63
|
"htmlparser2": "^9.0.0",
|
|
64
64
|
"kleur": "^4.1.5",
|
|
65
|
-
"
|
|
65
|
+
"shikiji": "^0.6.8",
|
|
66
66
|
"zod": "3.21.1",
|
|
67
|
-
"@astrojs/internal-helpers": "0.2.
|
|
67
|
+
"@astrojs/internal-helpers": "0.2.1",
|
|
68
68
|
"@astrojs/prism": "3.0.0"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
|
-
"astro": "^3.
|
|
71
|
+
"astro": "^3.3.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@types/chai": "^4.3.5",
|
|
@@ -81,13 +81,16 @@
|
|
|
81
81
|
"mocha": "^10.2.0",
|
|
82
82
|
"rollup": "^3.28.1",
|
|
83
83
|
"vite": "^4.4.9",
|
|
84
|
-
"@astrojs/markdown-remark": "3.
|
|
85
|
-
"astro": "3.
|
|
84
|
+
"@astrojs/markdown-remark": "3.3.0",
|
|
85
|
+
"astro": "3.3.0",
|
|
86
86
|
"astro-scripts": "0.0.14"
|
|
87
87
|
},
|
|
88
88
|
"engines": {
|
|
89
89
|
"node": ">=18.14.1"
|
|
90
90
|
},
|
|
91
|
+
"publishConfig": {
|
|
92
|
+
"provenance": true
|
|
93
|
+
},
|
|
91
94
|
"scripts": {
|
|
92
95
|
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
|
93
96
|
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|