Skip to content

Commit

Permalink
Faster performance by not processing every line
Browse files Browse the repository at this point in the history
  • Loading branch information
timothymcmackin committed Nov 25, 2024
1 parent 5819866 commit 8668d2c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/scripts/concatenate.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,30 @@ 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 = '';
let line = '';

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() {
Expand Down

0 comments on commit 8668d2c

Please sign in to comment.