« get me outta code hell

content: transformContent: absorbPunctuationFollowingExternalLinks - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-10-02 18:14:13 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-10-05 10:50:31 -0300
commit99d748daea7645c1ffa8eb47a67387447b53bad7 (patch)
treec26e2a5b86ec2c73c07f4b54c88bec4ec88e36c2 /src/content
parent11564d469dc313d732e462cb56314eb5a624e121 (diff)
content: transformContent: absorbPunctuationFollowingExternalLinks
Diffstat (limited to 'src/content')
-rw-r--r--src/content/dependencies/transformContent.js30
1 files changed, 27 insertions, 3 deletions
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,