From 1fd1fa4050aab88403426999284f45679dbba7f2 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 20 Nov 2024 19:01:17 -0400 Subject: replacer: postprocessComments Always say good morning! --- src/util/replacer.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/util/replacer.js b/src/util/replacer.js index 74e18873..d0c20ca4 100644 --- a/src/util/replacer.js +++ b/src/util/replacer.js @@ -444,6 +444,50 @@ export function cleanRawText(text) { return text; } +export function postprocessComments(inputNodes) { + const outputNodes = []; + + for (const node of inputNodes) { + if (node.type !== 'text') { + outputNodes.push(node); + continue; + } + + const commentRegexp = + new RegExp( + (// Remove comments which occupy entire lines, trimming the line break + // leading into them. These comments never include the ending of a + // comment which does not end a line, which is a regex way of saying + // "please fail early if we hit a --> that doesn't happen at the end + // of the line". + String.raw`\n(?!$))[\s\S])*?-->(?=$)` + + '|' + + + // Remove comments which appear at the start of a line, and any + // following spaces. + String.raw`^ *` + + + '|' + + + // Remove comments which appear anywhere else, including in the + // middle of a line or at the end of a line, and any leading spaces. + String.raw` *`), + + 'gm'); + + outputNodes.push({ + type: 'text', + + data: + node.data.replace(commentRegexp, ''), + + i: node.i, + iEnd: node.iEnd, + }); + } + + return outputNodes; +} + export function postprocessImages(inputNodes) { const outputNodes = []; @@ -714,6 +758,7 @@ export function parseInput(input) { try { let output = parseNodes(input, 0); + output = postprocessComments(output); output = postprocessImages(output); output = postprocessHeadings(output); output = postprocessSummaries(output); -- cgit 1.3.0-6-gf8a5