From 13e3058b12f4fff4ca46eaca9a5494bdb0d4b848 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 3 Oct 2024 21:45:27 +0800 Subject: feat: add linkify for geo link --- src/dumbymap.mjs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index f85251a..04d40b5 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs @@ -30,7 +30,8 @@ export const markdown2HTML = (container, mdContent) => { const md = MarkdownIt({ html: true, - breaks: true + breaks: true, + linkify: true }) .use(MarkdownItAnchor, { permalink: MarkdownItAnchor.permalink.linkInsideHeader({ @@ -41,14 +42,31 @@ export const markdown2HTML = (container, mdContent) => { .use(MarkdownItFrontMatter) .use(MarkdownItTocDoneRight) + // Create links with geo scheme + const coordinateRegex = /^(\D*)(-?\d+\.?\d*)\s*([,\x2F\uFF0C])\s*(-?\d+\.?\d*)/g + const coordinateValue = { + validate: coordinateRegex, + normalize: function (match) { + const matches = [...match.raw.matchAll(coordinateRegex)] + if (!matches[0]) return match + const [, , x, sep, y] = matches[0] + match.url = `geo:${y},${x}?xy=${x},${y}` + match.text = `${x}${sep}${y}` + match.index = match.raw.indexOf(x) + return match + } + } + const patterns = ['(', '📍', '\uFF08', '@', 'geo:', 'twd'] + patterns.forEach(prefix => + md.linkify.add(prefix, coordinateValue) + ) + // FIXME A better way to generate blocks md.renderer.rules.dumby_block_open = () => '
' md.renderer.rules.dumby_block_close = () => '
' - md.core.ruler.before('block', 'dumby_block', state => { state.tokens.push(new state.Token('dumby_block_open', '', 1)) }) - // Add close tag for block with more than 2 empty lines md.block.ruler.before('table', 'dumby_block', (state, startLine) => { if ( -- cgit v1.2.3-70-g09d2