From e28dd81d8c53ceacfce39a8109f5f05ea8beb30c Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Mon, 18 Mar 2019 09:53:21 +0100 Subject: [PATCH] fix(gatsby-transformer-remark): Revert/remark sources from different sources (#12639) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description This change should happen in core and not in remark itself. I didn't think this through. I should have read @m-allanson more carefully: > Thanks @alexkirsz 👍 this is a really useful feature - so much so, that something like it should be added to Gatsby core instead of this being implemented separately across different plugins. > It'll be particularly useful for the new Gatsby themes functionality that @ChristopherBiscardi is working on. As part of the themes work there's going to be an issue or rfc soon™️ (next week or after) to figure out exactly how this could be added to Gatsby core. Once the rfc has been written I'll add a link here. > You can check out more info on Gatsby themes here and here. ## Related Issues #7512 --- packages/gatsby-transformer-remark/README.md | 5 - .../gatsby-transformer-remark/package.json | 1 + .../src/__tests__/extend-node.js | 50 +----- .../src/__tests__/on-node-create.js | 45 +----- .../src/__tests__/utils.js | 24 --- .../src/extend-node-type.js | 152 +++++++++--------- .../src/on-node-create.js | 18 +-- .../gatsby-transformer-remark/src/utils.js | 12 -- 8 files changed, 86 insertions(+), 221 deletions(-) delete mode 100644 packages/gatsby-transformer-remark/src/__tests__/utils.js delete mode 100644 packages/gatsby-transformer-remark/src/utils.js diff --git a/packages/gatsby-transformer-remark/README.md b/packages/gatsby-transformer-remark/README.md index 6bfff15147284..ffea63c965858 100644 --- a/packages/gatsby-transformer-remark/README.md +++ b/packages/gatsby-transformer-remark/README.md @@ -14,11 +14,6 @@ plugins: [ { resolve: `gatsby-transformer-remark`, options: { - - // Defaults to `() => true` - filter: node => node.sourceInstanceName === `blog`, - // Defaults to `MarkdownRemark` - type: `BlogPost`, // CommonMark mode (default: true) commonmark: true, // Footnotes mode (default: true) diff --git a/packages/gatsby-transformer-remark/package.json b/packages/gatsby-transformer-remark/package.json index 26b9bd1b658ff..b7ac5ff542fd5 100644 --- a/packages/gatsby-transformer-remark/package.json +++ b/packages/gatsby-transformer-remark/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "@babel/runtime": "^7.0.0", + "bluebird": "^3.5.0", "gray-matter": "^4.0.0", "hast-util-raw": "^4.0.0", "hast-util-to-html": "^4.0.0", diff --git a/packages/gatsby-transformer-remark/src/__tests__/extend-node.js b/packages/gatsby-transformer-remark/src/__tests__/extend-node.js index e6b41b17a228b..435fc8b8d1862 100644 --- a/packages/gatsby-transformer-remark/src/__tests__/extend-node.js +++ b/packages/gatsby-transformer-remark/src/__tests__/extend-node.js @@ -4,7 +4,7 @@ const { GraphQLList, GraphQLSchema, } = require(`gatsby/graphql`) -const { onCreateNode, setFieldsOnGraphQLNodeType } = require(`../gatsby-node`) +const { onCreateNode } = require(`../gatsby-node`) const { inferObjectStructureFromNodes, } = require(`../../../gatsby/src/schema/infer-graphql-type`) @@ -14,7 +14,7 @@ const extendNodeType = require(`../extend-node-type`) async function queryResult( nodes, fragment, - { typeName = `MarkdownRemark`, types = [] } = {}, + { types = [] } = {}, { additionalParameters = {}, pluginOptions = {} } ) { const inferredFields = inferObjectStructureFromNodes({ @@ -51,7 +51,7 @@ async function queryResult( name: `LISTNODE`, type: new GraphQLList( new GraphQLObjectType({ - name: typeName, + name: `MarkdownRemark`, fields: markdownRemarkFields, }) ), @@ -835,47 +835,3 @@ describe(`Headings are generated correctly from schema`, () => { } ) }) - -describe(`Adding fields to the GraphQL schema`, () => { - it(`only adds fields when the GraphQL type matches the provided type`, async () => { - const getNode = jest.fn() - const getNodesByType = jest.fn() - - expect( - setFieldsOnGraphQLNodeType({ - type: { name: `MarkdownRemark` }, - getNode, - getNodesByType, - }) - ).toBeInstanceOf(Promise) - - expect( - setFieldsOnGraphQLNodeType( - { type: { name: `MarkdownRemark` }, getNode, getNodesByType }, - { type: `MarkdownRemark` } - ) - ).toBeInstanceOf(Promise) - - expect( - setFieldsOnGraphQLNodeType( - { type: { name: `MarkdownRemark` }, getNode, getNodesByType }, - { type: `GatsbyTestType` } - ) - ).toEqual({}) - - expect( - setFieldsOnGraphQLNodeType( - { type: { name: `GatsbyTestType` }, getNode, getNodesByType }, - { type: `GatsbyTestType` } - ) - ).toBeInstanceOf(Promise) - - expect( - setFieldsOnGraphQLNodeType({ - type: { name: `GatsbyTestType` }, - getNode, - getNodesByType, - }) - ).toEqual({}) - }) -}) diff --git a/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js b/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js index e138c4255119b..62564aedcfa9e 100644 --- a/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js @@ -1,3 +1,4 @@ +const Promise = require(`bluebird`) const _ = require(`lodash`) const onCreateNode = require(`../on-node-create`) @@ -119,50 +120,6 @@ yadda yadda expect(parsed.frontmatter.date).toEqual(new Date(date).toJSON()) }) - - it(`Filters nodes with the given filter function, if provided`, async () => { - const content = `` - - node.content = content - node.sourceInstanceName = `gatsby-test-source` - - const createNode = jest.fn() - const createParentChildLink = jest.fn() - const actions = { createNode, createParentChildLink } - const createNodeId = jest.fn() - createNodeId.mockReturnValue(`uuid-from-gatsby`) - - await onCreateNode( - { - node, - loadNodeContent, - actions, - createNodeId, - }, - { - filter: node => - node.sourceInstanceName === `gatsby-other-test-source`, - } - ).then(() => { - expect(createNode).toHaveBeenCalledTimes(0) - expect(createParentChildLink).toHaveBeenCalledTimes(0) - }) - - await onCreateNode( - { - node, - loadNodeContent, - actions, - createNodeId, - }, - { - filter: node => node.sourceInstanceName === `gatsby-test-source`, - } - ).then(() => { - expect(createNode).toHaveBeenCalledTimes(1) - expect(createParentChildLink).toHaveBeenCalledTimes(1) - }) - }) }) describe(`process graphql correctly`, () => { diff --git a/packages/gatsby-transformer-remark/src/__tests__/utils.js b/packages/gatsby-transformer-remark/src/__tests__/utils.js deleted file mode 100644 index 909bf72ad3f0a..0000000000000 --- a/packages/gatsby-transformer-remark/src/__tests__/utils.js +++ /dev/null @@ -1,24 +0,0 @@ -const { eachPromise } = require(`../utils`) - -test(`eachPromise`, async () => { - const args = [`foo`, `bar`, `baz`] - - const cb = jest.fn() - - await eachPromise(args, arg => { - cb(`Start`, arg) - return new Promise(resolve => { - setTimeout(() => { - cb(`Finish`, arg) - resolve() - }, 200) - }) - }) - - expect(cb).toHaveBeenNthCalledWith(1, `Start`, `foo`) - expect(cb).toHaveBeenNthCalledWith(2, `Finish`, `foo`) - expect(cb).toHaveBeenNthCalledWith(3, `Start`, `bar`) - expect(cb).toHaveBeenNthCalledWith(4, `Finish`, `bar`) - expect(cb).toHaveBeenNthCalledWith(5, `Start`, `baz`) - expect(cb).toHaveBeenNthCalledWith(6, `Finish`, `baz`) -}) diff --git a/packages/gatsby-transformer-remark/src/extend-node-type.js b/packages/gatsby-transformer-remark/src/extend-node-type.js index e976a1c4f6af6..f02da84ab1e7e 100644 --- a/packages/gatsby-transformer-remark/src/extend-node-type.js +++ b/packages/gatsby-transformer-remark/src/extend-node-type.js @@ -16,6 +16,7 @@ const toHAST = require(`mdast-util-to-hast`) const hastToHTML = require(`hast-util-to-html`) const mdastToToc = require(`mdast-util-toc`) const mdastToString = require(`mdast-util-to-string`) +const Promise = require(`bluebird`) const unified = require(`unified`) const parse = require(`remark-parse`) const stringify = require(`remark-stringify`) @@ -29,7 +30,6 @@ const { cloneTreeUntil, findLastTextNode, } = require(`./hast-processing`) -const { eachPromise } = require(`./utils`) let fileNodes let pluginsCacheStr = `` @@ -69,59 +69,6 @@ const safeGetCache = ({ getCache, cache }) => id => { return getCache(id) } -const HeadingType = new GraphQLObjectType({ - name: `MarkdownHeading`, - fields: { - value: { - type: GraphQLString, - resolve(heading) { - return heading.value - }, - }, - depth: { - type: GraphQLInt, - resolve(heading) { - return heading.depth - }, - }, - }, -}) - -const HeadingLevels = new GraphQLEnumType({ - name: `HeadingLevels`, - values: { - h1: { value: 1 }, - h2: { value: 2 }, - h3: { value: 3 }, - h4: { value: 4 }, - h5: { value: 5 }, - h6: { value: 6 }, - }, -}) - -const ExcerptFormats = new GraphQLEnumType({ - name: `ExcerptFormats`, - values: { - PLAIN: { value: `plain` }, - HTML: { value: `html` }, - }, -}) - -const WordCountType = new GraphQLObjectType({ - name: `wordCount`, - fields: { - paragraphs: { - type: GraphQLInt, - }, - sentences: { - type: GraphQLInt, - }, - words: { - type: GraphQLInt, - }, - }, -}) - /** * Map that keeps track of generation of AST to not generate it multiple * times in parallel. @@ -141,31 +88,29 @@ module.exports = ( reporter, ...rest }, - { - type: typeName = `MarkdownRemark`, - plugins = [], - blocks, - commonmark = true, - footnotes = true, - gfm = true, - pedantic = true, - tableOfContents = { - heading: null, - maxDepth: 6, - }, - ...grayMatterOptions - } = {} + pluginOptions ) => { - if (type.name !== typeName) { + if (type.name !== `MarkdownRemark`) { return {} } - pluginsCacheStr = plugins.map(p => p.name).join(``) + pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``) pathPrefixCacheStr = pathPrefix || `` const getCache = safeGetCache({ cache, getCache: possibleGetCache }) return new Promise((resolve, reject) => { // Setup Remark. + const { + blocks, + commonmark = true, + footnotes = true, + gfm = true, + pedantic = true, + tableOfContents = { + heading: null, + maxDepth: 6, + }, + } = pluginOptions const tocOptions = tableOfContents const remarkOptions = { commonmark, @@ -178,7 +123,7 @@ module.exports = ( } let remark = new Remark().data(`settings`, remarkOptions) - for (let plugin of plugins) { + for (let plugin of pluginOptions.plugins) { const requiredPlugin = require(plugin.resolve) if (_.isFunction(requiredPlugin.setParserPlugins)) { for (let parserPlugin of requiredPlugin.setParserPlugins( @@ -222,8 +167,8 @@ module.exports = ( if (process.env.NODE_ENV !== `production` || !fileNodes) { fileNodes = getNodesByType(`File`) } - - await eachPromise(plugins, plugin => { + // Use Bluebird's Promise function "each" to run remark plugins serially. + await Promise.each(pluginOptions.plugins, plugin => { const requiredPlugin = require(plugin.resolve) if (_.isFunction(requiredPlugin.mutateSource)) { return requiredPlugin.mutateSource( @@ -290,8 +235,8 @@ module.exports = ( if (process.env.NODE_ENV !== `production` || !fileNodes) { fileNodes = getNodesByType(`File`) } - - await eachPromise(plugins, plugin => { + // Use Bluebird's Promise function "each" to run remark plugins serially. + await Promise.each(pluginOptions.plugins, plugin => { const requiredPlugin = require(plugin.resolve) if (_.isFunction(requiredPlugin)) { return requiredPlugin( @@ -503,6 +448,59 @@ module.exports = ( return text } + const HeadingType = new GraphQLObjectType({ + name: `MarkdownHeading`, + fields: { + value: { + type: GraphQLString, + resolve(heading) { + return heading.value + }, + }, + depth: { + type: GraphQLInt, + resolve(heading) { + return heading.depth + }, + }, + }, + }) + + const HeadingLevels = new GraphQLEnumType({ + name: `HeadingLevels`, + values: { + h1: { value: 1 }, + h2: { value: 2 }, + h3: { value: 3 }, + h4: { value: 4 }, + h5: { value: 5 }, + h6: { value: 6 }, + }, + }) + + const ExcerptFormats = new GraphQLEnumType({ + name: `ExcerptFormats`, + values: { + PLAIN: { value: `plain` }, + HTML: { value: `html` }, + }, + }) + + const WordCountType = new GraphQLObjectType({ + name: `wordCount`, + fields: { + paragraphs: { + type: GraphQLInt, + }, + sentences: { + type: GraphQLInt, + }, + words: { + type: GraphQLInt, + }, + }, + }) + return resolve({ html: { type: GraphQLString, @@ -540,7 +538,7 @@ module.exports = ( format, pruneLength, truncate, - excerptSeparator: grayMatterOptions.excerpt_separator, + excerptSeparator: pluginOptions.excerpt_separator, }) }, }, @@ -560,7 +558,7 @@ module.exports = ( return getExcerptAst(markdownNode, { pruneLength, truncate, - excerptSeparator: grayMatterOptions.excerpt_separator, + excerptSeparator: pluginOptions.excerpt_separator, }).then(ast => { const strippedAst = stripPosition(_.clone(ast), true) return hastReparseRaw(strippedAst) diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index e89def5f1cf15..c2937b08595da 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -4,20 +4,14 @@ const _ = require(`lodash`) module.exports = async function onCreateNode( { node, loadNodeContent, actions, createNodeId, reporter }, - { - plugins = null, - filter = () => true, - type = `MarkdownRemark`, - ...grayMatterOptions - } = {} + pluginOptions ) { const { createNode, createParentChildLink } = actions // We only care about markdown content. if ( - (node.internal.mediaType !== `text/markdown` && - node.internal.mediaType !== `text/x-markdown`) || - !filter(node) + node.internal.mediaType !== `text/markdown` && + node.internal.mediaType !== `text/x-markdown` ) { return {} } @@ -25,7 +19,7 @@ module.exports = async function onCreateNode( const content = await loadNodeContent(node) try { - let data = grayMatter(content, grayMatterOptions) + let data = grayMatter(content, pluginOptions) if (data.data) { data.data = _.mapValues(data.data, value => { @@ -37,12 +31,12 @@ module.exports = async function onCreateNode( } let markdownNode = { - id: createNodeId(`${node.id} >>> ${type}`), + id: createNodeId(`${node.id} >>> MarkdownRemark`), children: [], parent: node.id, internal: { content: data.content, - type, + type: `MarkdownRemark`, }, } diff --git a/packages/gatsby-transformer-remark/src/utils.js b/packages/gatsby-transformer-remark/src/utils.js deleted file mode 100644 index 39456e299c311..0000000000000 --- a/packages/gatsby-transformer-remark/src/utils.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * @template T - * @param {Array} input - * @param {(input: T) => Promise} iterator - * @return Promise - */ -exports.eachPromise = (input, iterator) => - input.reduce( - (accumulatorPromise, nextValue) => - accumulatorPromise.then(() => iterator(nextValue)), - Promise.resolve() - )