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/common-util/wiki-data.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/common-util/wiki-data.js')
-rw-r--r-- | src/common-util/wiki-data.js | 48 |
1 files changed, 35 insertions, 13 deletions
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}; } } |