The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Processor } from "unified"; |
| 2 | import { decodeString } from "micromark-util-decode-string"; |
| 3 | import { isWindowsAbsolutePath } from "./path.ts"; |
| 4 | |
| 5 | interface DestinationCompileContext { |
| 6 | readonly stack: ReadonlyArray<{ readonly type: string; url?: string }>; |
| 7 | resume(): string; |
| 8 | sliceSerialize(token: unknown): string; |
| 9 | } |
| 10 | |
| 11 | function keepWindowsPathDestination(this: DestinationCompileContext, token: unknown) { |
| 12 | const decoded = this.resume(); |
| 13 | const authored = this.sliceSerialize(token); |
| 14 | const node = this.stack.at(-1); |
| 15 | if (node) |
| 16 | node.url = isWindowsAbsolutePath(authored) |
| 17 | ? decodeString(authored.replaceAll("\\", "\\\\")) |
| 18 | : decoded; |
| 19 | } |
| 20 | |
| 21 | // A drive path that has not reached whitespace yet: `C:`, `C:\Users\me`, `C:/work`. |
| 22 | export const UNFINISHED_DRIVE_PATH = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/; |
| 23 | |
| 24 | interface EscapeCompileContext { |
| 25 | readonly stack: ReadonlyArray<{ |
| 26 | readonly type: string; |
| 27 | children?: Array<{ type: string; value?: string }>; |
| 28 | }>; |
| 29 | } |
| 30 | |
| 31 | const WHITESPACE = /\s/g; |
| 32 | // Text nodes only grow by appending while parsing, so each one is scanned for whitespace once. |
| 33 | const wordScans = new WeakMap<object, { scannedTo: number; wordStart: number }>(); |
| 34 | |
| 35 | function keepWindowsPathEscape(this: EscapeCompileContext) { |
| 36 | const text = this.stack.at(-1)?.children?.at(-1); |
| 37 | if (text?.type !== "text" || text.value === undefined) return; |
| 38 | let scan = wordScans.get(text); |
| 39 | if (!scan) wordScans.set(text, (scan = { scannedTo: 0, wordStart: 0 })); |
| 40 | WHITESPACE.lastIndex = scan.scannedTo; |
| 41 | while (WHITESPACE.exec(text.value)) scan.wordStart = WHITESPACE.lastIndex; |
| 42 | // A prose path cannot cross whitespace, so preceding words cannot affect this escape. |
| 43 | const end = text.value.length - 1; |
| 44 | if (UNFINISHED_DRIVE_PATH.test(text.value.slice(scan.wordStart, end))) { |
| 45 | text.value = `${text.value.slice(0, end)}\\${text.value.slice(end)}`; |
| 46 | } |
| 47 | scan.scannedTo = text.value.length; |
| 48 | } |
| 49 | |
| 50 | function keepWindowsProsePathEscape(this: EscapeCompileContext) { |
| 51 | // Destinations, titles, and labels are buffered in a fragment. |
| 52 | if (this.stack.at(-1)?.type !== "fragment") keepWindowsPathEscape.call(this); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Keeps escapes inside drive paths in prose only, for parsers that already escape Windows |
| 57 | * destinations themselves. Prose paths end at whitespace: past that, nothing tells a path |
| 58 | * from a sentence, and `C:\x and \*this\*` must still read `*this*`. |
| 59 | */ |
| 60 | export function remarkKeepWindowsProsePaths(this: Processor) { |
| 61 | (this.data().fromMarkdownExtensions ??= []).push({ |
| 62 | exit: { characterEscape: keepWindowsProsePathEscape }, |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape. Every backslash in a |
| 68 | * Windows path is a separator, so link, image, and definition destinations that are |
| 69 | * Windows paths keep the text as written, and so do escapes inside drive paths. |
| 70 | */ |
| 71 | export function remarkKeepWindowsPaths(this: Processor) { |
| 72 | (this.data().fromMarkdownExtensions ??= []).push({ |
| 73 | exit: { |
| 74 | resourceDestinationString: keepWindowsPathDestination, |
| 75 | definitionDestinationString: keepWindowsPathDestination, |
| 76 | characterEscape: keepWindowsPathEscape, |
| 77 | }, |
| 78 | }); |
| 79 | } |