diff --git a/packages/loader/index.js b/packages/loader/index.js index 739292828..9acece025 100644 --- a/packages/loader/index.js +++ b/packages/loader/index.js @@ -3,9 +3,9 @@ const mdx = require('@mdx-js/mdx') module.exports = async function(content) { const callback = this.async() - const options = getOptions(this) + const options = Object.assign({}, getOptions(this), {filepath: this.resourcePath}); - const result = await mdx(content, options || {}) + const result = await mdx(content, options) const code = ` import React from 'react' diff --git a/packages/mdx/index.js b/packages/mdx/index.js index 1534185d8..659384858 100644 --- a/packages/mdx/index.js +++ b/packages/mdx/index.js @@ -103,7 +103,12 @@ function sync(mdx, options) { const opts = Object.assign({}, DEFAULT_OPTIONS, options) const compiler = createCompiler(opts) - const { contents } = compiler.processSync(mdx) + const fileOpts = { contents: mdx }; + if (opts.filepath) { + fileOpts.path = opts.filepath; + } + + const { contents } = compiler.processSync(fileOpts) return contents } @@ -112,7 +117,12 @@ async function compile(mdx, options = {}) { const opts = Object.assign({}, DEFAULT_OPTIONS, options) const compiler = createCompiler(opts) - const { contents } = await compiler.process(mdx) + const fileOpts = { contents: mdx }; + if (opts.filepath) { + fileOpts.path = opts.filepath; + } + + const { contents } = await compiler.process(fileOpts) return contents } diff --git a/packages/mdx/test/index.test.js b/packages/mdx/test/index.test.js index 70bd7a64e..4bd39ee49 100644 --- a/packages/mdx/test/index.test.js +++ b/packages/mdx/test/index.test.js @@ -122,6 +122,22 @@ test('Should await and render async plugins', async () => { expect(result).toMatch(/HELLO, WORLD!/) }) +test('Should process filepath and pass it to the plugins', async () => { + const result = await mdx(fixtureBlogPost, { + filepath: 'hello.mdx', + hastPlugins: [ + options => (tree, fileInfo) => { + expect(fileInfo.path).toBe('hello.mdx') + const headingNode = select('h1', tree) + const textNode = headingNode.children[0] + textNode.value = textNode.value.toUpperCase() + } + ] + }) + + expect(result).toMatch(/HELLO, WORLD!/) +}) + test( 'Should parse and render footnotes', async () => {