The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { keepLeadingWhitespace, keepsLeadingWhitespace } from "./markdownLeadingWhitespace.ts"; |
| 2 | import { keepOrderedListNumbers, keepsOrderedListNumbers } from "./markdownOrderedListNumbers.ts"; |
| 3 | |
| 4 | interface MarkdownPosition { |
| 5 | readonly start?: { |
| 6 | readonly line?: number; |
| 7 | readonly offset?: number; |
| 8 | }; |
| 9 | } |
| 10 | |
| 11 | interface MarkdownAstNode { |
| 12 | readonly type: string; |
| 13 | readonly value?: unknown; |
| 14 | readonly position?: MarkdownPosition; |
| 15 | children?: MarkdownAstNode[]; |
| 16 | } |
| 17 | |
| 18 | interface MarkdownFile { |
| 19 | readonly value?: unknown; |
| 20 | } |
| 21 | |
| 22 | interface MarkdownParser { |
| 23 | parse(markdown: string): unknown; |
| 24 | } |
| 25 | |
| 26 | interface RecoveredMarkdown { |
| 27 | readonly blocks: MarkdownAstNode[]; |
| 28 | readonly source: string; |
| 29 | } |
| 30 | |
| 31 | const INLINE_PARSE_PREFIX = "t3-markdown-inline-prefix:"; |
| 32 | |
| 33 | function isSameLineOverIndentedCode( |
| 34 | node: MarkdownAstNode, |
| 35 | parent: MarkdownAstNode | undefined, |
| 36 | markdown: string, |
| 37 | ): boolean { |
| 38 | if ( |
| 39 | node.type !== "code" || |
| 40 | parent?.type !== "listItem" || |
| 41 | typeof node.value !== "string" || |
| 42 | !/^[\t ]/.test(node.value) |
| 43 | ) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | const nodeStart = node.position?.start; |
| 48 | const parentStart = parent.position?.start; |
| 49 | if ( |
| 50 | nodeStart?.line === undefined || |
| 51 | nodeStart.offset === undefined || |
| 52 | parentStart?.line === undefined || |
| 53 | nodeStart.line !== parentStart.line |
| 54 | ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | const sourceCharacter = markdown[nodeStart.offset]; |
| 59 | return sourceCharacter !== "`" && sourceCharacter !== "~"; |
| 60 | } |
| 61 | |
| 62 | function parseRecoveredMarkdown(value: string, parser: MarkdownParser): RecoveredMarkdown { |
| 63 | // Prefix block-looking input to parse inline extensions; retain later block children after blank lines. |
| 64 | const source = `${INLINE_PARSE_PREFIX}${value}`; |
| 65 | const document = parser.parse(source) as MarkdownAstNode; |
| 66 | const blocks = document.children; |
| 67 | const paragraph = blocks?.[0]; |
| 68 | const children = paragraph?.type === "paragraph" ? paragraph.children : undefined; |
| 69 | const first = children?.[0]; |
| 70 | if ( |
| 71 | !blocks || |
| 72 | !children || |
| 73 | first?.type !== "text" || |
| 74 | typeof first.value !== "string" || |
| 75 | !first.value.startsWith(INLINE_PARSE_PREFIX) |
| 76 | ) { |
| 77 | return { blocks: [{ type: "text", value }], source }; |
| 78 | } |
| 79 | |
| 80 | const firstValue = first.value.slice(INLINE_PARSE_PREFIX.length); |
| 81 | return { |
| 82 | blocks: [ |
| 83 | { |
| 84 | ...paragraph, |
| 85 | type: "paragraph", |
| 86 | children: [...(firstValue ? [{ ...first, value: firstValue }] : []), ...children.slice(1)], |
| 87 | }, |
| 88 | ...blocks.slice(1), |
| 89 | ], |
| 90 | source, |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | function blocksFromIndentedCode(node: MarkdownAstNode, parser: MarkdownParser): RecoveredMarkdown { |
| 95 | const value = typeof node.value === "string" ? node.value.trim() : ""; |
| 96 | const recovered = parseRecoveredMarkdown(value, parser); |
| 97 | if (keepsOrderedListNumbers(parser)) { |
| 98 | for (const block of recovered.blocks) keepOrderedListNumbers(block, recovered.source); |
| 99 | } |
| 100 | if (keepsLeadingWhitespace(parser)) { |
| 101 | // The trimmed first line sets where the item's content starts. |
| 102 | const indent = /^[\t ]*/.exec(String(node.value))![0]; |
| 103 | const columns = [...indent].reduce( |
| 104 | (column, character) => (character === "\t" ? (Math.floor(column / 4) + 1) * 4 : column + 1), |
| 105 | 0, |
| 106 | ); |
| 107 | keepLeadingWhitespace({ type: "root", children: recovered.blocks }, recovered.source, columns); |
| 108 | } |
| 109 | const first = recovered.blocks[0]; |
| 110 | return { |
| 111 | ...recovered, |
| 112 | blocks: |
| 113 | first && node.position |
| 114 | ? [{ ...first, position: node.position }, ...recovered.blocks.slice(1)] |
| 115 | : recovered.blocks, |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | // CommonMark treats four or more spaces after a list marker as code. |
| 120 | function attachListItemIndentationNormalizer(this: MarkdownParser) { |
| 121 | return (tree: MarkdownAstNode, file: MarkdownFile) => { |
| 122 | if (typeof file.value !== "string") { |
| 123 | return; |
| 124 | } |
| 125 | const markdown = file.value; |
| 126 | |
| 127 | const visit = (node: MarkdownAstNode, source: string) => { |
| 128 | if (!node.children) { |
| 129 | return; |
| 130 | } |
| 131 | if (!node.children.some((child) => isSameLineOverIndentedCode(child, node, source))) { |
| 132 | for (const child of node.children) visit(child, source); |
| 133 | return; |
| 134 | } |
| 135 | node.children = node.children.flatMap((child) => { |
| 136 | if (isSameLineOverIndentedCode(child, node, source)) { |
| 137 | const recovered = blocksFromIndentedCode(child, this); |
| 138 | for (const block of recovered.blocks) { |
| 139 | visit(block, recovered.source); |
| 140 | } |
| 141 | return recovered.blocks; |
| 142 | } |
| 143 | visit(child, source); |
| 144 | return [child]; |
| 145 | }); |
| 146 | }; |
| 147 | |
| 148 | visit(tree, markdown); |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | export const remarkNormalizeListItemIndentation = attachListItemIndentationNormalizer; |