Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 12 additions & 2 deletions packages/mdx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks amazing! You could pass {contents: mdx, path: opts.filepath}, but that's just nitpicking.

Looks great, lekker bezig!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yes, I didn't do it in this case since the mdx transpiler didn't work for me when path: undefined. I can still change it since that's probably something that won't often happen, but this one is more correct methinks.

Also, dankjewel :D! Leuk om zoveel Nederlanders hier te zien!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird! Well then it’s fine! En ja, klopt! 👍

if (opts.filepath) {
fileOpts.path = opts.filepath;
}

const { contents } = compiler.processSync(fileOpts)

return contents
}
Expand All @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions packages/mdx/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down