TypeScript
fix(mobile): Windows paths in messages keep their backslashes (#21454)
Julius Marminge294981fa724e
diff --git a/apps/mobile/deps/README.md b/apps/mobile/deps/README.mdindex 46b0539e5c9..939ac2d9147 100644--- a/apps/mobile/deps/README.md+++ b/apps/mobile/deps/README.md@@ -1,3 +1,3 @@-The Enriched Markdown tarball comes from Julius's [`fork/t3-mobile` branch](https://github.com/pingdotgg/enriched-markdown/tree/fork/t3-mobile). Its `t3-fork.json` records the exact source commit and upstream base. React block renderers and signed image sources (`renderers`, `resolveImageSource`) are upstream. The fork carries native streaming memory bounds (an 8 MiB measurement-key budget and coalesced pending renders), plus changes with an open upstream PR: the `onLinksChange` link list that link pills and iOS link menus read ([#1088](https://github.com/software-mansion/enriched-markdown/pull/1088)), Android inline image alignment ([#1089](https://github.com/software-mansion/enriched-markdown/pull/1089)), clipboard link metadata ([#962](https://github.com/software-mansion/enriched-markdown/pull/962)), and development-host autolinks ([#961](https://github.com/software-mansion/enriched-markdown/pull/961)).+The Enriched Markdown tarball comes from Julius's [`fork/t3-mobile` branch](https://github.com/pingdotgg/enriched-markdown/tree/fork/t3-mobile). Its `t3-fork.json` records the exact source commit and upstream base. React block renderers and signed image sources (`renderers`, `resolveImageSource`) are upstream. The fork carries native streaming memory bounds (an 8 MiB measurement-key budget and coalesced pending renders), plus changes with an open upstream PR: the `onLinksChange` link list that link pills and iOS link menus read ([#1088](https://github.com/software-mansion/enriched-markdown/pull/1088)), Android inline image alignment ([#1089](https://github.com/software-mansion/enriched-markdown/pull/1089)), clipboard link metadata ([#962](https://github.com/software-mansion/enriched-markdown/pull/962)), development-host autolinks ([#961](https://github.com/software-mansion/enriched-markdown/pull/961)), and `md4cFlags.preserveWindowsPaths`, which keeps backslashes in Windows drive paths in prose ([#1105](https://github.com/software-mansion/enriched-markdown/pull/1105), after [md4c#5](https://github.com/software-mansion-labs/md4c/pull/5)).To replace it, commit the fork changes, run `yarn install --immutable` and `yarn workspace react-native-enriched-markdown prepare` in the fork checkout, then `node scripts/pack-t3.mjs /path/to/t3code/apps/mobile/deps`. Keep the package version aligned with Julius's `fork/t3-mobile` branch. Rename the archive to include the first eight characters of the source commit, such as `react-native-enriched-markdown-1.2.0-t3.16-8d2f84a8.tgz`, so each build has a distinct dependency path without version bumps. Update the tarball path in `apps/mobile/package.json` and `pnpm-workspace.yaml`'s `allowBuilds`, and run `vp i` to update the lockfile. Native clients need rebuilding when this dependency changes.diff --git a/apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz b/apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgznew file mode 100644index 00000000000..06ac560ff2eBinary files /dev/null and b/apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz differdiff --git a/apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz b/apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgzdeleted file mode 100644index 3cf77c4adf9..00000000000Binary files a/apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz and /dev/null differdiff --git a/apps/mobile/package.json b/apps/mobile/package.jsonindex 42d81c8adad..f5c1c652181 100644--- a/apps/mobile/package.json+++ b/apps/mobile/package.json@@ -121,7 +121,7 @@"react-dom": "catalog:","react-native": "0.88.0","react-native-blob-util": "0.25.1",- "react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz",+ "react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz","react-native-gesture-handler": "~3.3.0","react-native-image-viewing": "^0.2.2","react-native-keyboard-controller": "1.22.8",diff --git a/apps/mobile/src/lib/markdownDocument.ts b/apps/mobile/src/lib/markdownDocument.tsindex 7f3e2b7d77e..619ed8330a8 100644--- a/apps/mobile/src/lib/markdownDocument.ts+++ b/apps/mobile/src/lib/markdownDocument.ts@@ -2,9 +2,10 @@ import type { Root, RootContent } from "mdast";import { unified, type Processor } from "unified";import remarkParse from "remark-parse";import remarkGfm from "remark-gfm";+import { remarkKeepWindowsProsePaths } from "@t3tools/shared/markdownWindowsPaths";import { isWindowsAbsolutePath } from "@t3tools/shared/path";-const parser = unified().use(remarkParse).use(remarkGfm);+const parser = unified().use(remarkParse).use(remarkGfm).use(remarkKeepWindowsProsePaths);export type MarkdownNode = Root | RootContent;interface MarkdownEdit {diff --git a/apps/mobile/src/lib/markdownHtml.test.ts b/apps/mobile/src/lib/markdownHtml.test.tsindex 7c6a5e4bccb..f097d8047e8 100644--- a/apps/mobile/src/lib/markdownHtml.test.ts+++ b/apps/mobile/src/lib/markdownHtml.test.ts@@ -330,3 +330,11 @@ describe("Windows paths in native Markdown", () => {expect(markdownImageSources(markdown)).toEqual([image, paren, amp, htmlAmp]);});});++it("keeps backslashes in drive paths in mobile Markdown prose", () => {+ const path = String.raw`Saved to C:\Users\me\.t3\_build\shot.png and C:/work\.config.`;++ expect(parseMarkdownDocument(String.raw`${path} Then \*this\*.`).children).toMatchObject([+ { type: "paragraph", children: [{ type: "text", value: `${path} Then *this*.` }] },+ ]);+});diff --git a/apps/mobile/src/native/EnrichedMarkdownText.tsx b/apps/mobile/src/native/EnrichedMarkdownText.tsxindex 429b1595aa1..76bebf4c6b3 100644--- a/apps/mobile/src/native/EnrichedMarkdownText.tsx+++ b/apps/mobile/src/native/EnrichedMarkdownText.tsx@@ -337,6 +337,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {latexMath: mathRenderingEnabled,hardSoftBreaks: props.preserveSoftBreaks ?? false,admonitions: true,+ preserveWindowsPaths: true,}}enableTaskListItemToggle={false}enableCodeBlockWrapTogglediff --git a/packages/shared/package.json b/packages/shared/package.jsonindex be5dbfe9ebd..54ad60d92cb 100644--- a/packages/shared/package.json+++ b/packages/shared/package.json@@ -463,6 +463,10 @@"types": "./src/proposedPlanText.ts","import": "./src/proposedPlanText.ts"},+ "./markdownWindowsPaths": {+ "types": "./src/markdownWindowsPaths.ts",+ "import": "./src/markdownWindowsPaths.ts"+ },"./markdownPipeline": {"types": "./src/markdownPipeline.ts","import": "./src/markdownPipeline.ts"diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.tsindex 6e6bba7f7c2..275b93b562f 100644--- a/packages/shared/src/markdownPipeline.ts+++ b/packages/shared/src/markdownPipeline.ts@@ -1,6 +1,5 @@import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";-import type { Processor } from "unified";-import { isWindowsAbsolutePath } from "./path.ts";+import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts";import type { PluggableList } from "unified";import rehypeRaw from "rehype-raw";import rehypeSanitize, { defaultSchema } from "rehype-sanitize";@@ -302,52 +301,6 @@ function remarkNormalizeLinksAndTagInlineCode() {};}-interface DestinationCompileContext {- readonly stack: ReadonlyArray<{ readonly type: string; url?: string }>;- resume(): string;- sliceSerialize(token: unknown): string;-}--function keepWindowsPathDestination(this: DestinationCompileContext, token: unknown) {- const decoded = this.resume();- const authored = this.sliceSerialize(token);- const node = this.stack.at(-1);- // Character references still need decoding, so those destinations keep the parsed URL.- if (node)- node.url = isWindowsAbsolutePath(authored) && !authored.includes("&") ? authored : decoded;-}--const UNFINISHED_DRIVE_PATH = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/;--interface EscapeCompileContext {- readonly stack: ReadonlyArray<{- readonly type: string;- children?: Array<{ type: string; value?: string }>;- }>;-}--function keepWindowsPathEscape(this: EscapeCompileContext) {- const text = this.stack.at(-1)?.children?.at(-1);- if (text?.type !== "text" || text.value === undefined) return;- const before = text.value.slice(0, -1);- if (UNFINISHED_DRIVE_PATH.test(before)) text.value = `${before}\\${text.value.slice(-1)}`;-}--/**- * CommonMark treats backslashes as escapes; preserve them as separators in Windows destinations- * and drive-path prose. Prose paths end at whitespace so later escapes still work.- */-function remarkKeepWindowsPaths(this: Processor) {- const data = this.data();- (data.fromMarkdownExtensions ??= []).push({- exit: {- resourceDestinationString: keepWindowsPathDestination,- definitionDestinationString: keepWindowsPathDestination,- characterEscape: keepWindowsPathEscape,- },- });-}-export function shouldPreserveAssistantLineBreaks(text: string): boolean {return /^★ Insight(?:\s|─)/mu.test(text);}diff --git a/packages/shared/src/markdownWindowsPaths.ts b/packages/shared/src/markdownWindowsPaths.tsnew file mode 100644index 00000000000..a1569eb1f83--- /dev/null+++ b/packages/shared/src/markdownWindowsPaths.ts@@ -0,0 +1,67 @@+import type { Processor } from "unified";+import { isWindowsAbsolutePath } from "./path.ts";++interface DestinationCompileContext {+ readonly stack: ReadonlyArray<{ readonly type: string; url?: string }>;+ resume(): string;+ sliceSerialize(token: unknown): string;+}++function keepWindowsPathDestination(this: DestinationCompileContext, token: unknown) {+ const decoded = this.resume();+ const authored = this.sliceSerialize(token);+ const node = this.stack.at(-1);+ // Character references still need decoding, so those destinations keep the parsed URL.+ if (node)+ node.url = isWindowsAbsolutePath(authored) && !authored.includes("&") ? authored : decoded;+}++// A drive path that has not reached whitespace yet: `C:`, `C:\Users\me`, `C:/work`.+const UNFINISHED_DRIVE_PATH = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/;++interface EscapeCompileContext {+ readonly stack: ReadonlyArray<{+ readonly type: string;+ children?: Array<{ type: string; value?: string }>;+ }>;+}++function keepWindowsPathEscape(this: EscapeCompileContext) {+ const text = this.stack.at(-1)?.children?.at(-1);+ if (text?.type !== "text" || text.value === undefined) return;+ const before = text.value.slice(0, -1);+ if (UNFINISHED_DRIVE_PATH.test(before)) {+ text.value = `${before}\\${text.value.slice(-1)}`;+ }+}++function keepWindowsProsePathEscape(this: EscapeCompileContext) {+ // Destinations, titles, and labels are buffered in a fragment.+ if (this.stack.at(-1)?.type !== "fragment") keepWindowsPathEscape.call(this);+}++/**+ * Keeps escapes inside drive paths in prose only, for parsers that already escape Windows+ * destinations themselves. Prose paths end at whitespace: past that, nothing tells a path+ * from a sentence, and `C:\x and \*this\*` must still read `*this*`.+ */+export function remarkKeepWindowsProsePaths(this: Processor) {+ (this.data().fromMarkdownExtensions ??= []).push({+ exit: { characterEscape: keepWindowsProsePathEscape },+ });+}++/**+ * CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape. Every backslash in a+ * Windows path is a separator, so link, image, and definition destinations that are+ * Windows paths keep the text as written, and so do escapes inside drive paths.+ */+export function remarkKeepWindowsPaths(this: Processor) {+ (this.data().fromMarkdownExtensions ??= []).push({+ exit: {+ resourceDestinationString: keepWindowsPathDestination,+ definitionDestinationString: keepWindowsPathDestination,+ characterEscape: keepWindowsPathEscape,+ },+ });+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex ba6cfe4dee3..21c84a0f925 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -732,8 +732,8 @@ importers:specifier: 0.25.1version: 0.25.1([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])react-native-enriched-markdown:- specifier: file:deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz- version: file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ specifier: file:deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz+ version: file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])react-native-gesture-handler:specifier: 3.3.0version: 3.3.0(patch_hash=abe20e1d1c23549297cbbc818e14fe479b41455930d9663135d6b40fe359b626)([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])@@ -10824,8 +10824,8 @@ packages:react: '*'react-native: '*'- react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz:- resolution: {integrity: sha512-Dl8Raid4175mHijMpsIRLB3yXBZ1s04YxaQoIdHwy64KPHBVdt26x2OYRhYyjMM3VEo4U24AAYNXUh271zYDQw==, tarball: file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz}+ react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz:+ resolution: {integrity: sha512-Ru0Uuhq4QBphcC73ney0fgNnvr6Lfv+31H2Ij1qxvDejwVyFh/Romc1a1ot9i8/jnmgvAzTZzbPQcdGtQNFayw==, tarball: file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz}version: 1.2.0-t3.16peerDependencies:katex: '>=0.16.0'@@ -21087,7 +21087,7 @@ snapshots:react: 19.3.0react-native: 0.88.0(@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected])- react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]):+ react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]):dependencies:react: 19.3.0react-native: 0.88.0(@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected])diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex 4c273bae754..a07ad2b008d 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -23,7 +23,7 @@ allowBuilds:node-pty: true# Ships N-API prebuilds; its install script only rebuilds from source.objc-js: false- "react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-558767d9.tgz": true+ "react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz": truesharp: true"@tsc-rs/typescript": trueutf-8-validate: false