@astrojs/markdown-remark 3.0.0 → 3.1.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/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import type { AstroMarkdownOptions, MarkdownRenderingOptions, MarkdownRenderingResult } from './types';
2
2
  export { rehypeHeadingIds } from './rehype-collect-headings.js';
3
3
  export { remarkCollectImages } from './remark-collect-images.js';
4
+ export { remarkPrism } from './remark-prism.js';
5
+ export { remarkShiki } from './remark-shiki.js';
4
6
  export * from './types.js';
5
7
  export declare const markdownConfigDefaults: Omit<Required<AstroMarkdownOptions>, 'drafts'>;
6
8
  /** Shared utility for rendering markdown */
package/dist/index.js CHANGED
@@ -2,9 +2,8 @@ import { toRemarkInitializeAstroData } from "./frontmatter-injection.js";
2
2
  import { loadPlugins } from "./load-plugins.js";
3
3
  import { rehypeHeadingIds } from "./rehype-collect-headings.js";
4
4
  import { remarkCollectImages } from "./remark-collect-images.js";
5
- import remarkPrism from "./remark-prism.js";
6
- import scopedStyles from "./remark-scoped-styles.js";
7
- import remarkShiki from "./remark-shiki.js";
5
+ import { remarkPrism } from "./remark-prism.js";
6
+ import { remarkShiki } from "./remark-shiki.js";
8
7
  import rehypeRaw from "rehype-raw";
9
8
  import rehypeStringify from "rehype-stringify";
10
9
  import remarkGfm from "remark-gfm";
@@ -16,6 +15,8 @@ import { VFile } from "vfile";
16
15
  import { rehypeImages } from "./rehype-images.js";
17
16
  import { rehypeHeadingIds as rehypeHeadingIds2 } from "./rehype-collect-headings.js";
18
17
  import { remarkCollectImages as remarkCollectImages2 } from "./remark-collect-images.js";
18
+ import { remarkPrism as remarkPrism2 } from "./remark-prism.js";
19
+ import { remarkShiki as remarkShiki2 } from "./remark-shiki.js";
19
20
  export * from "./types.js";
20
21
  const markdownConfigDefaults = {
21
22
  syntaxHighlight: "shiki",
@@ -44,7 +45,6 @@ async function renderMarkdown(content, opts) {
44
45
  frontmatter: userFrontmatter = {}
45
46
  } = opts;
46
47
  const input = new VFile({ value: content, path: fileURL });
47
- const scopedClassName = opts.$?.scopedClassName;
48
48
  let parser = unified().use(markdown).use(toRemarkInitializeAstroData({ userFrontmatter })).use([]);
49
49
  if (!isPerformanceBenchmark && gfm) {
50
50
  if (gfm) {
@@ -60,15 +60,12 @@ async function renderMarkdown(content, opts) {
60
60
  parser.use([[plugin, pluginOpts]]);
61
61
  });
62
62
  if (!isPerformanceBenchmark) {
63
- if (scopedClassName) {
64
- parser.use([scopedStyles(scopedClassName)]);
65
- }
66
63
  if (syntaxHighlight === "shiki") {
67
- parser.use([await remarkShiki(shikiConfig, scopedClassName)]);
64
+ parser.use(remarkShiki, shikiConfig);
68
65
  } else if (syntaxHighlight === "prism") {
69
- parser.use([remarkPrism(scopedClassName)]);
66
+ parser.use(remarkPrism);
70
67
  }
71
- parser.use([remarkCollectImages]);
68
+ parser.use(remarkCollectImages);
72
69
  }
73
70
  parser.use([
74
71
  [
@@ -124,5 +121,7 @@ export {
124
121
  markdownConfigDefaults,
125
122
  rehypeHeadingIds2 as rehypeHeadingIds,
126
123
  remarkCollectImages2 as remarkCollectImages,
124
+ remarkPrism2 as remarkPrism,
125
+ remarkShiki2 as remarkShiki,
127
126
  renderMarkdown
128
127
  };
@@ -1,3 +1,2 @@
1
- type MaybeString = string | null | undefined;
2
- declare function plugin(className: MaybeString): () => (tree: any) => void;
3
- export default plugin;
1
+ import type { RemarkPlugin } from './types.js';
2
+ export declare function remarkPrism(): ReturnType<RemarkPlugin>;
@@ -1,27 +1,19 @@
1
1
  import { runHighlighterWithAstro } from "@astrojs/prism/dist/highlighter";
2
2
  import { visit } from "unist-util-visit";
3
- function transformer(className) {
3
+ function remarkPrism() {
4
4
  return function(tree) {
5
- const visitor = (node) => {
5
+ visit(tree, "code", (node) => {
6
6
  let { lang, value } = node;
7
7
  node.type = "html";
8
8
  let { html, classLanguage } = runHighlighterWithAstro(lang, value);
9
9
  let classes = [classLanguage];
10
- if (className) {
11
- classes.push(className);
12
- }
13
10
  node.value = `<pre class="${classes.join(
14
11
  " "
15
12
  )}"><code is:raw class="${classLanguage}">${html}</code></pre>`;
16
13
  return node;
17
- };
18
- return visit(tree, "code", visitor);
14
+ });
19
15
  };
20
16
  }
21
- function plugin(className) {
22
- return transformer.bind(null, className);
23
- }
24
- var remark_prism_default = plugin;
25
17
  export {
26
- remark_prism_default as default
18
+ remarkPrism
27
19
  };
@@ -1,3 +1,2 @@
1
- import type { ShikiConfig } from './types.js';
2
- declare const remarkShiki: ({ langs, theme, wrap }: ShikiConfig, scopedClassName?: string | null) => Promise<() => (tree: any) => void>;
3
- export default remarkShiki;
1
+ import type { RemarkPlugin, ShikiConfig } from './types.js';
2
+ export declare function remarkShiki({ langs, theme, wrap, }?: ShikiConfig): ReturnType<RemarkPlugin>;
@@ -1,7 +1,11 @@
1
1
  import { getHighlighter } from "shiki";
2
2
  import { visit } from "unist-util-visit";
3
3
  const highlighterCacheAsync = /* @__PURE__ */ new Map();
4
- const remarkShiki = async ({ langs = [], theme = "github-dark", wrap = false }, scopedClassName) => {
4
+ function remarkShiki({
5
+ langs = [],
6
+ theme = "github-dark",
7
+ wrap = false
8
+ } = {}) {
5
9
  const cacheID = typeof theme === "string" ? theme : theme.name;
6
10
  let highlighterAsync = highlighterCacheAsync.get(cacheID);
7
11
  if (!highlighterAsync) {
@@ -23,11 +27,14 @@ const remarkShiki = async ({ langs = [], theme = "github-dark", wrap = false },
23
27
  });
24
28
  highlighterCacheAsync.set(cacheID, highlighterAsync);
25
29
  }
26
- const highlighter = await highlighterAsync;
27
- for (const lang of langs) {
28
- await highlighter.loadLanguage(lang);
29
- }
30
- return () => (tree) => {
30
+ let highlighter;
31
+ return async (tree) => {
32
+ if (!highlighter) {
33
+ highlighter = await highlighterAsync;
34
+ for (const lang of langs) {
35
+ await highlighter.loadLanguage(lang);
36
+ }
37
+ }
31
38
  visit(tree, "code", (node) => {
32
39
  let lang;
33
40
  if (typeof node.lang === "string") {
@@ -42,10 +49,7 @@ const remarkShiki = async ({ langs = [], theme = "github-dark", wrap = false },
42
49
  lang = "plaintext";
43
50
  }
44
51
  let html = highlighter.codeToHtml(node.value, { lang });
45
- html = html.replace(
46
- /<pre class="(.*?)shiki(.*?)"/,
47
- `<pre is:raw class="$1astro-code$2${scopedClassName ? " " + scopedClassName : ""}"`
48
- );
52
+ html = html.replace(/<pre class="(.*?)shiki(.*?)"/, `<pre is:raw class="$1astro-code$2"`);
49
53
  if (node.lang === "diff") {
50
54
  html = html.replace(
51
55
  /<span class="line"><span style="(.*?)">([\+|\-])/g,
@@ -60,16 +64,12 @@ const remarkShiki = async ({ langs = [], theme = "github-dark", wrap = false },
60
64
  'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
61
65
  );
62
66
  }
63
- if (scopedClassName) {
64
- html = html.replace(/\<span class="line"\>/g, `<span class="line ${scopedClassName}"`);
65
- }
66
67
  node.type = "html";
67
68
  node.value = html;
68
69
  node.children = [];
69
70
  });
70
71
  };
71
- };
72
- var remark_shiki_default = remarkShiki;
72
+ }
73
73
  export {
74
- remark_shiki_default as default
74
+ remarkShiki
75
75
  };
package/dist/types.d.ts CHANGED
@@ -40,10 +40,6 @@ export interface ImageMetadata {
40
40
  export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
41
41
  /** @internal */
42
42
  fileURL?: URL;
43
- /** @internal */
44
- $?: {
45
- scopedClassName: string | null;
46
- };
47
43
  /** Used for frontmatter injection plugins */
48
44
  frontmatter?: Record<string, any>;
49
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -20,7 +20,7 @@
20
20
  "dist"
21
21
  ],
22
22
  "peerDependencies": {
23
- "astro": "^3.0.0"
23
+ "astro": "^3.0.11"
24
24
  },
25
25
  "dependencies": {
26
26
  "@astrojs/prism": "^3.0.0",
@@ -1,2 +0,0 @@
1
- /** */
2
- export default function scopedStyles(className: string): () => (tree: any) => void;
@@ -1,18 +0,0 @@
1
- import { visit } from "unist-util-visit";
2
- const noVisit = /* @__PURE__ */ new Set(["root", "html", "text"]);
3
- function scopedStyles(className) {
4
- const visitor = (node) => {
5
- if (noVisit.has(node.type))
6
- return;
7
- const { data } = node;
8
- let currentClassName = data?.hProperties?.class ?? "";
9
- node.data = node.data || {};
10
- node.data.hProperties = node.data.hProperties || {};
11
- node.data.hProperties.class = `${className} ${currentClassName}`.trim();
12
- return node;
13
- };
14
- return () => (tree) => visit(tree, visitor);
15
- }
16
- export {
17
- scopedStyles as default
18
- };