From 4c99ad241cd8d34c0a6a68a4edac15b869ce6ee1 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 1 May 2025 12:30:32 -0300 Subject: replacer: postprocessExternalLinks: keep text node {i,iEnd} --- src/replacer.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/replacer.js') diff --git a/src/replacer.js b/src/replacer.js index d94cb71e..8abbbd6b 100644 --- a/src/replacer.js +++ b/src/replacer.js @@ -770,19 +770,32 @@ export function postprocessExternalLinks(inputNodes) { continue; } - let textContent = ''; + let textNode = { + i: node.i, + iEnd: null, + type: 'text', + data: '', + }; + let parseFrom = 0; for (const match of matchMarkdownLinks(node.data, {marked})) { const {label, href, index, length} = match; - textContent += node.data.slice(parseFrom, index); + textNode.data += node.data.slice(parseFrom, index); // Split the containing text node into two - the second of these will // be filled in and pushed by the next match, or after iterating over // all matches. - if (textContent.length) { - outputNodes.push({type: 'text', data: textContent}); - textContent = ''; + if (textNode.data) { + textNode.iEnd = textNode.i + textNode.data.length; + outputNodes.push(textNode); + + textNode = { + i: node.i + index + length, + iEnd: null, + type: 'text', + data: '', + }; } outputNodes.push({ @@ -796,11 +809,12 @@ export function postprocessExternalLinks(inputNodes) { } if (parseFrom !== node.data.length) { - textContent += node.data.slice(parseFrom); + textNode.data += node.data.slice(parseFrom); + textNode.iEnd = node.iEnd; } - if (textContent.length) { - outputNodes.push({type: 'text', data: textContent}); + if (textNode.data) { + outputNodes.push(textNode); } } -- cgit 1.3.0-6-gf8a5