From 99d748daea7645c1ffa8eb47a67387447b53bad7 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 2 Oct 2024 18:14:13 -0300 Subject: content: transformContent: absorbPunctuationFollowingExternalLinks --- src/content/dependencies/transformContent.js | 30 +++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/content/dependencies/transformContent.js b/src/content/dependencies/transformContent.js index 09211d29..6f1ad0c7 100644 --- a/src/content/dependencies/transformContent.js +++ b/src/content/dependencies/transformContent.js @@ -236,6 +236,11 @@ export default { default: true, }, + absorbPunctuationFollowingExternalLinks: { + type: 'boolean', + default: true, + }, + thumb: { validate: v => v.is('small', 'medium', 'large'), default: 'large', @@ -247,11 +252,20 @@ export default { let internalLinkIndex = 0; let externalLinkIndex = 0; + let offsetTextNode = 0; + const contentFromNodes = - data.nodes.map(node => { + data.nodes.map((node, index) => { + const nextNode = data.nodes[index + 1]; + switch (node.type) { - case 'text': - return {type: 'text', data: node.data}; + case 'text': { + const text = node.data.slice(offsetTextNode); + + offsetTextNode = 0; + + return {type: 'text', data: text}; + } case 'image': { const src = @@ -401,6 +415,16 @@ export default { fromContent: true, }); + if (slots.absorbPunctuationFollowingExternalLinks && nextNode?.type === 'text') { + const text = nextNode.data; + const match = text.match(/^[.,;:?!…]+/); + const suffix = match?.[0]; + if (suffix) { + externalLink.setSlot('suffixNormalContent', suffix); + offsetTextNode = suffix.length; + } + } + if (slots.indicateExternalLinks) { externalLink.setSlots({ indicateExternal: true, -- cgit 1.3.0-6-gf8a5