« get me outta code hell

replacer, wiki-data: factor out matchMarkdownLinks - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/replacer.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-05-01 12:24:51 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-05-06 12:29:02 -0300
commit99c00eb298b9fb4ff9454250ff474a67d0713c13 (patch)
tree191651014f9c3d163eb84c1fc28346e53ea84ca9 /src/replacer.js
parenta46f1171c0e63ea670e14bf796f42642646d3ae7 (diff)
replacer, wiki-data: factor out matchMarkdownLinks
Diffstat (limited to 'src/replacer.js')
-rw-r--r--src/replacer.js61
1 files changed, 21 insertions, 40 deletions
diff --git a/src/replacer.js b/src/replacer.js
index d285a7ea..d94cb71e 100644
--- a/src/replacer.js
+++ b/src/replacer.js
@@ -9,6 +9,7 @@ import * as marked from 'marked';
 
 import * as html from '#html';
 import {escapeRegex, typeAppearance} from '#sugar';
+import {matchMarkdownLinks} from '#wiki-data';
 
 export const replacerSpec = {
   'album': {
@@ -769,49 +770,29 @@ export function postprocessExternalLinks(inputNodes) {
       continue;
     }
 
-    const plausibleLinkRegexp = /\[.*?\)/g;
-
     let textContent = '';
+    let parseFrom = 0;
+    for (const match of matchMarkdownLinks(node.data, {marked})) {
+      const {label, href, index, length} = match;
+
+      textContent += 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 = '';
+      }
 
-    let plausibleMatch = null, parseFrom = 0;
-    while (plausibleMatch = plausibleLinkRegexp.exec(node.data)) {
-      textContent += node.data.slice(parseFrom, plausibleMatch.index);
-
-      // Pedantic rules use more particular parentheses detection in link
-      // destinations - they allow one level of balanced parentheses, and
-      // otherwise, parentheses must be escaped. This allows for entire links
-      // to be wrapped in parentheses, e.g below:
-      //
-      //   This is so cool. ([You know??](https://example.com))
-      //
-      const definiteMatch =
-        marked.Lexer.rules.inline.pedantic.link
-          .exec(node.data.slice(plausibleMatch.index));
-
-      if (definiteMatch) {
-        const {1: label, 2: href} = definiteMatch;
-
-        // Split the containing text node into two - the second of these will
-        // be added after iterating over matches, or by the next match.
-        if (textContent.length) {
-          outputNodes.push({type: 'text', data: textContent});
-          textContent = '';
-        }
-
-        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},
-        });
+      outputNodes.push({
+        i: node.i + index,
+        iEnd: node.i + index + length,
+        type: 'external-link',
+        data: {label, href},
+      });
 
-        parseFrom = offset + length;
-      } else {
-        parseFrom = plausibleMatch.index;
-      }
+      parseFrom = index + length;
     }
 
     if (parseFrom !== node.data.length) {