« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-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);