Use .reduce() instead of .forEach() when replacing raw code blocks with beautified ones
This commit is contained in:
parent
5bb0bb6dba
commit
d778983135
|
@ -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)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue