From 99c00eb298b9fb4ff9454250ff474a67d0713c13 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 1 May 2025 12:24:51 -0300 Subject: replacer, wiki-data: factor out matchMarkdownLinks --- src/common-util/wiki-data.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/common-util') diff --git a/src/common-util/wiki-data.js b/src/common-util/wiki-data.js index c993f4ac..b21af786 100644 --- a/src/common-util/wiki-data.js +++ b/src/common-util/wiki-data.js @@ -523,3 +523,30 @@ export function combineWikiDataArrays(arrays) { return combined; } } + +// Markdown stuff + +export function* matchMarkdownLinks(markdownSource, {marked}) { + const plausibleLinkRegexp = /\[.*?\)/g; + + 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)); + + if (definiteMatch) { + const [{length}, label, href] = definiteMatch; + const index = plausibleMatch.index + definiteMatch.index; + + yield {label, href, index, length}; + } + } +} -- cgit 1.3.0-6-gf8a5