gatsby-plugin-mdx-v1
Version:
MDX integration for Gatsby (MDX v1)
78 lines (69 loc) • 1.93 kB
JavaScript
const visit = require(`unist-util-visit`)
// ensure only one `/` in new url
const withPathPrefix = (url, pathPrefix) =>
(pathPrefix + url).replace(/\/\//, `/`)
module.exports = async function getSourcePluginsAsRemarkPlugins({
gatsbyRemarkPlugins,
mdxNode,
getNode,
getNodesByType,
reporter,
cache,
pathPrefix,
...helpers
}) {
let pathPlugin = undefined
if (pathPrefix) {
pathPlugin = () =>
async function transformer(markdownAST) {
// Ensure relative links include `pathPrefix`
visit(markdownAST, [`link`, `definition`], node => {
if (
node.url &&
node.url.startsWith(`/`) &&
!node.url.startsWith(`//`)
) {
// TODO: where does withPathPrefix
node.url = withPathPrefix(node.url, pathPrefix)
}
})
return markdownAST
}
}
// return list of remarkPlugins
const userPluginsFiltered = gatsbyRemarkPlugins.filter(
plugin => typeof plugin.module === `function`
)
if (!userPluginsFiltered.length) {
return pathPlugin ? [pathPlugin] : []
}
const userPlugins = userPluginsFiltered.map(plugin => {
const requiredPlugin = plugin.module
const wrappedPlugin = () =>
async function transformer(markdownAST) {
await requiredPlugin(
{
markdownAST,
markdownNode: mdxNode,
getNode,
getNodesByType,
get files() {
return getNodesByType(`File`)
},
pathPrefix,
reporter,
cache,
...helpers,
},
plugin.pluginOptions || {}
)
return markdownAST
}
return [wrappedPlugin, {}]
})
if (pathPlugin) {
return [pathPlugin, ...userPlugins]
} else {
return [...userPlugins]
}
}