|
| 1 | +import type { Plugin } from 'unified'; |
| 2 | +import { visit } from 'unist-util-visit'; |
| 3 | +import type { Break, Content, Paragraph, PhrasingContent, Root, Text } from 'mdast'; |
| 4 | + |
| 5 | +/** |
| 6 | + * remark plugin that rewrites raw HTML nodes into plain-text equivalents. |
| 7 | + * |
| 8 | + * remark parses inline HTML into `html` nodes even when we do not want to render |
| 9 | + * them. We turn each of those nodes into regular text (plus `<br>` break markers) |
| 10 | + * so the downstream rehype pipeline escapes the characters instead of executing |
| 11 | + * them. Leading spaces and tab characters are converted to non-breaking spaces to |
| 12 | + * keep indentation identical to the original author input. |
| 13 | + */ |
| 14 | + |
| 15 | +const LINE_BREAK = /\r?\n/; |
| 16 | + |
| 17 | +const PHRASE_PARENTS = new Set([ |
| 18 | + 'paragraph', |
| 19 | + 'heading', |
| 20 | + 'emphasis', |
| 21 | + 'strong', |
| 22 | + 'delete', |
| 23 | + 'link', |
| 24 | + 'linkReference', |
| 25 | + 'tableCell' |
| 26 | +]); |
| 27 | + |
| 28 | +const NBSP = '\u00a0'; |
| 29 | +const TAB_AS_SPACES = NBSP.repeat(4); |
| 30 | + |
| 31 | +function preserveIndent(line: string): string { |
| 32 | + let index = 0; |
| 33 | + let output = ''; |
| 34 | + |
| 35 | + while (index < line.length) { |
| 36 | + const char = line[index]; |
| 37 | + |
| 38 | + if (char === ' ') { |
| 39 | + output += NBSP; |
| 40 | + index += 1; |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + if (char === '\t') { |
| 45 | + output += TAB_AS_SPACES; |
| 46 | + index += 1; |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + return output + line.slice(index); |
| 54 | +} |
| 55 | + |
| 56 | +function createLiteralChildren(value: string): PhrasingContent[] { |
| 57 | + const lines = value.split(LINE_BREAK); |
| 58 | + const nodes: PhrasingContent[] = []; |
| 59 | + |
| 60 | + for (const [lineIndex, rawLine] of lines.entries()) { |
| 61 | + if (lineIndex > 0) { |
| 62 | + nodes.push({ type: 'break' } as Break as unknown as PhrasingContent); |
| 63 | + } |
| 64 | + |
| 65 | + nodes.push({ |
| 66 | + type: 'text', |
| 67 | + value: preserveIndent(rawLine) |
| 68 | + } as Text as unknown as PhrasingContent); |
| 69 | + } |
| 70 | + |
| 71 | + if (!nodes.length) { |
| 72 | + nodes.push({ type: 'text', value: '' } as Text as unknown as PhrasingContent); |
| 73 | + } |
| 74 | + |
| 75 | + return nodes; |
| 76 | +} |
| 77 | + |
| 78 | +export const remarkLiteralHtml: Plugin<[], Root> = () => { |
| 79 | + return (tree) => { |
| 80 | + visit(tree, 'html', (node, index, parent) => { |
| 81 | + if (!parent || typeof index !== 'number') { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const replacement = createLiteralChildren(node.value); |
| 86 | + |
| 87 | + if (!PHRASE_PARENTS.has(parent.type as string)) { |
| 88 | + const paragraph: Paragraph = { |
| 89 | + type: 'paragraph', |
| 90 | + children: replacement as Paragraph['children'], |
| 91 | + data: { literalHtml: true } |
| 92 | + }; |
| 93 | + |
| 94 | + const siblings = parent.children as unknown as Content[]; |
| 95 | + siblings.splice(index, 1, paragraph as unknown as Content); |
| 96 | + |
| 97 | + if (index > 0) { |
| 98 | + const previous = siblings[index - 1] as Paragraph | undefined; |
| 99 | + |
| 100 | + if ( |
| 101 | + previous?.type === 'paragraph' && |
| 102 | + (previous.data as { literalHtml?: boolean } | undefined)?.literalHtml |
| 103 | + ) { |
| 104 | + const prevChildren = previous.children as unknown as PhrasingContent[]; |
| 105 | + |
| 106 | + if (prevChildren.length) { |
| 107 | + const lastChild = prevChildren[prevChildren.length - 1]; |
| 108 | + |
| 109 | + if (lastChild.type !== 'break') { |
| 110 | + prevChildren.push({ |
| 111 | + type: 'break' |
| 112 | + } as Break as unknown as PhrasingContent); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + prevChildren.push(...(paragraph.children as unknown as PhrasingContent[])); |
| 117 | + |
| 118 | + siblings.splice(index, 1); |
| 119 | + |
| 120 | + return index; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return index + 1; |
| 125 | + } |
| 126 | + |
| 127 | + (parent.children as unknown as PhrasingContent[]).splice( |
| 128 | + index, |
| 129 | + 1, |
| 130 | + ...(replacement as unknown as PhrasingContent[]) |
| 131 | + ); |
| 132 | + |
| 133 | + return index + replacement.length; |
| 134 | + }); |
| 135 | + }; |
| 136 | +}; |
0 commit comments