GeezerVison is showing github.com — fix(mobile): Windows image paths and non-ASCII file links open like d… · pingdotgg/t3code@1a4f3c4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/markdownDocument.ts +54-4Lines changed: 54 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { Root, RootContent } from "mdast"; | 2 | | -import { unified } from "unified"; | | 2 | +import { unified, type Processor } from "unified"; | 3 | 3 | import remarkParse from "remark-parse"; | 4 | 4 | import remarkGfm from "remark-gfm"; | | 5 | +import { isWindowsAbsolutePath } from "@t3tools/shared/path"; | 5 | 6 |
| 6 | 7 | const parser = unified().use(remarkParse).use(remarkGfm); | 7 | 8 | export type MarkdownNode = Root | RootContent; | @@ -382,13 +383,62 @@ export function markdownWithTableBreaks(markdown: string): string { | 382 | 383 | return transformNativeMarkdown(markdown, undefined, true); | 383 | 384 | } | 384 | 385 |
| | 386 | +// `>` covers a destination continued on a blockquote line. | | 387 | +const WINDOWS_DESTINATION = /\](?:\(|:)[\s>]*<?(?:[A-Za-z]:[\\/]|\\\\)/; | | 388 | + | | 389 | +interface DestinationToken { | | 390 | + readonly start: { readonly offset: number }; | | 391 | + readonly end: { readonly offset: number }; | | 392 | +} | | 393 | + | | 394 | +/** | | 395 | + * Markdown reads `C:\work\.t3` as `C:\work.t3`. Web keeps authored Windows destinations | | 396 | + * literally (see `remarkKeepWindowsPaths` in shared), so escape their backslashes to give | | 397 | + * both parsers here the same path. `\&` stays an escape, as `markdownWithHtml` emits it. | | 398 | + * Expects authored Markdown: already escaped output would be escaped twice. | | 399 | + */ | | 400 | +function markdownWithWindowsPathDestinations(markdown: string): string { | | 401 | + // Skips the extra parse for the common message that has no Windows destination. | | 402 | + if (!WINDOWS_DESTINATION.test(markdown)) return markdown; | | 403 | + const edits: MarkdownEdit[] = []; | | 404 | + function record( | | 405 | + this: { resume(): string; sliceSerialize(token: DestinationToken): string }, | | 406 | + token: DestinationToken, | | 407 | + ) { | | 408 | + this.resume(); | | 409 | + const authored = this.sliceSerialize(token); | | 410 | + if (!isWindowsAbsolutePath(authored) || !authored.includes("\\") || /[<>]/.test(authored)) | | 411 | + return; | | 412 | + const start = token.start.offset; | | 413 | + const end = token.end.offset; | | 414 | + const escaped = authored.replace(/\\(?!&)/g, "\\\\"); | | 415 | + // A bare destination ends at an unescaped `)`, so wrap it to keep `\)` inside. | | 416 | + const angled = markdown[start - 1] === "<"; | | 417 | + edits.push({ start, end, content: angled ? escaped : `<${escaped}>` }); | | 418 | + } | | 419 | + unified() | | 420 | + .use(remarkParse) | | 421 | + .use(remarkGfm) | | 422 | + .use(function (this: Processor) { | | 423 | + (this.data().fromMarkdownExtensions ??= []).push({ | | 424 | + exit: { resourceDestinationString: record, definitionDestinationString: record }, | | 425 | + }); | | 426 | + }) | | 427 | + .parse(markdown); | | 428 | + return applyMarkdownEdits(markdown, edits); | | 429 | +} | | 430 | + | | 431 | +// HTML wrappers can expose Markdown that wasn't in the first AST, so normalize them first. | | 432 | +export function markdownForNativeParsing(markdown: string): string { | | 433 | + return markdownWithWindowsPathDestinations(markdownWithHtml(markdown)); | | 434 | +} | | 435 | + | 385 | 436 | export function parseMarkdownDocument(markdown: string): Root { | 386 | | - return parser.parse(markdownWithHtml(markdown)); | | 437 | + return parser.parse(markdownForNativeParsing(markdown)); | 387 | 438 | } | 388 | 439 |
| 389 | 440 | export function markdownForNativeRendering(markdown: string, fileLink: RegExp): string { | 390 | | - // HTML wrappers can expose Markdown that wasn't in the first AST, so normalize them first. | 391 | | - return transformNativeMarkdown(markdownWithHtml(markdown), fileLink, true); | | 441 | + return transformNativeMarkdown(markdownForNativeParsing(markdown), fileLink, true); | 392 | 442 | } | 393 | 443 |
| 394 | 444 | /** Native media slots only cover standalone images; nested file embeds must become links. */ | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/markdownHtml.test.ts +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, expect, it } from "vite-plus/test"; | 2 | 2 | import { | | 3 | + markdownForNativeRendering, | 3 | 4 | markdownHtmlContent, | 4 | 5 | markdownWithHtml, | 5 | 6 | markdownWithFileEmbeds, | @@ -9,6 +10,7 @@ import { | 9 | 10 | import { markdownImageSources } from "./markdownImageSources"; | 10 | 11 | import { markdownFileEmbedLabel, resolveMarkdownMediaPreview } from "./markdownMedia"; | 11 | 12 | import { resolveMarkdownLinkPresentation } from "./markdownLinks"; | | 13 | +import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation"; | 12 | 14 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; | 13 | 15 | import { parseMarkdown } from "react-native-enriched-markdown/src/web/parseMarkdown"; | 14 | 16 |
| @@ -296,3 +298,35 @@ describe("file embeds in native thread and file Markdown", () => { | 296 | 298 | expect(converted.fileHrefs.has(href)).toBe(true); | 297 | 299 | }); | 298 | 300 | }); | | 301 | + | | 302 | +describe("Windows paths in native Markdown", () => { | | 303 | + // regression: #23722 Markdown escapes ate the separator before `.t3` and `_build` | | 304 | + it("keeps backslashes in Windows image and link destinations", async () => { | | 305 | + const image = String.raw`C:\work\.t3\_build\shot.png`; | | 306 | + const link = String.raw`\\server\share\.t3\notes.md`; | | 307 | + const markdown = [ | | 308 | + ``, | | 309 | + String.raw`b.png)`, | | 310 | + `[notes](<${link}>)`, | | 311 | + `<img src="${image}">`, | | 312 | + String.raw``, | | 313 | + String.raw`<img src="C:\R&D\.t3\b.png">`, | | 314 | + "> ", | | 315 | + ].join("\n\n"); | | 316 | + | | 317 | + const native = await parseMarkdown( | | 318 | + markdownForNativeRendering(markdown, ENRICHED_INLINE_FILE_LINK_REGEX), | | 319 | + ); | | 320 | + const urls: string[] = []; | | 321 | + const collect = (node: { attributes?: { url?: string }; children?: unknown[] }) => { | | 322 | + if (node.attributes?.url) urls.push(node.attributes.url); | | 323 | + node.children?.forEach((child) => collect(child as typeof node)); | | 324 | + }; | | 325 | + collect(native); | | 326 | + const paren = String.raw`C:\a\)b.png`; | | 327 | + const amp = String.raw`C:\R&D\.t3\a&b.png`; | | 328 | + const htmlAmp = String.raw`C:\R&D\.t3\b.png`; | | 329 | + expect(urls).toEqual([image, paren, link, image, amp, htmlAmp, image]); | | 330 | + expect(markdownImageSources(markdown)).toEqual([image, paren, amp, htmlAmp]); | | 331 | + }); | | 332 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/markdownInlineFileLinks.test.ts +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,7 +7,6 @@ import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation"; | 7 | 7 | import { | 8 | 8 | markdownForNativeRendering, | 9 | 9 | markdownWithInlineFileLinks, | 10 | | - parseMarkdownDocument, | 11 | 10 | markdownWithHtml, | 12 | 11 | } from "./markdownDocument"; | 13 | 12 |
| @@ -87,7 +86,7 @@ describe("inline code that names a file", () => { | 87 | 86 |
| 88 | 87 | it("links to the exact path when it holds backslashes or ampersands", () => { | 89 | 88 | for (const path of ["C:\\repo\\_private\\file.ts", "./src/a©.ts", "./src/[id].ts"]) { | 90 | | - const paragraph = parseMarkdownDocument(rewrite(`\`${path}\``)).children[0]; | | 89 | + const paragraph = parser.parse(rewrite(`\`${path}\``)).children[0]; | 91 | 90 | const link = paragraph?.type === "paragraph" ? paragraph.children[0] : undefined; | 92 | 91 | expect(link?.type === "link" && link.url).toBe(path); | 93 | 92 | expect( | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/markdownLinks.ts +1-5Lines changed: 1 addition & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -265,11 +265,7 @@ export function resolveMarkdownLinkPresentation(href: string): MarkdownLinkPrese | 265 | 265 | } | 266 | 266 | } catch {} | 267 | 267 |
| 268 | | - const target = | 269 | | - parseMarkdownFileLink(normalized) ?? | 270 | | - (/^(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\/)+$/.test(normalized) | 271 | | - ? { path: normalized } | 272 | | - : null); | | 268 | + const target = parseMarkdownFileLink(normalized); | 273 | 269 | if (target) { | 274 | 270 | return { | 275 | 271 | kind: "file", | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/native/EnrichedMarkdownText.tsx +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | import { | 8 | 8 | markdownForNativeRendering, | 9 | 9 | markdownWithFileEmbeds, | 10 | | - markdownWithHtml, | | 10 | + markdownForNativeParsing, | 11 | 11 | markdownWithInlineFileLinks, | 12 | 12 | markdownWithTableBreaks, | 13 | 13 | } from "../lib/markdownDocument"; | @@ -165,7 +165,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) { | 165 | 165 | }; | 166 | 166 | } | 167 | 167 | const content = markdownWithInlineFileLinks( | 168 | | - markdownWithHtml(props.markdown), | | 168 | + markdownForNativeParsing(props.markdown), | 169 | 169 | ENRICHED_INLINE_FILE_LINK_REGEX, | 170 | 170 | ); | 171 | 171 | const files = markdownWithFileEmbeds(content, fileEmbedLabel); | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/markdownLinks.test.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,15 @@ describe("parseMarkdownFileLink", () => { | 54 | 54 | expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/"); | 55 | 55 | }); | 56 | 56 |
| | 57 | + // regression: #16096 relative links with non-ASCII names open as files | | 58 | + it("recognizes relative paths with non-ASCII names", () => { | | 59 | + expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe( | | 60 | + "pastexam/線性代數/exam_易志偉.pdf", | | 61 | + ); | | 62 | + expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf"); | | 63 | + expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份"); | | 64 | + }); | | 65 | + | 57 | 66 | it("accepts conventional extensionless names with or without a position", () => { | 58 | 67 | expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" }); | 59 | 68 | expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/markdownLinks.ts +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,13 +14,14 @@ import { | 14 | 14 |
| 15 | 15 | const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/; | 16 | 16 |
| 17 | | -// The final segment is optional so an explicit folder link may end in `/` | 18 | | -// (`patches/`, `docs/My Folder/`). A bare name with no slash still fails. | | 17 | +// Names may use any script (`線性代數/考試.pdf`). The final segment is optional so an | | 18 | +// explicit folder link may end in `/` (`patches/`, `docs/My Folder/`). A bare name with no | | 19 | +// slash still fails. | 19 | 20 | const RELATIVE_FILE_PATH_PATTERN = | 20 | | - /^(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\/)+(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*(?::\d+){0,2})?$/; | | 21 | + /^(?:[\p{L}\p{M}\p{N}._-]+(?: +[\p{L}\p{M}\p{N}._-]+)*\/)+(?:[\p{L}\p{M}\p{N}._-]+(?: +[\p{L}\p{M}\p{N}._-]+)*(?::\d+){0,2})?$/u; | 21 | 22 |
| 22 | 23 | const RELATIVE_FILE_NAME_PATTERN = | 23 | | - /^[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\.[A-Za-z0-9_-]+(?::\d+){0,2}$/; | | 24 | + /^[\p{L}\p{M}\p{N}._-]+(?: +[\p{L}\p{M}\p{N}._-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+){0,2}$/u; | 24 | 25 |
| 25 | 26 | const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/; | 26 | 27 |
| |
|