diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-09-03 16:55:19 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-09-03 16:56:06 -0300 |
commit | c7f6383e34c30b63e0e0b86f320f42f2c9a0bdb7 (patch) | |
tree | 9fe61e6ccb8afd6c474cf667634d2c7f504247a0 /src/replacer.js | |
parent | 0c70a112914f7eb4bea0d839d44060cb30f4f30e (diff) |
content, replacer: match inline links, auto-provide custom label
Changes in matchMarkdownLinks here are refactoring only, not new behavior.
Diffstat (limited to 'src/replacer.js')
-rw-r--r-- | src/replacer.js | 62 |
1 files changed, 60 insertions, 2 deletions
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; } |