@astrojs/markdoc 0.7.1 → 0.7.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.
@@ -1,3 +1,3 @@
1
1
  import type { ShikiConfig } from 'astro';
2
2
  import type { AstroMarkdocConfig } from '../config.js';
3
- export default function shiki({ langs, theme, wrap, }?: ShikiConfig): Promise<AstroMarkdocConfig>;
3
+ export default function shiki(config?: ShikiConfig): Promise<AstroMarkdocConfig>;
@@ -1,90 +1,21 @@
1
+ import { createShikiHighlighter } from "@astrojs/markdown-remark";
1
2
  import Markdoc from "@markdoc/markdoc";
2
3
  import { unescapeHTML } from "astro/runtime/server/index.js";
3
- import { bundledLanguages, getHighlighter } from "shikiji";
4
- const ASTRO_COLOR_REPLACEMENTS = {
5
- "#000001": "var(--astro-code-color-text)",
6
- "#000002": "var(--astro-code-color-background)",
7
- "#000004": "var(--astro-code-token-constant)",
8
- "#000005": "var(--astro-code-token-string)",
9
- "#000006": "var(--astro-code-token-comment)",
10
- "#000007": "var(--astro-code-token-keyword)",
11
- "#000008": "var(--astro-code-token-parameter)",
12
- "#000009": "var(--astro-code-token-function)",
13
- "#000010": "var(--astro-code-token-string-expression)",
14
- "#000011": "var(--astro-code-token-punctuation)",
15
- "#000012": "var(--astro-code-token-link)"
16
- };
17
- const COLOR_REPLACEMENT_REGEX = new RegExp(
18
- `(${Object.keys(ASTRO_COLOR_REPLACEMENTS).join("|")})`,
19
- "g"
20
- );
21
- const PRE_SELECTOR = /<pre class="(.*?)shiki(.*?)"/;
22
- const LINE_SELECTOR = /<span class="line"><span style="(.*?)">([\+|\-])/g;
23
- const INLINE_STYLE_SELECTOR = /style="(.*?)"/;
24
- const INLINE_STYLE_SELECTOR_GLOBAL = /style="(.*?)"/g;
25
- const highlighterCache = /* @__PURE__ */ new Map();
26
- async function shiki({
27
- langs = [],
28
- theme = "github-dark",
29
- wrap = false
30
- } = {}) {
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
- }
4
+ async function shiki(config) {
5
+ const highlighter = await createShikiHighlighter(config);
40
6
  return {
41
7
  nodes: {
42
8
  fence: {
43
9
  attributes: Markdoc.nodes.fence.attributes,
44
10
  transform({ attributes }) {
45
- let lang;
46
- if (typeof attributes.language === "string") {
47
- const langExists = highlighter.getLoadedLanguages().includes(attributes.language);
48
- if (langExists) {
49
- lang = attributes.language;
50
- } else {
51
- console.warn(
52
- `[Shiki highlighter] The language "${attributes.language}" doesn't exist, falling back to plaintext.`
53
- );
54
- lang = "plaintext";
55
- }
56
- } else {
57
- lang = "plaintext";
58
- }
59
- let html = highlighter.codeToHtml(attributes.content, { lang, theme });
60
- html = html.replace(PRE_SELECTOR, `<pre class="$1astro-code$2"`);
61
- if (attributes.language === "diff") {
62
- html = html.replace(
63
- LINE_SELECTOR,
64
- '<span class="line"><span style="$1"><span style="user-select: none;">$2</span>'
65
- );
66
- }
67
- if (wrap === false) {
68
- html = html.replace(INLINE_STYLE_SELECTOR, 'style="$1; overflow-x: auto;"');
69
- } else if (wrap === true) {
70
- html = html.replace(
71
- INLINE_STYLE_SELECTOR,
72
- 'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
73
- );
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
- }
11
+ const lang = typeof attributes.language === "string" ? attributes.language : "plaintext";
12
+ const html = highlighter.highlight(attributes.content, lang);
79
13
  return unescapeHTML(html);
80
14
  }
81
15
  }
82
16
  }
83
17
  };
84
18
  }
85
- function replaceCssVariables(str) {
86
- return str.replace(COLOR_REPLACEMENT_REGEX, (match) => ASTRO_COLOR_REPLACEMENTS[match] || match);
87
- }
88
19
  export {
89
20
  shiki as default
90
21
  };
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.7.1",
4
+ "version": "0.7.2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -81,8 +81,8 @@
81
81
  "mocha": "^10.2.0",
82
82
  "rollup": "^3.28.1",
83
83
  "vite": "^4.4.9",
84
- "@astrojs/markdown-remark": "3.3.0",
85
- "astro": "3.4.1",
84
+ "@astrojs/markdown-remark": "3.5.0",
85
+ "astro": "3.5.4",
86
86
  "astro-scripts": "0.0.14"
87
87
  },
88
88
  "engines": {