|
| 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 | +import type StateInline from 'markdown-it/lib/rules_inline/state_inline.mjs' |
| 8 | + |
| 9 | +import linkRule from 'markdown-it/lib/rules_inline/link.mjs' |
| 10 | + |
| 11 | +type RefType = 'full' | 'collapsed' | 'shortcut' |
| 12 | + |
| 13 | +/** |
| 14 | + * Decide which reference form (if any) the wrapped link rule matched, |
| 15 | + * by inspecting the source range it consumed. |
| 16 | + * |
| 17 | + * @param state - markdown-it inline parser state |
| 18 | + * @param startPos - position the rule started at |
| 19 | + * @param endPos - position the rule ended at |
| 20 | + */ |
| 21 | +function detectReferenceForm( |
| 22 | + state: StateInline, |
| 23 | + startPos: number, |
| 24 | + endPos: number, |
| 25 | +): { label: string; type: RefType } | undefined { |
| 26 | + const savedPos = state.pos |
| 27 | + const labelEnd = state.md.helpers.parseLinkLabel(state, startPos) |
| 28 | + state.pos = savedPos |
| 29 | + if (labelEnd < 0) { |
| 30 | + return undefined |
| 31 | + } |
| 32 | + |
| 33 | + const labelStart = startPos + 1 |
| 34 | + const displayLabel = state.src.slice(labelStart, labelEnd) |
| 35 | + const after = labelEnd + 1 |
| 36 | + |
| 37 | + if (after >= endPos) { |
| 38 | + return { label: displayLabel, type: 'shortcut' } |
| 39 | + } |
| 40 | + |
| 41 | + const code = state.src.charCodeAt(after) |
| 42 | + |
| 43 | + if (code === 0x28 /* ( */) { |
| 44 | + // inline form |
| 45 | + return undefined |
| 46 | + } |
| 47 | + |
| 48 | + if (code !== 0x5b /* [ */) { |
| 49 | + return { label: displayLabel, type: 'shortcut' } |
| 50 | + } |
| 51 | + |
| 52 | + if (endPos === after + 2) { |
| 53 | + return { label: displayLabel, type: 'collapsed' } |
| 54 | + } |
| 55 | + |
| 56 | + return { label: state.src.slice(after + 1, endPos - 1), type: 'full' } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Wrap an existing inline rule so that, when it succeeds via the reference |
| 61 | + * branch, the freshly pushed token is tagged with reference metadata. |
| 62 | + * |
| 63 | + * @param original - upstream inline link rule |
| 64 | + */ |
| 65 | +function wrap(original: (state: StateInline, silent: boolean) => boolean) { |
| 66 | + return (state: StateInline, silent: boolean): boolean => { |
| 67 | + const start = state.pos |
| 68 | + if (!original(state, silent)) { |
| 69 | + return false |
| 70 | + } |
| 71 | + if (silent) { |
| 72 | + return true |
| 73 | + } |
| 74 | + |
| 75 | + const info = detectReferenceForm(state, start, state.pos) |
| 76 | + if (!info) { |
| 77 | + return true |
| 78 | + } |
| 79 | + |
| 80 | + const targetType = 'link_open' |
| 81 | + const target = state.tokens.findLast((t) => t.type === targetType) |
| 82 | + if (target) { |
| 83 | + target.attrSet('data-md-reference-label', info.label) |
| 84 | + target.attrSet('data-md-reference-type', info.type) |
| 85 | + } |
| 86 | + |
| 87 | + return true |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * markdown-it plugin: preserve reference-style link syntax across |
| 93 | + * the parse/serialize round-trip. |
| 94 | + * |
| 95 | + * Adds `data-md-reference-label` / `data-md-reference-type` attributes to |
| 96 | + * `link_open` tokens emitted via the reference branch. |
| 97 | + * |
| 98 | + * @param md - markdown-it instance to extend |
| 99 | + */ |
| 100 | +export default function referenceLinks(md: MarkdownIt): void { |
| 101 | + md.inline.ruler.at('link', wrap(linkRule)) |
| 102 | +} |
0 commit comments