From df30a064c1c1e65ada044ae8f77dfa18a3cd7c02 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Mon, 25 Nov 2024 15:39:28 -0500 Subject: [PATCH] Faster performance by not processing every line --- src/scripts/concatenate.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/scripts/concatenate.js b/src/scripts/concatenate.js index 6df063680..52d05043e 100644 --- a/src/scripts/concatenate.js +++ b/src/scripts/concatenate.js @@ -52,29 +52,29 @@ async function getFilePath(fileId) { } // Remove the front matter from an MD file and replace with an H1 -// Got to remove FM because multiple FM blocks are not legal +// Got to remove FM because multiple FM blocks break some markdown tools // Could do this with gray-matter but I don't want to add the dependency function removeFrontMatter(mdText) { - let outputLines = []; const lines = mdText.split('\n'); let inFrontMatter = false; let doneWithFrontMatter = false; const h1Regex = /^title:\s+(.*)$/; + let titleLine = ''; - lines.forEach(line => { + while (lines.length > 0) { + line = lines.shift(); if (line == '---') { doneWithFrontMatter = inFrontMatter; inFrontMatter = true; } if (inFrontMatter && !doneWithFrontMatter && h1Regex.test(line)) { const result = h1Regex.exec(line); - outputLines.push('# ' + result[1]); + titleLine = '# ' + result[1]; } if (line != '---' && doneWithFrontMatter) { - outputLines.push(line); + return [titleLine, ''].concat(lines).join('\n'); } - }); - return outputLines.join('\n'); + } } async function concatEverything() {