From: Thomas Crescenzi Date: Sat, 4 Nov 2023 00:30:55 +0000 (-0400) Subject: implement wikilinks in viewer X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=fd874e6c12dc3d6f8a98a5dbd3c4f3288dcc3115;p=flatnotes.git implement wikilinks in viewer --- diff --git a/flatnotes/src/autolinkParsers.js b/flatnotes/src/autolinkParsers.js new file mode 100644 index 0000000..cc65895 --- /dev/null +++ b/flatnotes/src/autolinkParsers.js @@ -0,0 +1,94 @@ +function parseWikiLink(source) { + const matched = source.matchAll(/\[\[(.*)\]\]/g); + if (matched) { + return Array.from(matched).map(match => { + const text = match[1]; + return { + text, + url: encodeURI(`/note/${text}`), + range: [match.index, match.index + match[0].length - 1] + } + }); + } + + return null; +} + +export function extendedAutolinks(source) { + return [...parseUrlLink(source), ...parseEmailLink(source), ...parseWikiLink(source)].sort( + (a, b) => a.range[0] - b.range[0] + ); +} + +/* + * Sourced from toast-ui. There autolink options are + * either override their built in functionality or + * use their built in functionality. We'd like to have + * both so this is the source of their parsers. +*/ +const DOMAIN = '(?:[w-]+.)*[A-Za-z0-9-]+.[A-Za-z0-9-]+'; +const PATH = '[^<\\s]*[^:"/\\|?*]/; @@ -234,7 +235,7 @@ export default { viewerOptions: { customHTMLRenderer: customHTMLRenderer, plugins: [codeSyntaxHighlight], - extendedAutolinks: true, + extendedAutolinks, }, editorOptions: { customHTMLRenderer: customHTMLRenderer,