From b37c0fd26b25a7506731a24f53bd7ae316cc5ea6 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 1 Mar 2024 08:13:27 -0400 Subject: replacer: preserve source i/iEnd through postprocessing --- src/util/replacer.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/util/replacer.js b/src/util/replacer.js index 64d8599..4bd9998 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; } -- cgit 1.3.0-6-gf8a5