diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-05-01 12:30:32 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-05-06 12:29:04 -0300 |
commit | 4c99ad241cd8d34c0a6a68a4edac15b869ce6ee1 (patch) | |
tree | 91eb346b315688ce7ffe3c9deddaa9614bbfb0cb | |
parent | 99c00eb298b9fb4ff9454250ff474a67d0713c13 (diff) |
replacer: postprocessExternalLinks: keep text node {i,iEnd}
-rw-r--r-- | src/replacer.js | 30 |
1 files changed, 22 insertions, 8 deletions
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); } } |