From c7f6383e34c30b63e0e0b86f320f42f2c9a0bdb7 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 3 Sep 2025 16:55:19 -0300 Subject: content, replacer: match inline links, auto-provide custom label Changes in matchMarkdownLinks here are refactoring only, not new behavior. --- src/replacer.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'src/replacer.js') diff --git a/src/replacer.js b/src/replacer.js index 779ee78d..78f3e104 100644 --- a/src/replacer.js +++ b/src/replacer.js @@ -9,7 +9,7 @@ import * as marked from 'marked'; import * as html from '#html'; import {empty, escapeRegex, typeAppearance} from '#sugar'; -import {matchMarkdownLinks} from '#wiki-data'; +import {matchInlineLinks, matchMarkdownLinks} from '#wiki-data'; export const replacerSpec = { 'album': { @@ -794,7 +794,7 @@ export function postprocessSummaries(inputNodes) { } export function postprocessExternalLinks(inputNodes) { - const outputNodes = []; + let outputNodes = []; for (const node of inputNodes) { if (node.type !== 'text') { @@ -850,6 +850,64 @@ export function postprocessExternalLinks(inputNodes) { } } + // Repeat everything, but for inline links, which are just a URL on its own, + // not formatted as a Markdown link. These don't have provided labels, and + // get labels automatically filled in by content code. + + inputNodes = outputNodes; + outputNodes = []; + + for (const node of inputNodes) { + if (node.type !== 'text') { + outputNodes.push(node); + continue; + } + + let textNode = { + i: node.i, + iEnd: null, + type: 'text', + data: '', + }; + + let parseFrom = 0; + for (const match of matchInlineLinks(node.data)) { + const {href, index, length} = match; + + textNode.data += node.data.slice(parseFrom, index); + + 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({ + i: node.i + index, + iEnd: node.i + index + length, + type: 'external-link', + data: {label: null, href}, + }); + + parseFrom = index + length; + } + + if (parseFrom !== node.data.length) { + textNode.data += node.data.slice(parseFrom); + textNode.iEnd = node.iEnd; + } + + if (textNode.data) { + outputNodes.push(textNode); + } + } + return outputNodes; } -- cgit 1.3.0-6-gf8a5