@astrojs/markdown-remark 0.14.0 → 0.14.1

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,5 +1,5 @@
1
- @astrojs/markdown-remark:build: cache hit, replaying output 6f4b2d3bfc1168f3
1
+ @astrojs/markdown-remark:build: cache hit, replaying output f2df71f430c1c421
2
2
  @astrojs/markdown-remark:build: 
3
- @astrojs/markdown-remark:build: > @astrojs/markdown-remark@0.14.0 build /home/runner/work/astro/astro/packages/markdown/remark
3
+ @astrojs/markdown-remark:build: > @astrojs/markdown-remark@0.14.1 build /home/runner/work/astro/astro/packages/markdown/remark
4
4
  @astrojs/markdown-remark:build: > astro-scripts build "src/**/*.ts" && tsc -p tsconfig.json
5
5
  @astrojs/markdown-remark:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @astrojs/markdown-remark
2
2
 
3
+ ## 0.14.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4176](https://github.com/withastro/astro/pull/4176) [`2675b8633`](https://github.com/withastro/astro/commit/2675b8633c5d5c45b237ec87940d5eaf1bfb1b4b) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support frontmatter injection for MD and MDX using remark and rehype plugins
8
+
9
+ * [#4137](https://github.com/withastro/astro/pull/4137) [`471c6f784`](https://github.com/withastro/astro/commit/471c6f784e21399676c8b2002665ffdf83a1c59e) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Speed up internal markdown builds with new vite-plugin markdown
10
+
11
+ - [#4169](https://github.com/withastro/astro/pull/4169) [`16034f0dd`](https://github.com/withastro/astro/commit/16034f0dd5b3683e9e022dbd413e85bd18d2b031) Thanks [@hippotastic](https://github.com/hippotastic)! - Fix double-escaping of non-highlighted code blocks in Astro-flavored markdown
12
+
3
13
  ## 0.14.0
4
14
 
5
15
  ### Minor Changes
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import rehypeExpressions from "./rehype-expressions.js";
5
5
  import rehypeIslands from "./rehype-islands.js";
6
6
  import rehypeJsx from "./rehype-jsx.js";
7
7
  import remarkEscape from "./remark-escape.js";
8
+ import { remarkInitializeAstroData } from "./remark-initialize-astro-data.js";
8
9
  import remarkMarkAndUnravel from "./remark-mark-and-unravel.js";
9
10
  import remarkMdxish from "./remark-mdxish.js";
10
11
  import remarkPrism from "./remark-prism.js";
@@ -33,7 +34,7 @@ async function renderMarkdown(content, opts) {
33
34
  const input = new VFile({ value: content, path: fileURL });
34
35
  const scopedClassName = (_a = opts.$) == null ? void 0 : _a.scopedClassName;
35
36
  const { headings, rehypeCollectHeadings } = createCollectHeadings();
36
- let parser = unified().use(markdown).use(isAstroFlavoredMd ? [remarkMdxish, remarkMarkAndUnravel, remarkUnwrap, remarkEscape] : []);
37
+ let parser = unified().use(markdown).use(remarkInitializeAstroData).use(isAstroFlavoredMd ? [remarkMdxish, remarkMarkAndUnravel, remarkUnwrap, remarkEscape] : []);
37
38
  if (remarkPlugins.length === 0 && rehypePlugins.length === 0) {
38
39
  remarkPlugins = [...DEFAULT_REMARK_PLUGINS];
39
40
  rehypePlugins = [...DEFAULT_REHYPE_PLUGINS];
@@ -72,18 +73,18 @@ async function renderMarkdown(content, opts) {
72
73
  parser.use(
73
74
  isAstroFlavoredMd ? [rehypeJsx, rehypeExpressions, rehypeEscape, rehypeIslands, rehypeCollectHeadings] : [rehypeCollectHeadings, rehypeRaw]
74
75
  ).use(rehypeStringify, { allowDangerousHtml: true });
75
- let result;
76
+ let vfile;
76
77
  try {
77
- const vfile = await parser.process(input);
78
- result = vfile.toString();
78
+ vfile = await parser.process(input);
79
79
  } catch (err) {
80
80
  err = prefixError(err, `Failed to parse Markdown file "${input.path}"`);
81
81
  console.error(err);
82
82
  throw err;
83
83
  }
84
84
  return {
85
- metadata: { headings, source: content, html: result.toString() },
86
- code: result.toString()
85
+ metadata: { headings, source: content, html: String(vfile.value) },
86
+ code: String(vfile.value),
87
+ vfile
87
88
  };
88
89
  }
89
90
  function prefixError(err, prefix) {
@@ -1,4 +1,4 @@
1
- import { visit } from "unist-util-visit";
1
+ import { SKIP, visit } from "unist-util-visit";
2
2
  function escapeEntities(value) {
3
3
  return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
4
4
  }
@@ -10,8 +10,8 @@ function rehypeEscape() {
10
10
  visit(el, "raw", (raw) => {
11
11
  raw.value = escapeEntities(raw.value);
12
12
  });
13
+ return SKIP;
13
14
  }
14
- return el;
15
15
  });
16
16
  };
17
17
  }
@@ -0,0 +1,2 @@
1
+ import type { VFile } from 'vfile';
2
+ export declare function remarkInitializeAstroData(): (tree: any, vfile: VFile) => void;
@@ -0,0 +1,10 @@
1
+ function remarkInitializeAstroData() {
2
+ return function(tree, vfile) {
3
+ if (!vfile.data.astro) {
4
+ vfile.data.astro = { frontmatter: {} };
5
+ }
6
+ };
7
+ }
8
+ export {
9
+ remarkInitializeAstroData
10
+ };
package/dist/types.d.ts CHANGED
@@ -2,6 +2,7 @@ import type * as hast from 'hast';
2
2
  import type * as mdast from 'mdast';
3
3
  import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
4
4
  import type * as unified from 'unified';
5
+ import type { VFile } from 'vfile';
5
6
  export type { Node } from 'unist';
6
7
  export declare type RemarkPlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, mdast.Root>;
7
8
  export declare type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlugin, any])[];
@@ -41,5 +42,6 @@ export interface MarkdownMetadata {
41
42
  }
42
43
  export interface MarkdownRenderingResult {
43
44
  metadata: MarkdownMetadata;
45
+ vfile: VFile;
44
46
  code: string;
45
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "0.14.0",
3
+ "version": "0.14.1",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ import rehypeExpressions from './rehype-expressions.js';
7
7
  import rehypeIslands from './rehype-islands.js';
8
8
  import rehypeJsx from './rehype-jsx.js';
9
9
  import remarkEscape from './remark-escape.js';
10
+ import { remarkInitializeAstroData } from './remark-initialize-astro-data.js';
10
11
  import remarkMarkAndUnravel from './remark-mark-and-unravel.js';
11
12
  import remarkMdxish from './remark-mdxish.js';
12
13
  import remarkPrism from './remark-prism.js';
@@ -45,6 +46,7 @@ export async function renderMarkdown(
45
46
 
46
47
  let parser = unified()
47
48
  .use(markdown)
49
+ .use(remarkInitializeAstroData)
48
50
  .use(isAstroFlavoredMd ? [remarkMdxish, remarkMarkAndUnravel, remarkUnwrap, remarkEscape] : []);
49
51
 
50
52
  if (remarkPlugins.length === 0 && rehypePlugins.length === 0) {
@@ -99,10 +101,9 @@ export async function renderMarkdown(
99
101
  )
100
102
  .use(rehypeStringify, { allowDangerousHtml: true });
101
103
 
102
- let result: string;
104
+ let vfile: VFile;
103
105
  try {
104
- const vfile = await parser.process(input);
105
- result = vfile.toString();
106
+ vfile = await parser.process(input);
106
107
  } catch (err) {
107
108
  // Ensure that the error message contains the input filename
108
109
  // to make it easier for the user to fix the issue
@@ -113,8 +114,9 @@ export async function renderMarkdown(
113
114
  }
114
115
 
115
116
  return {
116
- metadata: { headings, source: content, html: result.toString() },
117
- code: result.toString(),
117
+ metadata: { headings, source: content, html: String(vfile.value) },
118
+ code: String(vfile.value),
119
+ vfile,
118
120
  };
119
121
  }
120
122
 
@@ -1,4 +1,4 @@
1
- import { visit } from 'unist-util-visit';
1
+ import { SKIP, visit } from 'unist-util-visit';
2
2
 
3
3
  export function escapeEntities(value: string): string {
4
4
  return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
@@ -14,8 +14,9 @@ export default function rehypeEscape(): any {
14
14
  visit(el, 'raw', (raw) => {
15
15
  raw.value = escapeEntities(raw.value);
16
16
  });
17
+ // Do not visit children to prevent double escaping
18
+ return SKIP;
17
19
  }
18
- return el;
19
20
  });
20
21
  };
21
22
  }
@@ -2,7 +2,7 @@ import type { Literal } from 'unist';
2
2
  import { visit } from 'unist-util-visit';
3
3
 
4
4
  // In code blocks, this removes the JS comment wrapper added to
5
- // HTML comments by vite-plugin-markdown.
5
+ // HTML comments by vite-plugin-markdown-legacy.
6
6
  export default function remarkEscape() {
7
7
  return (tree: any) => {
8
8
  visit(tree, 'code', removeCommentWrapper);
@@ -0,0 +1,9 @@
1
+ import type { VFile } from 'vfile';
2
+
3
+ export function remarkInitializeAstroData() {
4
+ return function (tree: any, vfile: VFile) {
5
+ if (!vfile.data.astro) {
6
+ vfile.data.astro = { frontmatter: {} };
7
+ }
8
+ };
9
+ }
package/src/types.ts CHANGED
@@ -2,6 +2,7 @@ import type * as hast from 'hast';
2
2
  import type * as mdast from 'mdast';
3
3
  import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
4
4
  import type * as unified from 'unified';
5
+ import type { VFile } from 'vfile';
5
6
 
6
7
  export type { Node } from 'unist';
7
8
 
@@ -58,5 +59,6 @@ export interface MarkdownMetadata {
58
59
 
59
60
  export interface MarkdownRenderingResult {
60
61
  metadata: MarkdownMetadata;
62
+ vfile: VFile;
61
63
  code: string;
62
64
  }
@@ -2,11 +2,22 @@ import { renderMarkdown } from '../dist/index.js';
2
2
  import { expect } from 'chai';
3
3
 
4
4
  describe('entities', () => {
5
- const renderAstroMd = (text) => renderMarkdown(text, { isAstroFlavoredMd: false });
6
-
7
- it('should not unescape entities', async () => {
8
- const { code } = await renderAstroMd(`&lt;i&gt;This should NOT be italic&lt;/i&gt;`);
5
+ it('should not unescape entities in regular Markdown', async () => {
6
+ const { code } = await renderMarkdown(`&lt;i&gt;This should NOT be italic&lt;/i&gt;`, {
7
+ isAstroFlavoredMd: false,
8
+ });
9
9
 
10
10
  expect(code).to.equal(`<p>&#x3C;i>This should NOT be italic&#x3C;/i></p>`);
11
11
  });
12
+
13
+ it('should not escape entities in code blocks twice in Astro-flavored markdown', async () => {
14
+ const { code } = await renderMarkdown(`\`\`\`astro\n<h1>{x && x.name || ''}!</h1>\n\`\`\``, {
15
+ isAstroFlavoredMd: true,
16
+ syntaxHighlight: false,
17
+ });
18
+
19
+ expect(code).to.equal(
20
+ `<pre is:raw><code class="language-astro">&lt;h1&gt;{x &amp;&amp; x.name || ''}!&lt;/h1&gt;\n</code></pre>`
21
+ );
22
+ });
12
23
  });