Skip to content

Commit

Permalink
fix(#3264): ensure that remark files pass in file information
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Moore committed May 24, 2022
1 parent 81e07c7 commit 1d0c4c9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/markdown/remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -35,7 +36,8 @@ export async function renderMarkdown(
content: string,
opts: MarkdownRenderingOptions = {}
): Promise<MarkdownRenderingResult> {
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();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface AstroMarkdownOptions {
}

export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
/** @internal */
fileURL?: URL;
/** @internal */
$?: {
scopedClassName: string | null;
Expand Down
26 changes: 26 additions & 0 deletions packages/markdown/remark/test/plugins.test.js
Original file line number Diff line number Diff line change
@@ -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)));
});
})
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1d0c4c9

Please sign in to comment.