@astrojs/markdown-remark 3.4.0 → 4.0.0-beta.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.
@@ -4,9 +4,3 @@ export declare class InvalidAstroDataError extends TypeError {
4
4
  }
5
5
  export declare function safelyGetAstroData(vfileData: Data): MarkdownAstroData | InvalidAstroDataError;
6
6
  export declare function setVfileFrontmatter(vfile: VFile, frontmatter: Record<string, any>): void;
7
- /**
8
- * @deprecated Use `setVfileFrontmatter` instead
9
- */
10
- export declare function toRemarkInitializeAstroData({ userFrontmatter, }: {
11
- userFrontmatter: Record<string, any>;
12
- }): () => (tree: any, vfile: VFile) => void;
@@ -24,18 +24,8 @@ function setVfileFrontmatter(vfile, frontmatter) {
24
24
  vfile.data.astro ??= {};
25
25
  vfile.data.astro.frontmatter = frontmatter;
26
26
  }
27
- function toRemarkInitializeAstroData({
28
- userFrontmatter
29
- }) {
30
- return () => function(tree, vfile) {
31
- if (!vfile.data.astro) {
32
- vfile.data.astro = { frontmatter: userFrontmatter };
33
- }
34
- };
35
- }
36
27
  export {
37
28
  InvalidAstroDataError,
38
29
  safelyGetAstroData,
39
- setVfileFrontmatter,
40
- toRemarkInitializeAstroData
30
+ setVfileFrontmatter
41
31
  };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,13 @@
1
- import type { AstroMarkdownOptions, MarkdownProcessor, MarkdownRenderingOptions, MarkdownRenderingResult } from './types.js';
1
+ import type { AstroMarkdownOptions, MarkdownProcessor } from './types.js';
2
2
  export { InvalidAstroDataError, setVfileFrontmatter } from './frontmatter-injection.js';
3
3
  export { rehypeHeadingIds } from './rehype-collect-headings.js';
4
4
  export { remarkCollectImages } from './remark-collect-images.js';
5
5
  export { remarkPrism } from './remark-prism.js';
6
6
  export { remarkShiki } from './remark-shiki.js';
7
+ export { createShikiHighlighter, replaceCssVariables, type ShikiHighlighter } from './shiki.js';
7
8
  export * from './types.js';
8
- export declare const markdownConfigDefaults: Omit<Required<AstroMarkdownOptions>, 'drafts'>;
9
+ export declare const markdownConfigDefaults: Required<AstroMarkdownOptions>;
9
10
  /**
10
11
  * Create a markdown preprocessor to render multiple markdown files
11
12
  */
12
13
  export declare function createMarkdownProcessor(opts?: AstroMarkdownOptions): Promise<MarkdownProcessor>;
13
- /**
14
- * Shared utility for rendering markdown
15
- *
16
- * @deprecated Use `createMarkdownProcessor` instead for better performance
17
- */
18
- export declare function renderMarkdown(content: string, opts: MarkdownRenderingOptions): Promise<MarkdownRenderingResult>;
package/dist/index.js CHANGED
@@ -22,6 +22,7 @@ import { rehypeHeadingIds as rehypeHeadingIds2 } from "./rehype-collect-headings
22
22
  import { remarkCollectImages as remarkCollectImages2 } from "./remark-collect-images.js";
23
23
  import { remarkPrism as remarkPrism2 } from "./remark-prism.js";
24
24
  import { remarkShiki as remarkShiki2 } from "./remark-shiki.js";
25
+ import { createShikiHighlighter, replaceCssVariables } from "./shiki.js";
25
26
  export * from "./types.js";
26
27
  const markdownConfigDefaults = {
27
28
  syntaxHighlight: "shiki",
@@ -102,31 +103,11 @@ async function createMarkdownProcessor(opts) {
102
103
  headings: result.data.__astroHeadings ?? [],
103
104
  imagePaths: result.data.imagePaths ?? /* @__PURE__ */ new Set(),
104
105
  frontmatter: astroData.frontmatter ?? {}
105
- },
106
- // Compat for `renderMarkdown` only. Do not use!
107
- __renderMarkdownCompat: {
108
- result
109
106
  }
110
107
  };
111
108
  }
112
109
  };
113
110
  }
114
- async function renderMarkdown(content, opts) {
115
- const processor = await createMarkdownProcessor(opts);
116
- const result = await processor.render(content, {
117
- fileURL: opts.fileURL,
118
- frontmatter: opts.frontmatter
119
- });
120
- return {
121
- code: result.code,
122
- metadata: {
123
- headings: result.metadata.headings,
124
- source: content,
125
- html: result.code
126
- },
127
- vfile: result.__renderMarkdownCompat.result
128
- };
129
- }
130
111
  function prefixError(err, prefix) {
131
112
  if (err?.message) {
132
113
  try {
@@ -147,11 +128,12 @@ ${err.message}`;
147
128
  export {
148
129
  InvalidAstroDataError2 as InvalidAstroDataError,
149
130
  createMarkdownProcessor,
131
+ createShikiHighlighter,
150
132
  markdownConfigDefaults,
151
133
  rehypeHeadingIds2 as rehypeHeadingIds,
152
134
  remarkCollectImages2 as remarkCollectImages,
153
135
  remarkPrism2 as remarkPrism,
154
136
  remarkShiki2 as remarkShiki,
155
- renderMarkdown,
137
+ replaceCssVariables,
156
138
  setVfileFrontmatter2 as setVfileFrontmatter
157
139
  };
@@ -1 +1 @@
1
- export { InvalidAstroDataError, safelyGetAstroData, toRemarkInitializeAstroData, } from './frontmatter-injection.js';
1
+ export { InvalidAstroDataError, safelyGetAstroData } from './frontmatter-injection.js';
package/dist/internal.js CHANGED
@@ -1,10 +1,5 @@
1
- import {
2
- InvalidAstroDataError,
3
- safelyGetAstroData,
4
- toRemarkInitializeAstroData
5
- } from "./frontmatter-injection.js";
1
+ import { InvalidAstroDataError, safelyGetAstroData } from "./frontmatter-injection.js";
6
2
  export {
7
3
  InvalidAstroDataError,
8
- safelyGetAstroData,
9
- toRemarkInitializeAstroData
4
+ safelyGetAstroData
10
5
  };
@@ -1,2 +1,2 @@
1
1
  import type { RemarkPlugin, ShikiConfig } from './types.js';
2
- export declare function remarkShiki({ langs, theme, experimentalThemes, wrap, }?: ShikiConfig): ReturnType<RemarkPlugin>;
2
+ export declare function remarkShiki(config?: ShikiConfig): ReturnType<RemarkPlugin>;
@@ -1,84 +1,19 @@
1
- import { bundledLanguages, getHighlighter } from "shikiji";
2
1
  import { visit } from "unist-util-visit";
3
- const ASTRO_COLOR_REPLACEMENTS = {
4
- "#000001": "var(--astro-code-color-text)",
5
- "#000002": "var(--astro-code-color-background)",
6
- "#000004": "var(--astro-code-token-constant)",
7
- "#000005": "var(--astro-code-token-string)",
8
- "#000006": "var(--astro-code-token-comment)",
9
- "#000007": "var(--astro-code-token-keyword)",
10
- "#000008": "var(--astro-code-token-parameter)",
11
- "#000009": "var(--astro-code-token-function)",
12
- "#000010": "var(--astro-code-token-string-expression)",
13
- "#000011": "var(--astro-code-token-punctuation)",
14
- "#000012": "var(--astro-code-token-link)"
15
- };
16
- const COLOR_REPLACEMENT_REGEX = new RegExp(
17
- `(${Object.keys(ASTRO_COLOR_REPLACEMENTS).join("|")})`,
18
- "g"
19
- );
20
- const highlighterCacheAsync = /* @__PURE__ */ new Map();
21
- function remarkShiki({
22
- langs = [],
23
- theme = "github-dark",
24
- experimentalThemes = {},
25
- wrap = false
26
- } = {}) {
27
- const themes = experimentalThemes;
28
- const cacheId = Object.values(themes).map((t) => typeof t === "string" ? t : t.name ?? "").join(",") + (typeof theme === "string" ? theme : theme.name ?? "") + langs.map((l) => l.name ?? l.id).join(",");
29
- let highlighterAsync = highlighterCacheAsync.get(cacheId);
30
- if (!highlighterAsync) {
31
- highlighterAsync = getHighlighter({
32
- langs: langs.length ? langs : Object.keys(bundledLanguages),
33
- themes: Object.values(themes).length ? Object.values(themes) : [theme]
34
- });
35
- highlighterCacheAsync.set(cacheId, highlighterAsync);
36
- }
2
+ import { createShikiHighlighter } from "./shiki.js";
3
+ function remarkShiki(config) {
4
+ let highlighterAsync;
37
5
  return async (tree) => {
6
+ highlighterAsync ??= createShikiHighlighter(config);
38
7
  const highlighter = await highlighterAsync;
39
8
  visit(tree, "code", (node) => {
40
- let lang;
41
- if (typeof node.lang === "string") {
42
- const langExists = highlighter.getLoadedLanguages().includes(node.lang);
43
- if (langExists) {
44
- lang = node.lang;
45
- } else {
46
- console.warn(`The language "${node.lang}" doesn't exist, falling back to plaintext.`);
47
- lang = "plaintext";
48
- }
49
- } else {
50
- lang = "plaintext";
51
- }
52
- let themeOptions = Object.values(themes).length ? { themes } : { theme };
53
- let html = highlighter.codeToHtml(node.value, { ...themeOptions, lang });
54
- html = html.replace(/<pre class="(.*?)shiki(.*?)"/, `<pre class="$1astro-code$2"`);
55
- if (node.lang === "diff") {
56
- html = html.replace(
57
- /<span class="line"><span style="(.*?)">([\+|\-])/g,
58
- '<span class="line"><span style="$1"><span style="user-select: none;">$2</span>'
59
- );
60
- }
61
- if (wrap === false) {
62
- html = html.replace(/style="(.*?)"/, 'style="$1; overflow-x: auto;"');
63
- } else if (wrap === true) {
64
- html = html.replace(
65
- /style="(.*?)"/,
66
- 'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
67
- );
68
- }
69
- const themeName = typeof theme === "string" ? theme : theme.name;
70
- if (themeName === "css-variables") {
71
- html = html.replace(/style="(.*?)"/g, (m) => replaceCssVariables(m));
72
- }
9
+ const lang = typeof node.lang === "string" ? node.lang : "plaintext";
10
+ const html = highlighter.highlight(node.value, lang);
73
11
  node.type = "html";
74
12
  node.value = html;
75
13
  node.children = [];
76
14
  });
77
15
  };
78
16
  }
79
- function replaceCssVariables(str) {
80
- return str.replace(COLOR_REPLACEMENT_REGEX, (match) => ASTRO_COLOR_REPLACEMENTS[match] || match);
81
- }
82
17
  export {
83
18
  remarkShiki
84
19
  };
@@ -0,0 +1,7 @@
1
+ import type { ShikiConfig } from './types.js';
2
+ export interface ShikiHighlighter {
3
+ highlight(code: string, lang?: string, options?: {
4
+ inline?: boolean;
5
+ }): string;
6
+ }
7
+ export declare function createShikiHighlighter({ langs, theme, experimentalThemes, wrap, }?: ShikiConfig): Promise<ShikiHighlighter>;
package/dist/shiki.js ADDED
@@ -0,0 +1,104 @@
1
+ import { bundledLanguages, getHighlighter } from "shikiji";
2
+ import { visit } from "unist-util-visit";
3
+ const ASTRO_COLOR_REPLACEMENTS = {
4
+ "#000001": "var(--astro-code-color-text)",
5
+ "#000002": "var(--astro-code-color-background)",
6
+ "#000004": "var(--astro-code-token-constant)",
7
+ "#000005": "var(--astro-code-token-string)",
8
+ "#000006": "var(--astro-code-token-comment)",
9
+ "#000007": "var(--astro-code-token-keyword)",
10
+ "#000008": "var(--astro-code-token-parameter)",
11
+ "#000009": "var(--astro-code-token-function)",
12
+ "#000010": "var(--astro-code-token-string-expression)",
13
+ "#000011": "var(--astro-code-token-punctuation)",
14
+ "#000012": "var(--astro-code-token-link)"
15
+ };
16
+ const COLOR_REPLACEMENT_REGEX = new RegExp(
17
+ `(${Object.keys(ASTRO_COLOR_REPLACEMENTS).join("|")})`,
18
+ "g"
19
+ );
20
+ async function createShikiHighlighter({
21
+ langs = [],
22
+ theme = "github-dark",
23
+ experimentalThemes = {},
24
+ wrap = false
25
+ } = {}) {
26
+ const themes = experimentalThemes;
27
+ const highlighter = await getHighlighter({
28
+ langs: langs.length ? langs : Object.keys(bundledLanguages),
29
+ themes: Object.values(themes).length ? Object.values(themes) : [theme]
30
+ });
31
+ const loadedLanguages = highlighter.getLoadedLanguages();
32
+ return {
33
+ highlight(code, lang = "plaintext", options) {
34
+ if (lang !== "plaintext" && !loadedLanguages.includes(lang)) {
35
+ console.warn(`[Shiki] The language "${lang}" doesn't exist, falling back to "plaintext".`);
36
+ lang = "plaintext";
37
+ }
38
+ const themeOptions = Object.values(themes).length ? { themes } : { theme };
39
+ const inline = options?.inline ?? false;
40
+ return highlighter.codeToHtml(code, {
41
+ ...themeOptions,
42
+ lang,
43
+ transforms: {
44
+ pre(node) {
45
+ if (inline) {
46
+ node.tagName = "code";
47
+ }
48
+ const classValue = node.properties.class ?? "";
49
+ const styleValue = node.properties.style ?? "";
50
+ node.properties.class = classValue.replace(/shiki/g, "astro-code");
51
+ if (wrap === false) {
52
+ node.properties.style = styleValue + "; overflow-x: auto;";
53
+ } else if (wrap === true) {
54
+ node.properties.style = styleValue + "; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;";
55
+ }
56
+ },
57
+ line(node) {
58
+ if (lang === "diff") {
59
+ const innerSpanNode = node.children[0];
60
+ const innerSpanTextNode = innerSpanNode?.type === "element" && innerSpanNode.children?.[0];
61
+ if (innerSpanTextNode && innerSpanTextNode.type === "text") {
62
+ const start = innerSpanTextNode.value[0];
63
+ if (start === "+" || start === "-") {
64
+ innerSpanTextNode.value = innerSpanTextNode.value.slice(1);
65
+ innerSpanNode.children.unshift({
66
+ type: "element",
67
+ tagName: "span",
68
+ properties: { style: "user-select: none;" },
69
+ children: [{ type: "text", value: start }]
70
+ });
71
+ }
72
+ }
73
+ }
74
+ },
75
+ code(node) {
76
+ if (inline) {
77
+ return node.children[0];
78
+ }
79
+ },
80
+ root(node) {
81
+ if (Object.values(experimentalThemes).length) {
82
+ return;
83
+ }
84
+ const themeName = typeof theme === "string" ? theme : theme.name;
85
+ if (themeName === "css-variables") {
86
+ visit(node, "element", (child) => {
87
+ if (child.properties?.style) {
88
+ child.properties.style = replaceCssVariables(child.properties.style);
89
+ }
90
+ });
91
+ }
92
+ }
93
+ }
94
+ });
95
+ }
96
+ };
97
+ }
98
+ function replaceCssVariables(str) {
99
+ return str.replace(COLOR_REPLACEMENT_REGEX, (match) => ASTRO_COLOR_REPLACEMENTS[match] || match);
100
+ }
101
+ export {
102
+ createShikiHighlighter,
103
+ replaceCssVariables
104
+ };
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type * as hast from 'hast';
2
2
  import type * as mdast from 'mdast';
3
- import type { one as Handler, all as Handlers, Options as RemarkRehypeOptions } from 'remark-rehype';
3
+ import type { Options as RemarkRehypeOptions } from 'remark-rehype';
4
4
  import type { BuiltinTheme, LanguageRegistration, ThemeRegistration, ThemeRegistrationRaw } from 'shikiji';
5
5
  import type * as unified from 'unified';
6
6
  import type { VFile } from 'vfile';
@@ -12,10 +12,7 @@ export type RemarkPlugin<PluginParameters extends any[] = any[]> = unified.Plugi
12
12
  export type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlugin, any])[];
13
13
  export type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, hast.Root>;
14
14
  export type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
15
- export type RemarkRehype = Omit<RemarkRehypeOptions, 'handlers' | 'unknownHandler'> & {
16
- handlers?: typeof Handlers;
17
- handler?: typeof Handler;
18
- };
15
+ export type RemarkRehype = RemarkRehypeOptions;
19
16
  export interface ShikiConfig {
20
17
  langs?: LanguageRegistration[];
21
18
  theme?: BuiltinTheme | ThemeRegistration | ThemeRegistrationRaw;
@@ -23,7 +20,6 @@ export interface ShikiConfig {
23
20
  wrap?: boolean | null;
24
21
  }
25
22
  export interface AstroMarkdownOptions {
26
- drafts?: boolean;
27
23
  syntaxHighlight?: 'shiki' | 'prism' | false;
28
24
  shikiConfig?: ShikiConfig;
29
25
  remarkPlugins?: RemarkPlugins;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "3.4.0",
3
+ "version": "4.0.0-beta.0",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -20,33 +20,33 @@
20
20
  "dist"
21
21
  ],
22
22
  "peerDependencies": {
23
- "astro": "^3.0.0"
23
+ "astro": "^4.0.0-beta.0"
24
24
  },
25
25
  "dependencies": {
26
26
  "@astrojs/prism": "^3.0.0",
27
27
  "github-slugger": "^2.0.0",
28
- "import-meta-resolve": "^3.0.0",
28
+ "import-meta-resolve": "^4.0.0",
29
29
  "mdast-util-definitions": "^6.0.0",
30
- "rehype-raw": "^6.1.1",
31
- "rehype-stringify": "^9.0.4",
32
- "remark-gfm": "^3.0.1",
33
- "remark-parse": "^10.0.2",
34
- "remark-rehype": "^10.1.0",
30
+ "rehype-raw": "^7.0.0",
31
+ "rehype-stringify": "^10.0.0",
32
+ "remark-gfm": "^4.0.0",
33
+ "remark-parse": "^11.0.0",
34
+ "remark-rehype": "^11.0.0",
35
35
  "remark-smartypants": "^2.0.0",
36
- "shikiji": "^0.6.8",
37
- "unified": "^10.1.2",
38
- "unist-util-visit": "^4.1.2",
39
- "vfile": "^5.3.7"
36
+ "shikiji": "^0.6.13",
37
+ "unified": "^11.0.4",
38
+ "unist-util-visit": "^5.0.0",
39
+ "vfile": "^6.0.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@types/chai": "^4.3.5",
43
- "@types/estree": "^1.0.1",
44
- "@types/hast": "^2.3.5",
45
- "@types/mdast": "^3.0.12",
46
- "@types/mocha": "^10.0.1",
47
- "@types/unist": "^2.0.7",
42
+ "@types/chai": "^4.3.10",
43
+ "@types/estree": "^1.0.5",
44
+ "@types/hast": "^3.0.3",
45
+ "@types/mdast": "^4.0.3",
46
+ "@types/mocha": "^10.0.4",
47
+ "@types/unist": "^3.0.2",
48
48
  "chai": "^4.3.7",
49
- "mdast-util-mdx-expression": "^1.3.2",
49
+ "mdast-util-mdx-expression": "^2.0.0",
50
50
  "mocha": "^10.2.0",
51
51
  "astro-scripts": "0.0.14"
52
52
  },