aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-10-03 21:45:27 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-10-03 21:45:27 +0800
commit13e3058b12f4fff4ca46eaca9a5494bdb0d4b848 (patch)
tree8b1ecb38905b93dd2e50ca446cdc0d79dcf6e183
parent3337f59def9c342eb68d24589be3e9bb5c13ad36 (diff)
feat: add linkify for geo link
-rw-r--r--src/dumbymap.mjs24
1 files changed, 21 insertions, 3 deletions
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) => {
30 30
31 const md = MarkdownIt({ 31 const md = MarkdownIt({
32 html: true, 32 html: true,
33 breaks: true 33 breaks: true,
34 linkify: true
34 }) 35 })
35 .use(MarkdownItAnchor, { 36 .use(MarkdownItAnchor, {
36 permalink: MarkdownItAnchor.permalink.linkInsideHeader({ 37 permalink: MarkdownItAnchor.permalink.linkInsideHeader({
@@ -41,14 +42,31 @@ export const markdown2HTML = (container, mdContent) => {
41 .use(MarkdownItFrontMatter) 42 .use(MarkdownItFrontMatter)
42 .use(MarkdownItTocDoneRight) 43 .use(MarkdownItTocDoneRight)
43 44
45 // Create links with geo scheme
46 const coordinateRegex = /^(\D*)(-?\d+\.?\d*)\s*([,\x2F\uFF0C])\s*(-?\d+\.?\d*)/g
47 const coordinateValue = {
48 validate: coordinateRegex,
49 normalize: function (match) {
50 const matches = [...match.raw.matchAll(coordinateRegex)]
51 if (!matches[0]) return match
52 const [, , x, sep, y] = matches[0]
53 match.url = `geo:${y},${x}?xy=${x},${y}`
54 match.text = `${x}${sep}${y}`
55 match.index = match.raw.indexOf(x)
56 return match
57 }
58 }
59 const patterns = ['(', '📍', '\uFF08', '@', 'geo:', 'twd']
60 patterns.forEach(prefix =>
61 md.linkify.add(prefix, coordinateValue)
62 )
63
44 // FIXME A better way to generate blocks 64 // FIXME A better way to generate blocks
45 md.renderer.rules.dumby_block_open = () => '<div>' 65 md.renderer.rules.dumby_block_open = () => '<div>'
46 md.renderer.rules.dumby_block_close = () => '</div>' 66 md.renderer.rules.dumby_block_close = () => '</div>'
47
48 md.core.ruler.before('block', 'dumby_block', state => { 67 md.core.ruler.before('block', 'dumby_block', state => {
49 state.tokens.push(new state.Token('dumby_block_open', '', 1)) 68 state.tokens.push(new state.Token('dumby_block_open', '', 1))
50 }) 69 })
51
52 // Add close tag for block with more than 2 empty lines 70 // Add close tag for block with more than 2 empty lines
53 md.block.ruler.before('table', 'dumby_block', (state, startLine) => { 71 md.block.ruler.before('table', 'dumby_block', (state, startLine) => {
54 if ( 72 if (