|
| 1 | +import type { Root, Text, Parent } from 'mdast' |
| 2 | +import type { Plugin } from 'unified' |
| 3 | +import { visit } from 'unist-util-visit' |
| 4 | + |
| 5 | +type MarkNode = { |
| 6 | + type: 'mark' |
| 7 | + children: Text[] |
| 8 | + data: { |
| 9 | + hName: 'mark' |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +type HasChildren = Parent & { children: unknown[] } |
| 14 | + |
| 15 | +function isHasChildren(node: unknown): node is HasChildren { |
| 16 | + if (!node || typeof node !== 'object') return false |
| 17 | + if (!('children' in (node as HasChildren))) return false |
| 18 | + return Array.isArray((node as HasChildren).children) |
| 19 | +} |
| 20 | + |
| 21 | +function splitMarkedText(value: string): Array<Text | MarkNode> { |
| 22 | + const parts: Array<Text | MarkNode> = [] |
| 23 | + let hasMark = false |
| 24 | + |
| 25 | + let cursor = 0 |
| 26 | + |
| 27 | + while (cursor < value.length) { |
| 28 | + const start = value.indexOf('==', cursor) |
| 29 | + if (start === -1) { |
| 30 | + const tail = value.slice(cursor) |
| 31 | + if (tail) parts.push({ type: 'text', value: tail }) |
| 32 | + break |
| 33 | + } |
| 34 | + |
| 35 | + const before = value.slice(cursor, start) |
| 36 | + if (before) parts.push({ type: 'text', value: before }) |
| 37 | + |
| 38 | + const contentStart = start + 2 |
| 39 | + const end = value.indexOf('==', contentStart) |
| 40 | + |
| 41 | + // No closing delimiter; treat remainder as plain text. |
| 42 | + if (end === -1) { |
| 43 | + parts.push({ type: 'text', value: value.slice(start) }) |
| 44 | + break |
| 45 | + } |
| 46 | + |
| 47 | + const marked = value.slice(contentStart, end) |
| 48 | + |
| 49 | + // Empty highlights are treated as plain text (avoids producing empty <mark/>). |
| 50 | + if (!marked) { |
| 51 | + parts.push({ type: 'text', value: '====' }) |
| 52 | + cursor = end + 2 |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + parts.push({ |
| 57 | + type: 'mark', |
| 58 | + children: [{ type: 'text', value: marked }], |
| 59 | + data: { hName: 'mark' }, |
| 60 | + }) |
| 61 | + |
| 62 | + hasMark = true |
| 63 | + |
| 64 | + cursor = end + 2 |
| 65 | + } |
| 66 | + |
| 67 | + // If no marks were created, keep the original text intact. |
| 68 | + // This prevents breaking other token-based plugins (e.g., `<==>` in remark-replacements). |
| 69 | + if (!hasMark) return [{ type: 'text', value }] |
| 70 | + |
| 71 | + return parts |
| 72 | +} |
| 73 | + |
| 74 | +const remarkMarkPlus: Plugin<[], Root> = () => { |
| 75 | + return tree => { |
| 76 | + visit(tree, 'text', (node: Text, index, parent) => { |
| 77 | + if (!parent || typeof index !== 'number') return |
| 78 | + if (!isHasChildren(parent)) return |
| 79 | + |
| 80 | + if (!node.value.includes('==')) return |
| 81 | + |
| 82 | + const replacement = splitMarkedText(node.value) |
| 83 | + if (replacement.length === 1) { |
| 84 | + const first = replacement.at(0) |
| 85 | + if (first?.type === 'text') return |
| 86 | + } |
| 87 | + |
| 88 | + ;(parent.children as unknown[]).splice(index, 1, ...replacement) |
| 89 | + |
| 90 | + // Tell unist-util-visit to skip over the nodes we just inserted. |
| 91 | + return index + replacement.length |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export default remarkMarkPlus |
0 commit comments