- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10.3k
feat(gatsby-transformer-remark): Allow for multiple different remark sources #7512
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
          
     Merged
      
      
            wardpeet
  merged 4 commits into
  gatsbyjs:master
from
alexkirsz:topics/multiple-remark-source
  
      
      
   
  Mar 13, 2019 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -16,7 +16,6 @@ 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`) | ||
|  | @@ -69,6 +68,72 @@ const safeGetCache = ({ getCache, cache }) => id => { | |
| return getCache(id) | ||
| } | ||
|  | ||
| /** | ||
| * @template T | ||
| * @param {Array<T>} input | ||
| * @param {(input: T) => Promise<void>} iterator | ||
| * @return Promise<void> | ||
| */ | ||
| const eachPromise = (input, iterator) => | ||
| input.reduce( | ||
| (accumulatorPromise, nextValue) => | ||
| accumulatorPromise.then(() => void iterator(nextValue)), | ||
| Promise.resolve() | ||
| ) | ||
|  | ||
| 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. | ||
|  | @@ -88,29 +153,31 @@ module.exports = ( | |
| reporter, | ||
| ...rest | ||
| }, | ||
| pluginOptions | ||
| { | ||
| type: typeName = `MarkdownRemark`, | ||
| plugins = [], | ||
| blocks, | ||
| commonmark = true, | ||
| footnotes = true, | ||
| gfm = true, | ||
| pedantic = true, | ||
| tableOfContents = { | ||
| heading: null, | ||
| maxDepth: 6, | ||
| }, | ||
| ...grayMatterOptions | ||
| } = {} | ||
| ) => { | ||
| if (type.name !== `MarkdownRemark`) { | ||
| if (type.name !== typeName) { | ||
| return {} | ||
| } | ||
| pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``) | ||
| pluginsCacheStr = 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, | ||
|  | @@ -123,7 +190,7 @@ module.exports = ( | |
| } | ||
| let remark = new Remark().data(`settings`, remarkOptions) | ||
|  | ||
| for (let plugin of pluginOptions.plugins) { | ||
| for (let plugin of plugins) { | ||
| const requiredPlugin = require(plugin.resolve) | ||
| if (_.isFunction(requiredPlugin.setParserPlugins)) { | ||
| for (let parserPlugin of requiredPlugin.setParserPlugins( | ||
|  | @@ -167,8 +234,8 @@ module.exports = ( | |
| if (process.env.NODE_ENV !== `production` || !fileNodes) { | ||
| fileNodes = getNodesByType(`File`) | ||
| } | ||
| // Use Bluebird's Promise function "each" to run remark plugins serially. | ||
| await Promise.each(pluginOptions.plugins, plugin => { | ||
|  | ||
| await eachPromise(plugins, plugin => { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't seem to work | ||
| const requiredPlugin = require(plugin.resolve) | ||
| if (_.isFunction(requiredPlugin.mutateSource)) { | ||
| return requiredPlugin.mutateSource( | ||
|  | @@ -235,8 +302,8 @@ module.exports = ( | |
| if (process.env.NODE_ENV !== `production` || !fileNodes) { | ||
| fileNodes = getNodesByType(`File`) | ||
| } | ||
| // Use Bluebird's Promise function "each" to run remark plugins serially. | ||
| await Promise.each(pluginOptions.plugins, plugin => { | ||
|  | ||
| await eachPromise(plugins, plugin => { | ||
| const requiredPlugin = require(plugin.resolve) | ||
| if (_.isFunction(requiredPlugin)) { | ||
| return requiredPlugin( | ||
|  | @@ -448,44 +515,6 @@ 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` }, | ||
| }, | ||
| }) | ||
|  | ||
| return resolve({ | ||
| html: { | ||
| type: GraphQLString, | ||
|  | @@ -523,7 +552,7 @@ module.exports = ( | |
| format, | ||
| pruneLength, | ||
| truncate, | ||
| excerptSeparator: pluginOptions.excerpt_separator, | ||
| excerptSeparator: grayMatterOptions.excerpt_separator, | ||
| }) | ||
| }, | ||
| }, | ||
|  | @@ -543,7 +572,7 @@ module.exports = ( | |
| return getExcerptAst(markdownNode, { | ||
| pruneLength, | ||
| truncate, | ||
| excerptSeparator: pluginOptions.excerpt_separator, | ||
| excerptSeparator: grayMatterOptions.excerpt_separator, | ||
| }).then(ast => { | ||
| const strippedAst = stripPosition(_.clone(ast), true) | ||
| return hastReparseRaw(strippedAst) | ||
|  | @@ -602,20 +631,7 @@ module.exports = ( | |
| }, | ||
| // TODO add support for non-latin languages https://github.com/wooorm/remark/issues/251#issuecomment-296731071 | ||
| wordCount: { | ||
| type: new GraphQLObjectType({ | ||
| name: `wordCount`, | ||
| fields: { | ||
| paragraphs: { | ||
| type: GraphQLInt, | ||
| }, | ||
| sentences: { | ||
| type: GraphQLInt, | ||
| }, | ||
| words: { | ||
| type: GraphQLInt, | ||
| }, | ||
| }, | ||
| }), | ||
| type: WordCountType, | ||
| resolve(markdownNode) { | ||
| let counts = {} | ||
|  | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this
voidhere is problematic? after removing it, it seems to work - why is it here? some context is neededThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also generally - where do we stand on this? We use bluebird pretty heavily--so I'm not sure it was worth refactoring this.
But a bit tangential, if we went that way--just need to ensure we have a working alternative for sequential promises!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally don't agree with removing dependency for the sake of removing it (at least for stuff that is only used during the builds and don't end up in production js bundles produced by webpack). If it fixes bugs or improve performance, then that's fine.
I will open PR fixing that and add some tests for this helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When utils like
bluebirdhave maintainers with domain knowledge and it was battle-tested by thousands of developers - I will trust that more than hand rolled and not very tested approaches.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case - here's hot-fix for issue #12578 (first commit on purpose just added failing test, and second commit "fixes" the problem.
@alexkirsz I would appreciate you taking a look on this, because I imagine there was some reason for that
voidto be there, but I have no clue what that would be (and it did break that utility)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pieh I'm not the author of this particular piece of code, @wardpeet is (I only authored the first commit of this PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok - sorry @alexkirsz. I should pay more attention to individual commits in the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bady, sorry about this