From d7789831354cc9162b1db13f93ce4cc82662f0d6 Mon Sep 17 00:00:00 2001 From: Alexander Kachkaev Date: Tue, 9 Jan 2018 17:39:17 +0300 Subject: [PATCH] Use .reduce() instead of .forEach() when replacing raw code blocks with beautified ones --- src/beautifiers/markdown-blocks/index.coffee | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/beautifiers/markdown-blocks/index.coffee b/src/beautifiers/markdown-blocks/index.coffee index fa44a18..f884128 100644 --- a/src/beautifiers/markdown-blocks/index.coffee +++ b/src/beautifiers/markdown-blocks/index.coffee @@ -3,6 +3,11 @@ generateFakePath = (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) -> return new @Promise((resolve, reject) -> extractCodeBlocks = require 'gfm-code-blocks' @@ -45,11 +50,7 @@ module.exports = (text, logger) -> ) ) Promise.all(beautifyBlockPromises).then((cleanCodeBlocks) -> - cleanMarkdown = text - cleanCodeBlocks.reverse((b) -> !!b).forEach((codeBlock) -> - if (codeBlock) - cleanMarkdown = cleanMarkdown.substring(0, codeBlock.originalStart) + codeBlock.block + cleanMarkdown.substring(codeBlock.originalEnd) - ) - resolve(cleanMarkdown) + cleanText = cleanCodeBlocks.reverse((b) -> !!b).reduce(cleanCodeBlockReducer, text) + resolve(cleanText) ) )