@astrojs/markdoc 0.5.2 → 0.7.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/README.md CHANGED
@@ -458,6 +458,40 @@ To achieve a more Markdown-like experience, where HTML elements can be included
458
458
  > **Warning**
459
459
  > When `allowHTML` is enabled, HTML markup inside Markdoc documents will be rendered as actual HTML elements (including `<script>`), making attack vectors like XSS possible. Ensure that any HTML markup comes from trusted sources.
460
460
 
461
+ ### `ignoreIndentation`
462
+
463
+ By default, any content that is indented by four spaces is treated as a code block. Unfortunately, this behavior makes it difficult to use arbitrary levels of indentation to improve the readability of documents with complex structure.
464
+
465
+ When using nested tags in Markdoc, it can be helpful to indent the content inside of tags so that the level of depth is clear. To support arbitrary indentation, we have to disable the indent-based code blocks and modify several other markdown-it parsing rules that account for indent-based code blocks. These changes can be applied by enabling the ignoreIndentation option.
466
+
467
+ ```diff lang="js" "ignoreIndentation: true"
468
+ // astro.config.mjs
469
+ import { defineConfig } from 'astro/config';
470
+ import markdoc from '@astrojs/markdoc';
471
+
472
+ export default defineConfig({
473
+ // ...
474
+ + integrations: [markdoc({ ignoreIndentation: true })],
475
+ // ^^^^^^^^^^^^^^^^^^^^^^^
476
+ });
477
+ ```
478
+
479
+ ```md
480
+ # Welcome to Markdoc with indented tags 👋
481
+
482
+ # Note: Can use either spaces or tabs for indentation
483
+
484
+ {% custom-tag %}
485
+ {% custom-tag %} ### Tags can be indented for better readability
486
+
487
+ {% another-custom-tag %}
488
+ This is easier to follow when there is a lot of nesting
489
+ {% /another-custom-tag %}
490
+
491
+ {% /custom-tag %}
492
+ {% /custom-tag %}
493
+ ```
494
+
461
495
  ## Examples
462
496
 
463
497
  - The [Astro Markdoc starter template](https://github.com/withastro/astro/tree/latest/examples/with-markdoc) shows how to use Markdoc files in your Astro project.
@@ -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 "shiki";
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 cacheID = typeof theme === "string" ? theme : theme.name;
27
- if (!highlighterCache.has(cacheID)) {
28
- highlighterCache.set(
29
- cacheID,
30
- await getHighlighter({ theme }).then((hl) => {
31
- hl.setColorReplacements(ASTRO_COLOR_REPLACEMENTS);
32
- return hl;
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/dist/options.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export interface MarkdocIntegrationOptions {
2
2
  allowHTML?: boolean;
3
+ ignoreIndentation?: boolean;
3
4
  }
package/dist/tokenizer.js CHANGED
@@ -11,6 +11,9 @@ function getMarkdocTokenizer(options) {
11
11
  tokenizerOptions.allowIndentation = true;
12
12
  tokenizerOptions.html = true;
13
13
  }
14
+ if (options?.ignoreIndentation) {
15
+ tokenizerOptions.allowIndentation = true;
16
+ }
14
17
  _cachedMarkdocTokenizers[key] = new Markdoc.Tokenizer(tokenizerOptions);
15
18
  }
16
19
  return _cachedMarkdocTokenizers[key];
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.5.2",
4
+ "version": "0.7.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
- "shiki": "^0.14.3",
65
+ "shikiji": "^0.6.8",
66
66
  "zod": "3.21.1",
67
67
  "@astrojs/internal-helpers": "0.2.1",
68
68
  "@astrojs/prism": "3.0.0"
69
69
  },
70
70
  "peerDependencies": {
71
- "astro": "^3.2.3"
71
+ "astro": "^3.3.4"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@types/chai": "^4.3.5",
@@ -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.2.1",
85
- "astro": "3.2.3",
84
+ "@astrojs/markdown-remark": "3.3.0",
85
+ "astro": "3.3.4",
86
86
  "astro-scripts": "0.0.14"
87
87
  },
88
88
  "engines": {