From 1d0c4c9bb01ee894673f5dbcc35644144c9e0857 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 19 May 2022 16:26:06 -0500 Subject: [PATCH] fix(#3264): ensure that remark files pass in file information --- .../astro/src/vite-plugin-markdown/index.ts | 2 +- packages/markdown/remark/package.json | 3 ++- packages/markdown/remark/src/index.ts | 6 +++-- packages/markdown/remark/src/types.ts | 2 ++ packages/markdown/remark/test/plugins.test.js | 26 +++++++++++++++++++ pnpm-lock.yaml | 2 ++ 6 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 packages/markdown/remark/test/plugins.test.js diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 25322072312f..232934650f4b 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -127,7 +127,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { // Turn HTML comments into JS comments markdownContent = markdownContent.replace(/<\s*!--([^-->]*)(.*?)-->/gs, (whole) => `{/*${whole}*/}`) - let renderResult = await renderMarkdown(markdownContent, renderOpts); + let renderResult = await renderMarkdown(markdownContent, { ...renderOpts, fileURL: fileUrl }); let { code: astroResult, metadata } = renderResult; const { layout = '', components = '', setup = '', ...content } = frontmatter; content.astro = metadata; diff --git a/packages/markdown/remark/package.json b/packages/markdown/remark/package.json index 75eb73852a1e..58be5e9d1abf 100644 --- a/packages/markdown/remark/package.json +++ b/packages/markdown/remark/package.json @@ -42,7 +42,8 @@ "shiki": "^0.10.1", "unified": "^10.1.2", "unist-util-map": "^3.1.1", - "unist-util-visit": "^4.1.0" + "unist-util-visit": "^4.1.0", + "vfile": "^5.3.2" }, "devDependencies": { "@types/chai": "^4.3.1", diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index fdba28bb2fb2..e93b9bd21e80 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -19,6 +19,7 @@ import markdownToHtml from 'remark-rehype'; import rehypeStringify from 'rehype-stringify'; import rehypeRaw from 'rehype-raw'; import Slugger from 'github-slugger'; +import { VFile } from 'vfile'; export * from './types.js'; @@ -35,7 +36,8 @@ export async function renderMarkdown( content: string, opts: MarkdownRenderingOptions = {} ): Promise { - let { mode = 'mdx', syntaxHighlight = 'shiki', shikiConfig = {}, remarkPlugins = [], rehypePlugins = [] } = opts; + let { fileURL, mode = 'mdx', syntaxHighlight = 'shiki', shikiConfig = {}, remarkPlugins = [], rehypePlugins = [] } = opts; + const input = new VFile({ value: content, path: fileURL }) const scopedClassName = opts.$?.scopedClassName; const isMDX = mode === 'mdx'; const { headers, rehypeCollectHeaders } = createCollectHeaders(); @@ -97,7 +99,7 @@ export async function renderMarkdown( const vfile = await parser .use([rehypeCollectHeaders]) .use(rehypeStringify, { allowDangerousHtml: true, allowParseErrors: true }) - .process(content); + .process(input); result = vfile.toString(); } catch (err) { console.error(err); diff --git a/packages/markdown/remark/src/types.ts b/packages/markdown/remark/src/types.ts index 8b3bbbac5257..af9778c9a045 100644 --- a/packages/markdown/remark/src/types.ts +++ b/packages/markdown/remark/src/types.ts @@ -35,6 +35,8 @@ export interface AstroMarkdownOptions { } export interface MarkdownRenderingOptions extends AstroMarkdownOptions { + /** @internal */ + fileURL?: URL; /** @internal */ $?: { scopedClassName: string | null; diff --git a/packages/markdown/remark/test/plugins.test.js b/packages/markdown/remark/test/plugins.test.js new file mode 100644 index 000000000000..4954047b51e6 --- /dev/null +++ b/packages/markdown/remark/test/plugins.test.js @@ -0,0 +1,26 @@ +import { renderMarkdown } from '../dist/index.js'; +import chai from 'chai'; + +import { fileURLToPath } from 'node:url'; + +describe('plugins', () => { + // https://github.com/withastro/astro/issues/3264 + it('should be able to get file path when passing fileURL', async () => { + let context; + await renderMarkdown(`test`, { + fileURL: new URL('virtual.md', import.meta.url), + remarkPlugins: [ + function () { + const transformer = (tree, file) => { + context = file; + }; + + return transformer; + } + ] + }); + + chai.expect(typeof context).to.equal('object'); + chai.expect(context.path).to.equal(fileURLToPath(new URL('virtual.md', import.meta.url))); + }); +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 00d233e338a9..a2b98c3d5f40 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1718,6 +1718,7 @@ importers: unified: ^10.1.2 unist-util-map: ^3.1.1 unist-util-visit: ^4.1.0 + vfile: ^5.3.2 dependencies: '@astrojs/prism': link:../../astro-prism assert: 2.0.0 @@ -1738,6 +1739,7 @@ importers: unified: 10.1.2 unist-util-map: 3.1.1 unist-util-visit: 4.1.0 + vfile: 5.3.2 devDependencies: '@types/chai': 4.3.1 '@types/github-slugger': 1.3.0