fix(markdown): keep Windows paths intact in link and image destinations #12615
MergedSunkenInTime(GitHub profile, opens in a new tab)merged 1 commit into
main fromrefs/pull/12615/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- packages/client-runtime/src/markdownWindowsPaths.ts lines 12 to 12OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,12 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+const WINDOWS_DRIVE_DESTINATION_PATTERN = /\]\(\s*(?:<([A-Za-z]:\\[^>\n]*)>|([A-Za-z]:\\[^\s)]*))/g;12+const INLINE_CODE_PATTERN = /(`+)[^`][\s\S]*?\1(?!`)/g;packages/client-runtime/src/markdownWindowsPaths.ts line 62OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +42,21 @@42+ * Rewrites Windows drive-letter link and image destinations to forward slashes43+ * so backslash escapes cannot corrupt the path during parsing. Code spans and44+ * fenced code blocks are left as written. The result has the same length as45+ * the input.46+ */47+export function normalizeWindowsMarkdownDestinations(markdown: string): string {48+ if (!markdown.includes(":\\")) return markdown;49+50+ const lines = markdown.split("\n");51+ const output: string[] = [];52+ let prose: string[] = [];53+ let openFence: string | null = null;54+55+ const flushProse = () => {56+ if (prose.length === 0) return;57+ output.push(normalizeOutsideInlineCode(prose.join("\n")));58+ prose = [];59+ };60+61+ for (const line of lines) {62+ const fence = CODE_FENCE_PATTERN.exec(line)?.[1];packages/client-runtime/src/markdownWindowsPaths.ts line 11OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,11 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+const WINDOWS_DRIVE_DESTINATION_PATTERN = /\]\(\s*(?:<([A-Za-z]:\\[^>\n]*)>|([A-Za-z]:\\[^\s)]*))/g;
- packages/client-runtime/src/markdownWindowsPaths.ts line 11OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,11 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+const WINDOWS_DRIVE_DESTINATION_PATTERN = /\]\(\s*(?:<([A-Za-z]:\\[^>\n]*)>|([A-Za-z]:\\[^\s)]*))/g;packages/client-runtime/src/markdownWindowsPaths.ts line 12OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,12 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+const WINDOWS_DRIVE_DESTINATION_PATTERN = /\]\(\s*(?:<([A-Za-z]:\\[^>\n]*)>|([A-Za-z]:\\[^\s)]*))/g;12+const INLINE_CODE_PATTERN = /(`+)[^`][\s\S]*?\1(?!`)/g;packages/client-runtime/src/markdownWindowsPaths.ts line 13OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,13 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+const WINDOWS_DRIVE_DESTINATION_PATTERN = /\]\(\s*(?:<([A-Za-z]:\\[^>\n]*)>|([A-Za-z]:\\[^\s)]*))/g;12+const INLINE_CODE_PATTERN = /(`+)[^`][\s\S]*?\1(?!`)/g;13+const CODE_FENCE_PATTERN = /^ {0,3}(`{3,}|~{3,})/;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- packages/client-runtime/src/markdownWindowsPaths.ts lines 11 to 11OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,11 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+const DESTINATION_START_PATTERN = /\]\(\s*(<?)([A-Za-z]:\\)/g;packages/client-runtime/src/markdownWindowsPaths.ts lines 15 to 15OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,15 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+const DESTINATION_START_PATTERN = /\]\(\s*(<?)([A-Za-z]:\\)/g;12+// A code span opens and closes with backtick runs of the same length. A run13+// next to another backtick is part of a longer run, and a backslash before a14+// run escapes its first backtick, so neither can delimit a span.15+const INLINE_CODE_PATTERN = /(?<![`\\])(`+)[^`][\s\S]*?(?<![`\\])\1(?!`)/g;
- packages/client-runtime/src/markdownWindowsPaths.ts lines 56 to 56OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +36,21 @@36+ return index - start;37+}38+39+function normalizeDestinations(segment: string): string {40+ if (!segment.includes(":\\")) return segment;41+ let result = "";42+ let cursor = 0;43+ for (const match of segment.matchAll(DESTINATION_START_PATTERN)) {44+ const angle = match[1] === "<";45+ const start = match.index + match[0].length - 3;46+ if (start < cursor) continue;47+ const length = angle48+ ? (() => {49+ const end = segment.indexOf(">", start);50+ const lineEnd = segment.indexOf("\n", start);51+ return end < 0 || (lineEnd >= 0 && lineEnd < end) ? -1 : end - start;52+ })()53+ : bareDestinationLength(segment, start);54+ if (length < 0) continue;55+ result += segment.slice(cursor, start);56+ result += segment.slice(start, start + length).replaceAll("\\", "/");packages/client-runtime/src/markdownWindowsPaths.ts line 12OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,12 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+const DESTINATION_START_PATTERN = /(?:\]\(\s*|^ {0,3}\[[^\]\n]+\]:[ \t]*)(<?)([A-Za-z]:\\)/gm;
- packages/client-runtime/src/markdownWindowsPaths.ts lines 13 to 13OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,13 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+const DESTINATION_START_PATTERN =13+ /(?:\]\(\s*|^ {0,3}\[(?:[^\]\\\n]|\\.)+\]:[ \t]*(?:\n[ \t]*)?)(<?)([A-Za-z]:\\)/gm;
- packages/client-runtime/src/markdownWindowsPaths.ts lines 14 to 14OutdatedResolvedby SunkenInTime(GitHub profile, opens in a new tab)@@ -0,0 +1,14 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+// A definition, like a fence, can sit inside block quotes and list items.13+const DESTINATION_START_PATTERN =14+ /(?:\]\(\s*|^(?:(?: {0,3}(?:>|[-+*]|\d{1,9}[.)])(?: |$))*) {0,3}\[(?:[^\]\\\n]|\\.)+\]:[ \t]*(?:\n[ \t]*)?)(<?)([A-Za-z]:\\)/gm;
- packages/client-runtime/src/markdownWindowsPaths.ts lines 14 to 14OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,14 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+// A definition, like a fence, can sit inside block quotes and list items.13+const DESTINATION_START_PATTERN =14+ /(?:\]\(\s*|^(?:(?: {0,3}(?:> ?|(?:[-+*]|\d{1,9}[.)])(?: |$)))*) {0,3}\[(?:[^\]\\\n]|\\.)+\]:[ \t]*(?:\n[ \t]*)?)(<?)([A-Za-z]:\\)/gm;packages/client-runtime/src/markdownWindowsPaths.ts line 14OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,14 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+// A definition, like a fence, can sit inside block quotes and list items.13+const DESTINATION_START_PATTERN =14+ /(?:\]\(\s*|^(?:(?: {0,3}(?:> ?|(?:[-+*]|\d{1,9}[.)])(?: |$)))*) {0,3}\[(?:[^\]\\\n]|\\.)+\]:[ \t]*(?:\n[ \t]*)?)(<?)([A-Za-z]:\\)/gm;packages/client-runtime/src/markdownWindowsPaths.ts line 151OutdatedResolvedby SunkenInTime(GitHub profile, opens in a new tab)@@ -0,0 +131,21 @@131+ if (prose.length === 0) return;132+ output.push(normalizeOutsideInlineCode(prose.join("\n")));133+ prose = [];134+ };135+136+ for (const line of lines) {137+ const match = CODE_FENCE_PATTERN.exec(line);138+ const fence = match?.[1];139+ const info = match?.[2] ?? "";140+ if (openFence === null) {141+ // A backtick fence cannot carry a backtick in its info string.142+ if (fence !== undefined && !(fence[0] === "`" && info.includes("`"))) {143+ flushProse();144+ openFence = fence;145+ output.push(line);146+ } else {147+ prose.push(line);148+ }149+ continue;150+ }151+ output.push(line);
- packages/client-runtime/src/markdownWindowsPaths.ts line 14OutdatedResolvedby SunkenInTime(GitHub profile, opens in a new tab)@@ -0,0 +1,14 @@1+/**2+ * CommonMark reads a backslash before ASCII punctuation as an escape, even3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+// A definition, like a fence, can sit inside block quotes and list items.13+const DESTINATION_START_PATTERN =14+ /(?:\]\(\s*|^(?:(?: {0,3}(?:> ?|(?:[-+*]|\d{1,9}[.)])(?: |$)))*) {0,3}\[(?:[^\]\\\n]|\\.)+\]:[ \t]*(?:\n[ \t]*)?)(<?)([A-Za-z]:\\)/gm;packages/client-runtime/src/markdownWindowsPaths.ts lines 147 to 168OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +148,21 @@148+ output.push(line);149+ } else {150+ prose.push(line);151+ }152+ continue;153+ }154+ // A fence opened inside a block quote ends with the quote.155+ if (fenceInQuote && !/^ {0,3}>/.test(line)) {156+ openFence = null;157+ prose.push(line);158+ continue;159+ }160+ output.push(line);161+ if (162+ fence !== undefined &&163+ fence[0] === openFence[0] &&164+ fence.length >= openFence.length &&165+ info.trim() === ""166+ ) {167+ openFence = null;168+ }
- packages/client-runtime/src/markdownWindowsPaths.ts line 23OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +3,21 @@3+ * inside a link or image destination. Agents write Windows paths with their4+ * native separator, so `C:\Users\dara\.t3\shot.png` parses as5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+// A definition, like a fence, can sit inside block quotes and list items.13+const DESTINATION_START_PATTERN =14+ /(?:\]\(\s*|^(?:(?: {0,3}(?:> ?|(?:[-+*]|\d{1,9}[.)])(?: |$)))*) {0,3}\[(?:[^\]\\\n]|\\.)+\]:[ \t]*(?:\n[ \t]*)?)(<?)([A-Za-z]:[\\/])/gm;15+// A backslash before a parenthesis is an escape the parser needs; rewriting it16+// would leave the parenthesis unbalanced and break the link entirely.17+const SEPARATOR_PATTERN = /\\(?![()])/g;18+// A code span opens and closes with backtick runs of the same length. A run19+// next to another backtick is part of a longer run, and a backslash before the20+// opening run escapes its first backtick. Escapes are inert inside a span, so21+// a backslash before the closing run does not. A span cannot cross a blank22+// line, since that ends the paragraph.23+const INLINE_CODE_PATTERN = /(?<![`\\])(`+)[^`](?:(?!\n[ \t]*\n)[\s\S])*?(?<!`)\1(?!`)/g;
- packages/client-runtime/src/markdownWindowsPaths.ts lines 25 to 25OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +5,21 @@5+ * `C:\Users\dara.t3\shot.png` and the asset request asks for a file that does6+ * not exist. Rewriting the separators to `/` before parsing keeps every7+ * character in place: the path is still absolute, the parser has nothing to8+ * unescape, and the text keeps its length so source offsets stay valid.9+ */10+11+// Inline destinations after `](` and reference definitions such as `[id]: C:\...`.12+// A definition, like a fence, can sit inside block quotes and list items.13+const DESTINATION_START_PATTERN =14+ /(?:\]\(\s*|^(?:(?: {0,3}(?:> ?|(?:[-+*]|\d{1,9}[.)])(?: |$)))*) {0,3}\[(?:[^\]\\\n]|\\.)+\]:[ \t]*(?:\n[ \t]*)?)(<?)([A-Za-z]:[\\/])/gm;15+// A backslash before a parenthesis is an escape the parser needs; rewriting it16+// would leave the parenthesis unbalanced and break the link entirely.17+const SEPARATOR_PATTERN = /\\(?![()])/g;18+// A code span opens and closes with backtick runs of the same length. A run19+// next to another backtick is part of a longer run, and a backslash before the20+// opening run escapes its first backtick, unless that backslash is itself21+// escaped, so only an odd run of backslashes counts. Escapes are inert inside22+// a span, so a backslash before the closing run does not. A span cannot cross23+// a blank line, since that ends the paragraph.24+const INLINE_CODE_PATTERN =25+ /(?<!`)(?<!(?<!\\)(?:\\\\)*\\)(`+)[^`](?:(?!\n[ \t]*\n)[\s\S])*?(?<!`)\1(?!`)/g;
- packages/client-runtime/src/markdownWindowsPaths.ts line 169OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +149,21 @@149+ const info = match?.[2] ?? "";150+ if (openFence === null) {151+ // A backtick fence cannot carry a backtick in its info string.152+ if (fence !== undefined && !(fence[0] === "`" && info.includes("`"))) {153+ flushProse();154+ openFence = fence;155+ fenceInQuote = /^ {0,3}>/.test(line);156+ fenceColumn = line.indexOf(fence);157+ output.push(line);158+ } else {159+ prose.push(line);160+ }161+ continue;162+ }163+ // A fence opened inside a block quote ends with the quote, and an indented164+ // fence ends with its list item or indented code block: the first non-blank165+ // line that sits left of the fence run is outside that container.166+ const indent = line.length - line.trimStart().length;167+ if (168+ (fenceInQuote && !/^ {0,3}>/.test(line)) ||169+ (!fenceInQuote && fenceColumn > 0 && indent < fenceColumn && line.trim() !== "")
- packages/client-runtime/src/markdownWindowsPaths.ts lines 91 to 95OutdatedResolvedby SunkenInTime(GitHub profile, opens in a new tab)@@ -0,0 +75,21 @@75+ if (end < 0 || (lineEnd >= 0 && lineEnd < end)) continue;76+ length = end - start;77+ // Parentheses need no escaping inside angle brackets.78+ separators = /\\/g;79+ } else {80+ // A backslash before a parenthesis is a separator when reading it that81+ // way still yields a balanced destination that the link can close after,82+ // which repairs `\(old)\` segments the parser would otherwise cut short.83+ // Otherwise it stays an escape, so a link that renders today keeps84+ // rendering with the same extent.85+ const escaped = bareDestination(segment, start, true);86+ const literal = bareDestination(segment, start, false);87+ const afterLiteral = segment[start + literal.length];88+ const afterEscapedClose = segment[start + escaped.length + 1];89+ // The escaped reading is bogus when the `)` it stops at is followed by90+ // more path text, since that `)` cannot be the link's own closer.91+ const escapedCutShort =92+ segment[start + escaped.length] === ")" &&93+ afterEscapedClose !== undefined &&94+ afterEscapedClose !== ")" &&95+ !/\s/.test(afterEscapedClose);
juliusmarminge reviewed
@@ -792,3 +792,9 @@792792 expect(html).toContain("chat-markdown-file-link");793793 });794794795+ it.each([true, false])(796+ "keeps backslash escapes out of dotfile paths with parseRawHtml=%s",797+ (parseRawHtml) => {798+ const html = renderToStaticMarkup(799+ <ChatMarkdown800+ cwd="C:/Users/shawn/project"github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
juliusmarminge reviewed
@@ -0,0 +14,21 @@14+import { parse, postprocess, preprocess } from "micromark";15+16+const WINDOWS_DRIVE_PATH = /^[A-Za-z]:[\\/]/;17+const DESTINATION_TOKEN_TYPES = new Set([18+ "resourceDestinationString",19+ "definitionDestinationString",20+]);21+// In a bare destination a backslash before a parenthesis is an escape the22+// parser has already used to find the destination's end; rewriting it would23+// unbalance the destination and break the link.24+const BARE_SEPARATOR_PATTERN = /\\(?![()])/g;25+// Parentheses need no escaping inside angle brackets.26+const ANGLE_SEPARATOR_PATTERN = /\\/g;27+28+/**29+ * Rewrites Windows drive-letter link, image, and reference destinations to30+ * forward slashes so backslash escapes cannot corrupt the path during parsing.31+ * Everything else, including code, is left as written. The result has the32+ * same length as the input.33+ */34+export function normalizeWindowsMarkdownDestinations(markdown: string): string {- apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts line 546OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -520,0 +526,21 @@526+ * Every backslash in a Windows path is a separator, so link and image paths go back to527+ * the destination as written in `markdown`.528+ */529+export function nativeMarkdownWithAuthoredWindowsPaths(530+ node: MarkdownNode,531+ markdown: string,532+): MarkdownNode {533+ if (!markdown.includes("\\")) return node;534+ const restore = (current: MarkdownNode): MarkdownNode => {535+ const href = current.href && authoredWindowsPath(current.href, markdown);536+ const children = current.children?.map(restore);537+ return { ...current, ...(href ? { href } : {}), ...(children ? { children } : {}) };538+ };539+ return restore(node);540+}541+542+function authoredWindowsPath(parsed: string, markdown: string): string | undefined {543+ if (!/^(?:[A-Za-z]:|\\)/.test(parsed)) return undefined;544+ // Any punctuation the parser kept may have followed a backslash it dropped.545+ const path = parsed.replace(MARKDOWN_PUNCTUATION_PATTERN, (character) => `\\\\?\\${character}`);546+ const authored = new RegExp(String.raw`\](?:\(|:)\s*<?(${path})`).exec(markdown)?.[1];
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed