@@ -16,6 +16,7 @@ const toHAST = require(`mdast-util-to-hast`)
1616const hastToHTML = require ( `hast-util-to-html` )
1717const mdastToToc = require ( `mdast-util-toc` )
1818const mdastToString = require ( `mdast-util-to-string` )
19+ const Promise = require ( `bluebird` )
1920const unified = require ( `unified` )
2021const parse = require ( `remark-parse` )
2122const 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