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/common-util/wiki-data.js | 48 ++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'src/common-util') diff --git a/src/common-util/wiki-data.js b/src/common-util/wiki-data.js index cb024022..e0f41ee1 100644 --- a/src/common-util/wiki-data.js +++ b/src/common-util/wiki-data.js @@ -535,24 +535,46 @@ export function combineWikiDataArrays(arrays) { export function* matchMarkdownLinks(markdownSource, {marked}) { const plausibleLinkRegexp = /\[(?=.*?\))/g; + // 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 definiteLinkRegexp = marked.Lexer.rules.inline.pedantic.link; + let plausibleMatch = null; while (plausibleMatch = plausibleLinkRegexp.exec(markdownSource)) { - // 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(markdownSource.slice(plausibleMatch.index)); + definiteLinkRegexp.exec(markdownSource.slice(plausibleMatch.index)); - if (definiteMatch) { - const [{length}, label, href] = definiteMatch; - const index = plausibleMatch.index + definiteMatch.index; + if (!definiteMatch) { + continue; + } + + const [{length}, label, href] = definiteMatch; + const index = plausibleMatch.index + definiteMatch.index; - yield {label, href, index, length}; + yield {label, href, index, length}; + } +} + +export function* matchInlineLinks(source) { + const plausibleLinkRegexp = /\b[a-z]*:\/\/[^ ]*?(?=(?:[,.!?]*)(?:\s|$))/gm; + + let plausibleMatch = null; + while (plausibleMatch = plausibleLinkRegexp.exec(source)) { + const [href] = plausibleMatch; + const {index} = plausibleMatch; + const [{length}] = plausibleMatch; + + try { + new URL(href); + } catch { + continue; } + + yield {href, length, index}; } } -- cgit 1.3.0-6-gf8a5