Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-transformer-remark): correctly pass cache to sub plugins #10892

Merged
merged 8 commits into from
Jan 8, 2019
Next Next commit
fix(gatsby-transformer-remark): correctly pass cache to sub plugins
  • Loading branch information
DSchau committed Jan 7, 2019
commit d65def5c84503dbe3d680523de7a54fa5d5d8a00
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ date: 2018-12-14
This is a truly meaningful blog post

<h2 data-testid="sub-title">%SUB_TITLE%</h2>

<h2 data-testid="gatsby-remark-subcache-value"></h2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This e2e test validates that the cache structure
* is unique per plugin (even sub-plugins)
* and can interact between Gatsby lifecycles and a plugin
*/
describe(`sub-plugin caching`, () => {
beforeEach(() => {
cy.visit(`/2018-12-14-hello-world/`).waitForAPI(`onRouteUpdate`)
})

it(`has access to custom sub-plugin cache`, () => {
cy.getTestElement(`gatsby-remark-subcache-value`)
.invoke(`text`)
.should(`eq`, `Hello World`)
})
})
2 changes: 1 addition & 1 deletion e2e-tests/development-runtime/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [],
plugins: [`gatsby-remark-subcache`],
},
},
`gatsby-plugin-sharp`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.id = `gatsby-remark-subcache-value`
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { id } = require(`./constants`)

exports.onPreBootstrap = async ({ cache }) => {
await cache.set(id, `Hello World`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const visit = require(`unist-util-visit`)
const { id } = require(`./constants`)

module.exports = function remarkPlugin({ cache, markdownAST }) {
visit(markdownAST, `html`, async node => {
if (node.value.match(id)) {
const value = await cache.get(id)
node.value = node.value.replace(/></, () => `>${value}<`)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "gatsby-remark-subcache"
}
19 changes: 16 additions & 3 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ const withPathPrefix = (url, pathPrefix) =>
const ASTPromiseMap = new Map()

module.exports = (
{ type, store, pathPrefix, getNode, getNodesByType, cache, reporter },
{
type,
pathPrefix,
getNode,
getNodesByType,
cache,
getCache,
reporter,
...rest
},
pluginOptions
) => {
if (type.name !== `MarkdownRemark`) {
Expand Down Expand Up @@ -150,7 +159,9 @@ module.exports = (
files: fileNodes,
getNode,
reporter,
cache,
cache: getCache(plugin.name),
getCache,
...rest,
},
plugin.pluginOptions
)
Expand Down Expand Up @@ -218,7 +229,9 @@ module.exports = (
files: fileNodes,
pathPrefix,
reporter,
cache,
cache: getCache(plugin.name),
getCache,
...rest,
},
plugin.pluginOptions
)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const crypto = require(`crypto`)
const _ = require(`lodash`)

module.exports = async function onCreateNode(
{ node, getNode, loadNodeContent, actions, createNodeId, reporter },
{ node, loadNodeContent, actions, createNodeId, reporter },
pluginOptions
) {
const { createNode, createParentChildLink } = actions
Expand Down