|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type MarkdownIt from 'markdown-it' |
| 7 | + |
| 8 | +/** |
| 9 | + * markdown-it plugin: parse Obsidian-style wiki links and wiki image links |
| 10 | + * |
| 11 | + * Produces |
| 12 | + * Produces a standard `link` or `image` token so that downstream plugins |
| 13 | + * treat it exactly like a normal link/image. |
| 14 | + * |
| 15 | + * A `data-wiki-image` or `data-wiki-link` attribute is set so the ProseMirror |
| 16 | + * serializer can round-trip the syntax back to the wiki style link/image syntax. |
| 17 | + * |
| 18 | + * @param md - The markdown-it instance to extend |
| 19 | + */ |
| 20 | +export default function wikiLinks(md: MarkdownIt): void { |
| 21 | + // Parse wiki image links `![[filename]]` |
| 22 | + md.inline.ruler.before('image', 'wiki_image_link', (state, silent) => { |
| 23 | + const src = state.src |
| 24 | + const pos = state.pos |
| 25 | + |
| 26 | + // Must start with ![[ |
| 27 | + if (src.charCodeAt(pos) !== 0x21 /* ! */) return false |
| 28 | + if (src.charCodeAt(pos + 1) !== 0x5b /* [ */) return false |
| 29 | + if (src.charCodeAt(pos + 2) !== 0x5b /* [ */) return false |
| 30 | + |
| 31 | + // Find the closing ]] — no newlines allowed inside |
| 32 | + let end = pos + 3 |
| 33 | + while (end < src.length) { |
| 34 | + const ch = src.charCodeAt(end) |
| 35 | + if (ch === 0x0a /* \n */) return false |
| 36 | + if (ch === 0x5d /* ] */ && src.charCodeAt(end + 1) === 0x5d /* ] */) |
| 37 | + break |
| 38 | + end++ |
| 39 | + } |
| 40 | + if (end >= src.length) return false |
| 41 | + |
| 42 | + const filename = src.slice(pos + 3, end) |
| 43 | + if (!filename) return false |
| 44 | + |
| 45 | + if (silent) return true |
| 46 | + |
| 47 | + const token = state.push('image', 'img', 0) |
| 48 | + token.attrs = [ |
| 49 | + ['src', filename], |
| 50 | + ['alt', ''], |
| 51 | + ['data-wiki-image', 'true'], |
| 52 | + ] |
| 53 | + // Alt text is derived from token.children by the renderer |
| 54 | + const altToken = new state.Token('text', '', 0) |
| 55 | + altToken.content = filename |
| 56 | + token.children = [altToken] |
| 57 | + token.content = filename |
| 58 | + |
| 59 | + state.pos = end + 2 |
| 60 | + return true |
| 61 | + }) |
| 62 | + |
| 63 | + // Parse wiki links `[[link]]` |
| 64 | + md.inline.ruler.before('link', 'wiki_link', (state, silent) => { |
| 65 | + const src = state.src |
| 66 | + const pos = state.pos |
| 67 | + |
| 68 | + // Must start with [[ |
| 69 | + if (src.charCodeAt(pos) !== 0x5b /* [ */) return false |
| 70 | + if (src.charCodeAt(pos + 1) !== 0x5b /* [ */) return false |
| 71 | + |
| 72 | + // Prevent matching `[[` that is itself preceded by `[` (e.g. `[[[foo]]]`) |
| 73 | + if (pos > 0 && src.charCodeAt(pos - 1) === 0x5b /* [ */) return false |
| 74 | + |
| 75 | + // Find the closing ]] — no newlines allowed inside |
| 76 | + let end = pos + 2 |
| 77 | + while (end < src.length) { |
| 78 | + const ch = src.charCodeAt(end) |
| 79 | + if (ch === 0x0a /* \n */) return false |
| 80 | + if (ch === 0x5d /* ] */ && src.charCodeAt(end + 1) === 0x5d /* ] */) |
| 81 | + break |
| 82 | + end++ |
| 83 | + } |
| 84 | + if (end >= src.length) return false |
| 85 | + |
| 86 | + const content = src.slice(pos + 2, end) |
| 87 | + if (!content) return false |
| 88 | + |
| 89 | + // Split on first | to get target and optional display text |
| 90 | + const pipeIdx = content.indexOf('|') |
| 91 | + const target = pipeIdx === -1 ? content : content.slice(0, pipeIdx) |
| 92 | + const displayText = pipeIdx === -1 ? content : content.slice(pipeIdx + 1) |
| 93 | + |
| 94 | + if (!target) return false |
| 95 | + |
| 96 | + // Reject targets containing characters that conflict with CommonMark inline syntax |
| 97 | + // ([, ], * are not valid in file names on most systems anyway) |
| 98 | + if (/[[\]*]/.test(target)) return false |
| 99 | + |
| 100 | + if (silent) return true |
| 101 | + |
| 102 | + const tokenOpen = state.push('link_open', 'a', 1) |
| 103 | + tokenOpen.attrs = [ |
| 104 | + ['href', target], |
| 105 | + ['data-wiki-link', 'true'], |
| 106 | + ] |
| 107 | + |
| 108 | + const tokenText = state.push('text', '', 0) |
| 109 | + tokenText.content = displayText |
| 110 | + |
| 111 | + state.push('link_close', 'a', -1) |
| 112 | + |
| 113 | + state.pos = end + 2 |
| 114 | + return true |
| 115 | + }) |
| 116 | +} |
0 commit comments