Skip to content

Commit 3b2740e

Browse files
committed
Revert "feat(gatsby-transformer-remark): Allow for multiple different remark sources (#7512)"
This reverts commit 95155e0.
1 parent 59bc2b0 commit 3b2740e

File tree

6 files changed

+85
-198
lines changed

6 files changed

+85
-198
lines changed

packages/gatsby-transformer-remark/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ plugins: [
1414
{
1515
resolve: `gatsby-transformer-remark`,
1616
options: {
17-
18-
// Defaults to `() => true`
19-
filter: node => node.sourceInstanceName === `blog`,
20-
// Defaults to `MarkdownRemark`
21-
type: `BlogPost`,
2217
// CommonMark mode (default: true)
2318
commonmark: true,
2419
// Footnotes mode (default: true)

packages/gatsby-transformer-remark/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"@babel/runtime": "^7.0.0",
11+
"bluebird": "^3.5.0",
1112
"gray-matter": "^4.0.0",
1213
"hast-util-raw": "^4.0.0",
1314
"hast-util-to-html": "^4.0.0",

packages/gatsby-transformer-remark/src/__tests__/extend-node.js

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
GraphQLList,
55
GraphQLSchema,
66
} = require(`gatsby/graphql`)
7-
const { onCreateNode, setFieldsOnGraphQLNodeType } = require(`../gatsby-node`)
7+
const { onCreateNode } = require(`../gatsby-node`)
88
const {
99
inferObjectStructureFromNodes,
1010
} = require(`../../../gatsby/src/schema/infer-graphql-type`)
@@ -14,7 +14,7 @@ const extendNodeType = require(`../extend-node-type`)
1414
async function queryResult(
1515
nodes,
1616
fragment,
17-
{ typeName = `MarkdownRemark`, types = [] } = {},
17+
{ types = [] } = {},
1818
{ additionalParameters = {}, pluginOptions = {} }
1919
) {
2020
const inferredFields = inferObjectStructureFromNodes({
@@ -51,7 +51,7 @@ async function queryResult(
5151
name: `LISTNODE`,
5252
type: new GraphQLList(
5353
new GraphQLObjectType({
54-
name: typeName,
54+
name: `MarkdownRemark`,
5555
fields: markdownRemarkFields,
5656
})
5757
),
@@ -835,47 +835,3 @@ describe(`Headings are generated correctly from schema`, () => {
835835
}
836836
)
837837
})
838-
839-
describe(`Adding fields to the GraphQL schema`, () => {
840-
it(`only adds fields when the GraphQL type matches the provided type`, async () => {
841-
const getNode = jest.fn()
842-
const getNodesByType = jest.fn()
843-
844-
expect(
845-
setFieldsOnGraphQLNodeType({
846-
type: { name: `MarkdownRemark` },
847-
getNode,
848-
getNodesByType,
849-
})
850-
).toBeInstanceOf(Promise)
851-
852-
expect(
853-
setFieldsOnGraphQLNodeType(
854-
{ type: { name: `MarkdownRemark` }, getNode, getNodesByType },
855-
{ type: `MarkdownRemark` }
856-
)
857-
).toBeInstanceOf(Promise)
858-
859-
expect(
860-
setFieldsOnGraphQLNodeType(
861-
{ type: { name: `MarkdownRemark` }, getNode, getNodesByType },
862-
{ type: `GatsbyTestType` }
863-
)
864-
).toEqual({})
865-
866-
expect(
867-
setFieldsOnGraphQLNodeType(
868-
{ type: { name: `GatsbyTestType` }, getNode, getNodesByType },
869-
{ type: `GatsbyTestType` }
870-
)
871-
).toBeInstanceOf(Promise)
872-
873-
expect(
874-
setFieldsOnGraphQLNodeType({
875-
type: { name: `GatsbyTestType` },
876-
getNode,
877-
getNodesByType,
878-
})
879-
).toEqual({})
880-
})
881-
})

packages/gatsby-transformer-remark/src/__tests__/on-node-create.js

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const Promise = require(`bluebird`)
12
const _ = require(`lodash`)
23

34
const onCreateNode = require(`../on-node-create`)
@@ -119,50 +120,6 @@ yadda yadda
119120

120121
expect(parsed.frontmatter.date).toEqual(new Date(date).toJSON())
121122
})
122-
123-
it(`Filters nodes with the given filter function, if provided`, async () => {
124-
const content = ``
125-
126-
node.content = content
127-
node.sourceInstanceName = `gatsby-test-source`
128-
129-
const createNode = jest.fn()
130-
const createParentChildLink = jest.fn()
131-
const actions = { createNode, createParentChildLink }
132-
const createNodeId = jest.fn()
133-
createNodeId.mockReturnValue(`uuid-from-gatsby`)
134-
135-
await onCreateNode(
136-
{
137-
node,
138-
loadNodeContent,
139-
actions,
140-
createNodeId,
141-
},
142-
{
143-
filter: node =>
144-
node.sourceInstanceName === `gatsby-other-test-source`,
145-
}
146-
).then(() => {
147-
expect(createNode).toHaveBeenCalledTimes(0)
148-
expect(createParentChildLink).toHaveBeenCalledTimes(0)
149-
})
150-
151-
await onCreateNode(
152-
{
153-
node,
154-
loadNodeContent,
155-
actions,
156-
createNodeId,
157-
},
158-
{
159-
filter: node => node.sourceInstanceName === `gatsby-test-source`,
160-
}
161-
).then(() => {
162-
expect(createNode).toHaveBeenCalledTimes(1)
163-
expect(createParentChildLink).toHaveBeenCalledTimes(1)
164-
})
165-
})
166123
})
167124

168125
describe(`process graphql correctly`, () => {

packages/gatsby-transformer-remark/src/extend-node-type.js

Lines changed: 74 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const toHAST = require(`mdast-util-to-hast`)
1616
const hastToHTML = require(`hast-util-to-html`)
1717
const mdastToToc = require(`mdast-util-toc`)
1818
const mdastToString = require(`mdast-util-to-string`)
19+
const Promise = require(`bluebird`)
1920
const unified = require(`unified`)
2021
const parse = require(`remark-parse`)
2122
const stringify = require(`remark-stringify`)
@@ -68,72 +69,6 @@ const safeGetCache = ({ getCache, cache }) => id => {
6869
return getCache(id)
6970
}
7071

71-
/**
72-
* @template T
73-
* @param {Array<T>} input
74-
* @param {(input: T) => Promise<void>} iterator
75-
* @return Promise<void>
76-
*/
77-
const eachPromise = (input, iterator) =>
78-
input.reduce(
79-
(accumulatorPromise, nextValue) =>
80-
accumulatorPromise.then(() => void iterator(nextValue)),
81-
Promise.resolve()
82-
)
83-
84-
const HeadingType = new GraphQLObjectType({
85-
name: `MarkdownHeading`,
86-
fields: {
87-
value: {
88-
type: GraphQLString,
89-
resolve(heading) {
90-
return heading.value
91-
},
92-
},
93-
depth: {
94-
type: GraphQLInt,
95-
resolve(heading) {
96-
return heading.depth
97-
},
98-
},
99-
},
100-
})
101-
102-
const HeadingLevels = new GraphQLEnumType({
103-
name: `HeadingLevels`,
104-
values: {
105-
h1: { value: 1 },
106-
h2: { value: 2 },
107-
h3: { value: 3 },
108-
h4: { value: 4 },
109-
h5: { value: 5 },
110-
h6: { value: 6 },
111-
},
112-
})
113-
114-
const ExcerptFormats = new GraphQLEnumType({
115-
name: `ExcerptFormats`,
116-
values: {
117-
PLAIN: { value: `plain` },
118-
HTML: { value: `html` },
119-
},
120-
})
121-
122-
const WordCountType = new GraphQLObjectType({
123-
name: `wordCount`,
124-
fields: {
125-
paragraphs: {
126-
type: GraphQLInt,
127-
},
128-
sentences: {
129-
type: GraphQLInt,
130-
},
131-
words: {
132-
type: GraphQLInt,
133-
},
134-
},
135-
})
136-
13772
/**
13873
* Map that keeps track of generation of AST to not generate it multiple
13974
* times in parallel.
@@ -153,31 +88,29 @@ module.exports = (
15388
reporter,
15489
...rest
15590
},
156-
{
157-
type: typeName = `MarkdownRemark`,
158-
plugins = [],
159-
blocks,
160-
commonmark = true,
161-
footnotes = true,
162-
gfm = true,
163-
pedantic = true,
164-
tableOfContents = {
165-
heading: null,
166-
maxDepth: 6,
167-
},
168-
...grayMatterOptions
169-
} = {}
91+
pluginOptions
17092
) => {
171-
if (type.name !== typeName) {
93+
if (type.name !== `MarkdownRemark`) {
17294
return {}
17395
}
174-
pluginsCacheStr = plugins.map(p => p.name).join(``)
96+
pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``)
17597
pathPrefixCacheStr = pathPrefix || ``
17698

17799
const getCache = safeGetCache({ cache, getCache: possibleGetCache })
178100

179101
return new Promise((resolve, reject) => {
180102
// Setup Remark.
103+
const {
104+
blocks,
105+
commonmark = true,
106+
footnotes = true,
107+
gfm = true,
108+
pedantic = true,
109+
tableOfContents = {
110+
heading: null,
111+
maxDepth: 6,
112+
},
113+
} = pluginOptions
181114
const tocOptions = tableOfContents
182115
const remarkOptions = {
183116
commonmark,
@@ -190,7 +123,7 @@ module.exports = (
190123
}
191124
let remark = new Remark().data(`settings`, remarkOptions)
192125

193-
for (let plugin of plugins) {
126+
for (let plugin of pluginOptions.plugins) {
194127
const requiredPlugin = require(plugin.resolve)
195128
if (_.isFunction(requiredPlugin.setParserPlugins)) {
196129
for (let parserPlugin of requiredPlugin.setParserPlugins(
@@ -234,8 +167,8 @@ module.exports = (
234167
if (process.env.NODE_ENV !== `production` || !fileNodes) {
235168
fileNodes = getNodesByType(`File`)
236169
}
237-
238-
await eachPromise(plugins, plugin => {
170+
// Use Bluebird's Promise function "each" to run remark plugins serially.
171+
await Promise.each(pluginOptions.plugins, plugin => {
239172
const requiredPlugin = require(plugin.resolve)
240173
if (_.isFunction(requiredPlugin.mutateSource)) {
241174
return requiredPlugin.mutateSource(
@@ -302,8 +235,8 @@ module.exports = (
302235
if (process.env.NODE_ENV !== `production` || !fileNodes) {
303236
fileNodes = getNodesByType(`File`)
304237
}
305-
306-
await eachPromise(plugins, plugin => {
238+
// Use Bluebird's Promise function "each" to run remark plugins serially.
239+
await Promise.each(pluginOptions.plugins, plugin => {
307240
const requiredPlugin = require(plugin.resolve)
308241
if (_.isFunction(requiredPlugin)) {
309242
return requiredPlugin(
@@ -515,6 +448,44 @@ module.exports = (
515448
return text
516449
}
517450

451+
const HeadingType = new GraphQLObjectType({
452+
name: `MarkdownHeading`,
453+
fields: {
454+
value: {
455+
type: GraphQLString,
456+
resolve(heading) {
457+
return heading.value
458+
},
459+
},
460+
depth: {
461+
type: GraphQLInt,
462+
resolve(heading) {
463+
return heading.depth
464+
},
465+
},
466+
},
467+
})
468+
469+
const HeadingLevels = new GraphQLEnumType({
470+
name: `HeadingLevels`,
471+
values: {
472+
h1: { value: 1 },
473+
h2: { value: 2 },
474+
h3: { value: 3 },
475+
h4: { value: 4 },
476+
h5: { value: 5 },
477+
h6: { value: 6 },
478+
},
479+
})
480+
481+
const ExcerptFormats = new GraphQLEnumType({
482+
name: `ExcerptFormats`,
483+
values: {
484+
PLAIN: { value: `plain` },
485+
HTML: { value: `html` },
486+
},
487+
})
488+
518489
return resolve({
519490
html: {
520491
type: GraphQLString,
@@ -552,7 +523,7 @@ module.exports = (
552523
format,
553524
pruneLength,
554525
truncate,
555-
excerptSeparator: grayMatterOptions.excerpt_separator,
526+
excerptSeparator: pluginOptions.excerpt_separator,
556527
})
557528
},
558529
},
@@ -572,7 +543,7 @@ module.exports = (
572543
return getExcerptAst(markdownNode, {
573544
pruneLength,
574545
truncate,
575-
excerptSeparator: grayMatterOptions.excerpt_separator,
546+
excerptSeparator: pluginOptions.excerpt_separator,
576547
}).then(ast => {
577548
const strippedAst = stripPosition(_.clone(ast), true)
578549
return hastReparseRaw(strippedAst)
@@ -631,7 +602,20 @@ module.exports = (
631602
},
632603
// TODO add support for non-latin languages https://github.com/wooorm/remark/issues/251#issuecomment-296731071
633604
wordCount: {
634-
type: WordCountType,
605+
type: new GraphQLObjectType({
606+
name: `wordCount`,
607+
fields: {
608+
paragraphs: {
609+
type: GraphQLInt,
610+
},
611+
sentences: {
612+
type: GraphQLInt,
613+
},
614+
words: {
615+
type: GraphQLInt,
616+
},
617+
},
618+
}),
635619
resolve(markdownNode) {
636620
let counts = {}
637621

0 commit comments

Comments
 (0)