« get me outta code hell

replacer: postprocessComments - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-11-20 19:01:17 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-20 19:01:17 -0400
commit1fd1fa4050aab88403426999284f45679dbba7f2 (patch)
tree5205adb2772a81ee140ddfcd71d54b7226144284
parentadf8035778d409cd2a57d3a537193b4bdb3f1503 (diff)
replacer: postprocessComments
Always say good morning!
-rw-r--r--src/util/replacer.js45
1 files changed, 45 insertions, 0 deletions
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`^<!--[\s\S]*?--> *` +
+       + '|' +
+
+         // 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` *<!--[\s\S]*?-->`),
+
+        '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);