Repository navigation
fix(web): images and file links render when a Windows path has a space #27970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -380,8 +380,66 @@ function markdownLinkDestinationScanner(markdown: string) { | |||||
| }; | ||||||
| } | ||||||
|
|
||||||
| export function extractMarkdownLinkHrefs(markdown: string): string[] { | ||||||
| // Stands in for a space inside a drive path destination while CommonMark parses it. One UTF-16 | ||||||
| // unit, so every source offset the renderers read stays where it was. | ||||||
| const DESTINATION_SPACE = "\uE020"; | ||||||
|
|
||||||
| // `](C:\Users\Jo Doe\shot.png)` or with a title. Parentheses inside the path must balance. | ||||||
| const SPACED_DRIVE_DESTINATION = | ||||||
| /\]\([ \t]*([A-Za-z]:[\\/](?:[^()\n]|\([^()\n]*\))*?)(?=(?:[ \t]+"[^"\n]*")?[ \t]*\))/g; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep single-quoted titles outside the destination. For
|
||||||
| const HAS_SPACED_DRIVE_DESTINATION = /\]\([ \t]*[A-Za-z]:[\\/][^)\n]* [^)\n]*\)/; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High
Suggested change
🤖 Copy this AI Prompt to have your agent fix this:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Detect spaces after parenthesized path components. For
|
||||||
| const CODE_FENCE = /^ {0,3}(`{3,}|~{3,})/; | ||||||
| const INLINE_CODE = /(`+)[^`]*?\1/g; | ||||||
|
|
||||||
| function protectDestinationsOutsideInlineCode(line: string): string { | ||||||
| let result = ""; | ||||||
| let cursor = 0; | ||||||
| const protect = (segment: string) => | ||||||
| segment.replace( | ||||||
| SPACED_DRIVE_DESTINATION, | ||||||
| (match: string, destination: string) => | ||||||
| match.slice(0, match.length - destination.length) + | ||||||
| destination.replaceAll(" ", DESTINATION_SPACE), | ||||||
| ); | ||||||
| for (const code of line.matchAll(INLINE_CODE)) { | ||||||
| result += protect(line.slice(cursor, code.index)) + code[0]; | ||||||
| cursor = code.index + code[0].length; | ||||||
| } | ||||||
| return result + protect(line.slice(cursor)); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * CommonMark ends a bare destination at its first space, so `` | ||||||
| * renders as text. Agents write Windows paths that way, so a drive path destination keeps its | ||||||
| * spaces until the parser has read it. Code stays as written. | ||||||
| */ | ||||||
| export function protectSpacedWindowsDestinations(markdown: string): string { | ||||||
| if (!HAS_SPACED_DRIVE_DESTINATION.test(markdown)) return markdown; | ||||||
| let fence: string | null = null; | ||||||
| return markdown | ||||||
| .split("\n") | ||||||
| .map((line) => { | ||||||
| const marker = CODE_FENCE.exec(line)?.[1]; | ||||||
| if (fence !== null) { | ||||||
| if (marker && marker[0] === fence[0] && marker.length >= fence.length) fence = null; | ||||||
| return line; | ||||||
| } | ||||||
| if (marker) { | ||||||
| fence = marker; | ||||||
| return line; | ||||||
| } | ||||||
| return line.includes("](") ? protectDestinationsOutsideInlineCode(line) : line; | ||||||
| }) | ||||||
| .join("\n"); | ||||||
| } | ||||||
|
|
||||||
| export function restoreWindowsDestinationSpaces(value: string): string { | ||||||
| return value.includes(DESTINATION_SPACE) ? value.replaceAll(DESTINATION_SPACE, " ") : value; | ||||||
| } | ||||||
|
|
||||||
| export function extractMarkdownLinkHrefs(source: string): string[] { | ||||||
| const hrefs: string[] = []; | ||||||
| const markdown = protectSpacedWindowsDestinations(source); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract the full path when its folder name contains parentheses. Protection preserves
|
||||||
| const scanDestination = markdownLinkDestinationScanner(markdown); | ||||||
| let cursor = 0; | ||||||
| while (cursor < markdown.length) { | ||||||
|
|
@@ -396,7 +454,7 @@ export function extractMarkdownLinkHrefs(markdown: string): string[] { | |||||
| if (!destination) continue; | ||||||
| cursor = destination.end; | ||||||
| const href = markdown.slice(destination.hrefStart, destination.hrefEnd).trim(); | ||||||
| if (href) hrefs.push(href); | ||||||
| if (href) hrefs.push(restoreWindowsDestinationSpaces(href)); | ||||||
| } | ||||||
| return hrefs; | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
src/markdownLinks.ts:389extractMarkdownLinkHrefsreturnsC:/work/file.ts 'Title'for a link with a single-quoted title, so callers try to resolve a nonexistent path.SPACED_DRIVE_DESTINATIONrecognizes only double-quoted titles and therefore protects the space before'Title'as part of the destination; recognize single-quoted titles as well.🤖 Copy this AI Prompt to have your agent fix this: