« get me outta code hell

replacer: preserve source i/iEnd through postprocessing - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-03-01 08:13:27 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-03-31 19:19:33 -0300
commitb37c0fd26b25a7506731a24f53bd7ae316cc5ea6 (patch)
tree9dfcd553db116801064d54c0c4f575cda29d5797 /src
parent5e95d7e7e2ff19b70ee831c93efb94331b3f6327 (diff)
replacer: preserve source i/iEnd through postprocessing
Diffstat (limited to 'src')
-rw-r--r--src/util/replacer.js33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/util/replacer.js b/src/util/replacer.js
index 64d8599b..4bd99988 100644
--- a/src/util/replacer.js
+++ b/src/util/replacer.js
@@ -462,7 +462,14 @@ export function postprocessImages(inputNodes) {
       let match = null, parseFrom = 0;
       while (match = imageRegexp.exec(node.data)) {
         const previousText = node.data.slice(parseFrom, match.index);
-        outputNodes.push({type: 'text', data: previousText});
+
+        outputNodes.push({
+          type: 'text',
+          data: previousText,
+          i: node.i + parseFrom,
+          iEnd: node.i + parseFrom + match.index,
+        });
+
         parseFrom = match.index + match[0].length;
 
         const imageNode = {type: 'image'};
@@ -534,6 +541,8 @@ export function postprocessImages(inputNodes) {
         outputNodes.push({
           type: 'text',
           data: node.data.slice(parseFrom),
+          i: node.i + parseFrom,
+          iEnd: node.iEnd,
         });
       }
 
@@ -576,7 +585,12 @@ export function postprocessHeadings(inputNodes) {
       textContent += node.data.slice(parseFrom);
     }
 
-    outputNodes.push({type: 'text', data: textContent});
+    outputNodes.push({
+      type: 'text',
+      data: textContent,
+      i: node.i,
+      iEnd: node.iEnd,
+    });
   }
 
   return outputNodes;
@@ -613,12 +627,17 @@ export function postprocessExternalLinks(inputNodes) {
           textContent = '';
         }
 
-        outputNodes.push({type: 'external-link', data: {label, href}});
+        const offset = plausibleMatch.index + definiteMatch.index;
+        const length = definiteMatch[0].length;
+
+        outputNodes.push({
+          i: node.i + offset,
+          iEnd: node.i + offset + length,
+          type: 'external-link',
+          data: {label, href},
+        });
 
-        parseFrom =
-          plausibleMatch.index +
-          definiteMatch.index +
-          definiteMatch[0].length;
+        parseFrom = offset + length;
       } else {
         parseFrom = plausibleMatch.index;
       }