Use .reduce() instead of .forEach() when replacing raw code blocks with beautified ones

This commit is contained in:
Alexander Kachkaev 2018-01-09 17:39:17 +03:00
parent 5bb0bb6dba
commit d778983135
No known key found for this signature in database
GPG Key ID: DEFE41F826386E2E
1 changed files with 7 additions and 6 deletions

View File

@ -3,6 +3,11 @@
generateFakePath = (codeBlockFileExtension) -> generateFakePath = (codeBlockFileExtension) ->
return 'fakepath.' + codeBlockFileExtension return 'fakepath.' + codeBlockFileExtension
cleanCodeBlockReducer = (text, cleanCodeBlock) ->
if (cleanCodeBlock && text)
return text.substring(0, cleanCodeBlock.originalStart) + cleanCodeBlock.block + text.substring(cleanCodeBlock.originalEnd)
return text
module.exports = (text, logger) -> module.exports = (text, logger) ->
return new @Promise((resolve, reject) -> return new @Promise((resolve, reject) ->
extractCodeBlocks = require 'gfm-code-blocks' extractCodeBlocks = require 'gfm-code-blocks'
@ -45,11 +50,7 @@ module.exports = (text, logger) ->
) )
) )
Promise.all(beautifyBlockPromises).then((cleanCodeBlocks) -> Promise.all(beautifyBlockPromises).then((cleanCodeBlocks) ->
cleanMarkdown = text cleanText = cleanCodeBlocks.reverse((b) -> !!b).reduce(cleanCodeBlockReducer, text)
cleanCodeBlocks.reverse((b) -> !!b).forEach((codeBlock) -> resolve(cleanText)
if (codeBlock)
cleanMarkdown = cleanMarkdown.substring(0, codeBlock.originalStart) + codeBlock.block + cleanMarkdown.substring(codeBlock.originalEnd)
)
resolve(cleanMarkdown)
) )
) )