Skip to content

Commit

Permalink
chore(gatsby-plugin-mdx): refactor lodash and debug use out (#27968)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdz committed Nov 12, 2020
1 parent 84aae96 commit 3fda83b
Showing 1 changed file with 31 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const visit = require(`unist-util-visit`)
const _ = require(`lodash`)
const debug = require(`debug`)(`get-source-plugins-as-remark-plugins`)
const { interopDefault } = require(`./interop-default`)

// ensure only one `/` in new url
Expand All @@ -17,7 +15,6 @@ module.exports = async function getSourcePluginsAsRemarkPlugins({
pathPrefix,
...helpers
}) {
debug(`getSourcePluginsAsRemarkPlugins`)
let pathPlugin = undefined
if (pathPrefix) {
pathPlugin = () =>
Expand All @@ -38,42 +35,40 @@ module.exports = async function getSourcePluginsAsRemarkPlugins({
}

// return list of remarkPlugins
const userPlugins = gatsbyRemarkPlugins
.filter(plugin => {
if (_.isFunction(interopDefault(require(plugin.resolve)))) {
return true
} else {
debug(`userPlugins: filtering out`, plugin)
return false
}
})
.map(plugin => {
debug(`userPlugins: constructing remark plugin for `, plugin)
const requiredPlugin = interopDefault(require(plugin.resolve))
const wrappedPlugin = () =>
async function transformer(markdownAST) {
await requiredPlugin(
{
markdownAST,
markdownNode: mdxNode,
getNode,
getNodesByType,
get files() {
return getNodesByType(`File`)
},
pathPrefix,
reporter,
cache,
...helpers,
const userPluginsFiltered = gatsbyRemarkPlugins.filter(
plugin => typeof interopDefault(require(plugin.resolve)) === `function`
)

if (!userPluginsFiltered.length) {
return pathPlugin ? [pathPlugin] : []
}

const userPlugins = userPluginsFiltered.map(plugin => {
const requiredPlugin = interopDefault(require(plugin.resolve))
const wrappedPlugin = () =>
async function transformer(markdownAST) {
await requiredPlugin(
{
markdownAST,
markdownNode: mdxNode,
getNode,
getNodesByType,
get files() {
return getNodesByType(`File`)
},
plugin.options || {}
)
pathPrefix,
reporter,
cache,
...helpers,
},
plugin.options || {}
)

return markdownAST
}
return markdownAST
}

return [wrappedPlugin, {}]
})
return [wrappedPlugin, {}]
})

if (pathPlugin) {
return [pathPlugin, ...userPlugins]
Expand Down

0 comments on commit 3fda83b

Please sign in to comment.