@astrojs/markdown-remark 4.3.1 → 5.0.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.
@@ -1,7 +1,7 @@
1
1
  import type { Root } from 'hast';
2
2
  type Highlighter = (code: string, language: string, options?: {
3
3
  meta?: string;
4
- }) => string;
4
+ }) => Promise<string>;
5
5
  /**
6
6
  * A hast utility to syntax highlight code blocks with a given syntax highlighter.
7
7
  *
@@ -11,5 +11,5 @@ type Highlighter = (code: string, language: string, options?: {
11
11
  * A fnction which receives the code and language, and returns the HTML of a syntax
12
12
  * highlighted `<pre>` element.
13
13
  */
14
- export declare function highlightCodeBlocks(tree: Root, highlighter: Highlighter): void;
14
+ export declare function highlightCodeBlocks(tree: Root, highlighter: Highlighter): Promise<void>;
15
15
  export {};
package/dist/highlight.js CHANGED
@@ -3,7 +3,8 @@ import { toText } from "hast-util-to-text";
3
3
  import { removePosition } from "unist-util-remove-position";
4
4
  import { visitParents } from "unist-util-visit-parents";
5
5
  const languagePattern = /\blanguage-(\S+)\b/;
6
- function highlightCodeBlocks(tree, highlighter) {
6
+ async function highlightCodeBlocks(tree, highlighter) {
7
+ const nodes = [];
7
8
  visitParents(tree, { type: "element", tagName: "code" }, (node, ancestors) => {
8
9
  const parent = ancestors.at(-1);
9
10
  if (parent?.type !== "element" || parent.tagName !== "pre") {
@@ -30,15 +31,22 @@ function highlightCodeBlocks(tree, highlighter) {
30
31
  if (languageMatch?.[1] === "math") {
31
32
  return;
32
33
  }
34
+ nodes.push({
35
+ node,
36
+ language: languageMatch?.[1] || "plaintext",
37
+ parent,
38
+ grandParent: ancestors.at(-2)
39
+ });
40
+ });
41
+ for (const { node, language, grandParent, parent } of nodes) {
33
42
  const meta = node.data?.meta ?? node.properties.metastring ?? void 0;
34
43
  const code = toText(node, { whitespace: "pre" });
35
- const html = highlighter(code, languageMatch?.[1] || "plaintext", { meta });
44
+ const html = await highlighter(code, language, { meta });
36
45
  const replacement = fromHtml(html, { fragment: true }).children[0];
37
46
  removePosition(replacement);
38
- const grandParent = ancestors.at(-2);
39
47
  const index = grandParent.children.indexOf(parent);
40
48
  grandParent.children[index] = replacement;
41
- });
49
+ }
42
50
  }
43
51
  export {
44
52
  highlightCodeBlocks
package/dist/index.d.ts CHANGED
@@ -4,9 +4,7 @@ export { rehypeHeadingIds } from './rehype-collect-headings.js';
4
4
  export { remarkCollectImages } from './remark-collect-images.js';
5
5
  export { rehypePrism } from './rehype-prism.js';
6
6
  export { rehypeShiki } from './rehype-shiki.js';
7
- export { remarkPrism } from './remark-prism.js';
8
- export { remarkShiki } from './remark-shiki.js';
9
- export { createShikiHighlighter, replaceCssVariables, type ShikiHighlighter } from './shiki.js';
7
+ export { createShikiHighlighter, type ShikiHighlighter } from './shiki.js';
10
8
  export * from './types.js';
11
9
  export declare const markdownConfigDefaults: Required<AstroMarkdownOptions>;
12
10
  /**
package/dist/index.js CHANGED
@@ -22,9 +22,7 @@ import { rehypeHeadingIds as rehypeHeadingIds2 } from "./rehype-collect-headings
22
22
  import { remarkCollectImages as remarkCollectImages2 } from "./remark-collect-images.js";
23
23
  import { rehypePrism as rehypePrism2 } from "./rehype-prism.js";
24
24
  import { rehypeShiki as rehypeShiki2 } from "./rehype-shiki.js";
25
- import { remarkPrism } from "./remark-prism.js";
26
- import { remarkShiki } from "./remark-shiki.js";
27
- import { createShikiHighlighter, replaceCssVariables } from "./shiki.js";
25
+ import { createShikiHighlighter } from "./shiki.js";
28
26
  export * from "./types.js";
29
27
  const markdownConfigDefaults = {
30
28
  syntaxHighlight: "shiki",
@@ -139,8 +137,5 @@ export {
139
137
  rehypePrism2 as rehypePrism,
140
138
  rehypeShiki2 as rehypeShiki,
141
139
  remarkCollectImages2 as remarkCollectImages,
142
- remarkPrism,
143
- remarkShiki,
144
- replaceCssVariables,
145
140
  setVfileFrontmatter2 as setVfileFrontmatter
146
141
  };
@@ -1,10 +1,14 @@
1
1
  import { runHighlighterWithAstro } from "@astrojs/prism/dist/highlighter";
2
2
  import { highlightCodeBlocks } from "./highlight.js";
3
- const rehypePrism = () => (tree) => {
4
- highlightCodeBlocks(tree, (code, language) => {
5
- let { html, classLanguage } = runHighlighterWithAstro(language, code);
6
- return `<pre class="${classLanguage}"><code is:raw class="${classLanguage}">${html}</code></pre>`;
7
- });
3
+ const rehypePrism = () => {
4
+ return async (tree) => {
5
+ await highlightCodeBlocks(tree, (code, language) => {
6
+ let { html, classLanguage } = runHighlighterWithAstro(language, code);
7
+ return Promise.resolve(
8
+ `<pre class="${classLanguage}"><code is:raw class="${classLanguage}">${html}</code></pre>`
9
+ );
10
+ });
11
+ };
8
12
  };
9
13
  export {
10
14
  rehypePrism
@@ -5,7 +5,7 @@ const rehypeShiki = (config) => {
5
5
  return async (tree) => {
6
6
  highlighterAsync ??= createShikiHighlighter(config);
7
7
  const highlighter = await highlighterAsync;
8
- highlightCodeBlocks(tree, highlighter.highlight);
8
+ await highlightCodeBlocks(tree, highlighter.highlight);
9
9
  };
10
10
  };
11
11
  export {
package/dist/shiki.d.ts CHANGED
@@ -7,6 +7,6 @@ export interface ShikiHighlighter {
7
7
  * Raw `meta` information to be used by Shiki transformers
8
8
  */
9
9
  meta?: string;
10
- }): string;
10
+ }): Promise<string>;
11
11
  }
12
12
  export declare function createShikiHighlighter({ langs, theme, themes, wrap, transformers, }?: ShikiConfig): Promise<ShikiHighlighter>;
package/dist/shiki.js CHANGED
@@ -1,4 +1,8 @@
1
- import { bundledLanguages, createCssVariablesTheme, getHighlighter } from "shiki";
1
+ import {
2
+ createCssVariablesTheme,
3
+ getHighlighter,
4
+ isSpecialLang
5
+ } from "shiki";
2
6
  import { visit } from "unist-util-visit";
3
7
  const ASTRO_COLOR_REPLACEMENTS = {
4
8
  "--astro-code-foreground": "--astro-code-color-text",
@@ -19,15 +23,21 @@ async function createShikiHighlighter({
19
23
  } = {}) {
20
24
  theme = theme === "css-variables" ? cssVariablesTheme() : theme;
21
25
  const highlighter = await getHighlighter({
22
- langs: langs.length ? langs : Object.keys(bundledLanguages),
26
+ langs: ["plaintext", ...langs],
23
27
  themes: Object.values(themes).length ? Object.values(themes) : [theme]
24
28
  });
25
- const loadedLanguages = highlighter.getLoadedLanguages();
26
29
  return {
27
- highlight(code, lang = "plaintext", options) {
28
- if (lang !== "plaintext" && !loadedLanguages.includes(lang)) {
29
- console.warn(`[Shiki] The language "${lang}" doesn't exist, falling back to "plaintext".`);
30
- lang = "plaintext";
30
+ async highlight(code, lang = "plaintext", options) {
31
+ const loadedLanguages = highlighter.getLoadedLanguages();
32
+ if (!isSpecialLang(lang) && !loadedLanguages.includes(lang)) {
33
+ try {
34
+ await highlighter.loadLanguage(lang);
35
+ } catch (_err) {
36
+ console.warn(
37
+ `[Shiki] The language "${lang}" doesn't exist, falling back to "plaintext".`
38
+ );
39
+ lang = "plaintext";
40
+ }
31
41
  }
32
42
  const themeOptions = Object.values(themes).length ? { themes } : { theme };
33
43
  const inline = options?.inline ?? false;
@@ -109,6 +119,5 @@ function replaceCssVariables(str) {
109
119
  return str.replace(COLOR_REPLACEMENT_REGEX, (match) => ASTRO_COLOR_REPLACEMENTS[match] || match);
110
120
  }
111
121
  export {
112
- createShikiHighlighter,
113
- replaceCssVariables
122
+ createShikiHighlighter
114
123
  };
package/dist/types.d.ts CHANGED
@@ -30,12 +30,6 @@ export interface AstroMarkdownOptions {
30
30
  gfm?: boolean;
31
31
  smartypants?: boolean;
32
32
  }
33
- export interface ImageMetadata {
34
- src: string;
35
- width: number;
36
- height: number;
37
- type: string;
38
- }
39
33
  export interface MarkdownProcessor {
40
34
  render: (content: string, opts?: MarkdownProcessorRenderOptions) => Promise<MarkdownProcessorRenderResult>;
41
35
  }
@@ -51,26 +45,14 @@ export interface MarkdownProcessorRenderResult {
51
45
  frontmatter: Record<string, any>;
52
46
  };
53
47
  }
54
- export interface MarkdownRenderingOptions extends AstroMarkdownOptions, MarkdownProcessorRenderOptions {
55
- }
56
48
  export interface MarkdownHeading {
57
49
  depth: number;
58
50
  slug: string;
59
51
  text: string;
60
52
  }
61
- export interface MarkdownMetadata {
62
- headings: MarkdownHeading[];
63
- source: string;
64
- html: string;
65
- }
66
53
  export interface MarkdownVFile extends VFile {
67
54
  data: {
68
55
  __astroHeadings?: MarkdownHeading[];
69
56
  imagePaths?: Set<string>;
70
57
  };
71
58
  }
72
- export interface MarkdownRenderingResult {
73
- metadata: MarkdownMetadata;
74
- vfile: MarkdownVFile;
75
- code: string;
76
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "4.3.1",
3
+ "version": "5.0.0",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -1,5 +0,0 @@
1
- import type { RemarkPlugin } from './types.js';
2
- /**
3
- * @deprecated Use `rehypePrism` instead
4
- */
5
- export declare function remarkPrism(): ReturnType<RemarkPlugin>;
@@ -1,19 +0,0 @@
1
- import { runHighlighterWithAstro } from "@astrojs/prism/dist/highlighter";
2
- import { visit } from "unist-util-visit";
3
- function remarkPrism() {
4
- return function(tree) {
5
- visit(tree, "code", (node) => {
6
- let { lang, value } = node;
7
- node.type = "html";
8
- let { html, classLanguage } = runHighlighterWithAstro(lang, value);
9
- let classes = [classLanguage];
10
- node.value = `<pre class="${classes.join(
11
- " "
12
- )}"><code is:raw class="${classLanguage}">${html}</code></pre>`;
13
- return node;
14
- });
15
- };
16
- }
17
- export {
18
- remarkPrism
19
- };
@@ -1,5 +0,0 @@
1
- import type { RemarkPlugin, ShikiConfig } from './types.js';
2
- /**
3
- * @deprecated Use `rehypeShiki` instead
4
- */
5
- export declare function remarkShiki(config?: ShikiConfig): ReturnType<RemarkPlugin>;
@@ -1,19 +0,0 @@
1
- import { visit } from "unist-util-visit";
2
- import { createShikiHighlighter } from "./shiki.js";
3
- function remarkShiki(config) {
4
- let highlighterAsync;
5
- return async (tree) => {
6
- highlighterAsync ??= createShikiHighlighter(config);
7
- const highlighter = await highlighterAsync;
8
- visit(tree, "code", (node) => {
9
- const lang = typeof node.lang === "string" ? node.lang : "plaintext";
10
- const html = highlighter.highlight(node.value, lang);
11
- node.type = "html";
12
- node.value = html;
13
- node.children = [];
14
- });
15
- };
16
- }
17
- export {
18
- remarkShiki
19
- };