Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
933fabf
fix(clients): render local media embeds and file links whose paths co…
anaremore Nov 22, 2026
fa4f157
fix(shared): index spaced-path ranges, skip fenced backticks, and pre…
anaremore Nov 22, 2026
7225a82
fix(shared): allow inline code inside repaired spaced-path link labels
anaremore Nov 22, 2026
8c3c4a9
fix(shared): preserve bracket state across multiline inline code in l…
anaremore Nov 22, 2026
8bebfb3
fix(shared): also repair relative spaced local media and file paths
anaremore Nov 24, 2026
8062d3e
Merge remote-tracking branch 'origin/main' into fix/markdown-spaced-f…
juliusmarminge Nov 27, 2026
f2be706
test(shared): drop duplicate import after merge and share link collector
juliusmarminge Nov 27, 2026
9f70029
Merge remote-tracking branch 'origin/main' into fix/markdown-spaced-f…
juliusmarminge Nov 27, 2026
d869d2d
perf(shared): stop spaced-path scans at the next link destination
juliusmarminge Nov 27, 2026
7308b2b
Merge remote-tracking branch 'origin/main' into fix/markdown-spaced-f…
anaremore Nov 29, 2026
1e08c37
fix(shared): keep quoted titles after spaced local paths
anaremore Nov 30, 2026
d3238e4
Merge remote-tracking branch 'origin/main' into fix/markdown-spaced-f…
anaremore Dec 1, 2026
1f3eb73
fix(shared): repair linked spaced images, escaped parens, and unclose…
anaremore Dec 2, 2026
5a0a176
fix(shared): keep a spaced-path repair only when it renders a link or…
anaremore Dec 3, 2026
11d8c57
fix(shared): repair spaced paths in Markdown that starts with a BOM
anaremore Dec 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/mobile/src/lib/markdownDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Root, RootContent } from "mdast";
import { unified, type Processor } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import { markdownWithSpacedFilePaths } from "@t3tools/shared/markdownSpacedFilePaths";
import { remarkKeepWindowsProsePaths } from "@t3tools/shared/markdownWindowsPaths";
import {
isBareLink,
Expand Down Expand Up @@ -563,7 +564,10 @@ function markdownWithImageParagraphs(markdown: string): string {

// HTML wrappers can expose Markdown that wasn't in the first AST, so normalize them first.
function normalizedMarkdown(markdown: string): string {
return markdownWithWindowsPathDestinations(markdownWithHtml(markdown));
const content = markdownWithHtml(markdown);
return markdownWithWindowsPathDestinations(
markdownWithSpacedFilePaths(content, (markdown) => parser.parse(markdown)),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
);
}

const autolinkParser = unified()
Expand Down
21 changes: 21 additions & 0 deletions apps/mobile/src/lib/markdownHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation";
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { parseMarkdown } from "react-native-enriched-markdown/src/web/parseMarkdown";

it("renders bare Windows and POSIX spaced media and file paths in native Markdown", () => {
const winSlash = "Z:/Projects/Demo App/Output Shots/bust.jpg";
const winBackslash = String.raw`C:\Users\First Last\.t3\shot.png`;
const mac = "/Users/test/Mobile Documents/shot.png";
const markdown = [
`![Bust](${winSlash})`,
`![Win](${winBackslash})`,
`![Mac](${mac})`,
`[File](C:/Users/First Last/src/app.ts:10)`,
].join("\n\n");
expect(markdownImageSources(markdown)).toEqual([winSlash, winBackslash, mac]);
const rendered = markdownForNativeRendering(markdown, ENRICHED_INLINE_FILE_LINK_REGEX);
expect(rendered).toContain(`![Bust](<${winSlash}>)`);
expect(rendered).toContain(String.raw`![Win](<C:\\Users\\First Last\\.t3\\shot.png>)`);
expect(rendered).toContain(`![Mac](<${mac}>)`);
expect(rendered).toContain(`[File](<C:/Users/First Last/src/app.ts:10>)`);
expect(
markdownForNativeRendering(`\uFEFF![Mac](${mac})`, ENRICHED_INLINE_FILE_LINK_REGEX),
).toContain(`![Mac](<${mac}>)`);
});

describe("HTML in native Markdown", () => {
it("preserves screenshot URLs and captions from GitHub HTML image embeds", () => {
expect(
Expand Down
80 changes: 80 additions & 0 deletions apps/web/src/markdown/wordsHast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,86 @@ describe("wordsHast", () => {
expect(isMarkdownFileLinkLabel(basename, String(anchor.properties.href))).toBe(true);
});

it("renders bare Windows and POSIX media embeds and file links with spaces while leaving code literal", () => {
const winSlash = "Z:/Projects/Demo App/Output Shots/final-bust.jpg";
const winBackslash = String.raw`C:\Users\First Last\.t3\design-review\r6-1.png`;
const macIcloud = "/Users/test/Mobile Documents/iCloud~md~notes/cover photo.png";
const winFileLink = "C:/Users/First Last/Downloads GC/project/src/DetailsTab.tsx:42";
const markdown = [
`![Bust](${winSlash})`,
"",
`![Round 6](${winBackslash})`,
"",
`![Cover](${macIcloud})`,
"",
`[DetailsTab.tsx:42](${winFileLink})`,
"",
`\`![Inline](${winSlash})\``,
"",
"```md",
`![Fence](${macIcloud})`,
"```",
].join("\n");
const html = wordsHtml(markdown);
expect(html).toContain(`data-local-src="Z:/Projects/Demo%20App/Output%20Shots/final-bust.jpg"`);
expect(html).toContain(
String.raw`data-local-src="C:\Users\First%20Last\.t3\design-review\r6-1.png"`,
);
expect(html).toContain(
`src="/Users/test/Mobile%20Documents/iCloud~md~notes/cover%20photo.png"`,
);
expect(html).toContain(
`href="file:///C:/Users/First%20Last/Downloads%20GC/project/src/DetailsTab.tsx:42"`,
);
expect(html).toContain(`<code data-inline-code="" dir="ltr">![Inline](${winSlash})</code>`);
expect(html).toContain(`<code class="language-md">![Fence](${macIcloud})\n</code>`);

// Backticks inside a fenced block must not pair with prose backticks after an image.
expect(wordsHtml("~~~\n`unclosed\n~~~\n![real](/tmp/a b.png)\n`tail`")).toContain(
`src="/tmp/a%20b.png"`,
);
expect(wordsHtml("[Open `README.md`](/tmp/My Project/README.md)")).toContain(
`href="/tmp/My%20Project/README.md"`,
);
expect(wordsHtml("[Open `README\n.md`](/tmp/My Project/README.md)")).toContain(
`href="/tmp/My%20Project/README.md"`,
);
expect(wordsHtml("![a](./img !32.png)\n\n![b](screenshots/my shot (1).png)")).toContain(
`src="./img%20!32.png"`,
);
expect(wordsHtml("![b](screenshots/my shot (1).png)")).toContain(
`src="screenshots/my%20shot%20(1).png"`,
);
});

it("leaves spaced paths alone where they would not become a link or image", () => {
for (const [markdown, options] of [
["<div>\n[x](/tmp/a b.png)\n</div>", {}],
['<span title="[x](/tmp/a b.png)">hi</span>', { escapeHtml: true }],
["`a` \\``` ![x](/tmp/a b.png) ``", {}],
["[label\n# heading\n](/tmp/a b.ts)", {}],
["[label\n---\n](/tmp/a b.ts)", {}],
["[unclosed\\\n\nfoo](/tmp/a b.png)", {}],
] as const) {
expect(wordsHtml(markdown, options)).not.toContain("&#x3C;/tmp/");
}
expect(wordsHtml("![x](/tmp/a b.png)")).toContain(`src="/tmp/a%20b.png"`);
// A failed candidate does not stop a later one from repairing.
expect(wordsHtml("[label\n# heading\n](/tmp/a b.ts) ![ok](/tmp/c d.png)")).toContain(
`src="/tmp/c%20d.png"`,
);

// Existing links keep their labels and autolinks, and an image inside one still repairs.
expect(wordsHtml("[![Shot](/tmp/my shot.png)](https://example.test)")).toContain(
`<a href="https://example.test"><img src="/tmp/my%20shot.png" alt="Shot" data-standalone></a>`,
);
expect(wordsHtml("[a <https://example.com> c](/tmp/My Folder/a.ts)")).not.toContain(
"My%20Folder",
);

expect(wordsHtml(String.raw`![x](.\My Folder\(1)\a.png)`)).not.toContain(String.raw`)\a.png`);
});

it("supports chunked footnote modes (omit, omit-seeded, only)", () => {
const chunk0 = toHtml(
wordsHast("[^a]: .\n[^b]: .\n\nFirst[^a].", {
Expand Down
82 changes: 80 additions & 2 deletions apps/web/src/markdown/wordsHast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { resolveChangeRequestMediaUrl } from "@t3tools/client-runtime/pull-reque
import { expandIndentedCodeTabs } from "@t3tools/shared/markdownListIndentation";
import { GITHUB_ALERT_MARKER } from "@t3tools/shared/markdownGithubAlerts";
import { absoluteFilePathMatches } from "@t3tools/shared/markdownLinks";
import {
countSpacedFilePathInsertionsBefore,
hasSpacedFilePathCandidate,
wrapSpacedFilePathsInProtectedRanges,
wrapSpacedFilePathsThatRender,
} from "@t3tools/shared/markdownSpacedFilePaths";
import {
rehypeNormalizeLocalFileLinks,
rehypePreserveImageSourceMeta,
Expand Down Expand Up @@ -1193,6 +1199,74 @@ function makeInert(node: Root | Element) {
}
}

const SPACED_PATH_PROTECTED_TAGS = new Set(["pre", "img", "svg", "script", "style"]);

function hastLinkStarts(tree: Root): ReadonlySet<number> {
const starts = new Set<number>();
const visit = (node: Root | Element) => {
for (const child of node.children) {
if (child.type !== "element") continue;
const start = child.position?.start.offset;
if ((child.tagName === "a" || child.tagName === "img") && start !== undefined) {
starts.add(start);
}
visit(child);
}
};
visit(tree);
return starts;
}

function repairSpacedFilePaths(
source: string,
tree: Root,
options: RenderOptions,
): {
readonly source: string;
readonly tree: Root;
readonly toOriginal: (offset: number) => number;
} | null {
if (!hasSpacedFilePathCandidate(source)) return null;
const protectedRanges: Array<readonly [number, number]> = markdownHtmlComments(source).map(
({ start, end }) => [start, end] as const,
);
// Existing links keep their labels; an image inside one can still be repaired.
const linkRanges: Array<readonly [number, number]> = [];
const collectProtected = (node: Root | Element) => {
for (const child of node.children) {
if (child.type !== "element") continue;
if (child.tagName === "a" && child.position) {
linkRanges.push([child.position.start.offset!, child.position.end.offset!]);
} else if (SPACED_PATH_PROTECTED_TAGS.has(child.tagName) && child.position) {
protectedRanges.push([child.position.start.offset!, child.position.end.offset!]);
continue;
}
collectProtected(child);
}
};
collectProtected(tree);
const repaired = wrapSpacedFilePathsThatRender(
(skipOpeners) =>
wrapSpacedFilePathsInProtectedRanges(
source,
protectedRanges,
undefined,
linkRanges,
undefined,
skipOpeners,
),
(markdown) => parse(markdown, options),
hastLinkStarts,
);
if (!repaired) return null;
const { markdown, insertions } = repaired;
return {
source: markdown,
tree: repaired.tree,
toOriginal: (offset) => offset - countSpacedFilePathInsertionsBefore(insertions, offset),
};
}

function isFootnotesSection(node: Root["children"][number]): boolean {
return (
node.type === "element" &&
Expand Down Expand Up @@ -1270,8 +1344,11 @@ export function wordsHast(text: string, options: WordsHastOptions): Root {
: { escapeHtml: options.escapeHtml }),
};
const insights = options.insights ? insightSource(text, renderOptions) : null;
const source = insights?.source ?? text;
const tree = insights?.tree ?? parse(source, renderOptions);
const initialSource = insights?.source ?? text;
const initialTree = insights?.tree ?? parse(initialSource, renderOptions);
const spaced = repairSpacedFilePaths(initialSource, initialTree, renderOptions);
const source = spaced?.source ?? initialSource;
const tree = spaced?.tree ?? initialTree;
let documentDefinitions: string[] | undefined;
const walk: Walk = {
definitions: (markdown) =>
Expand All @@ -1297,6 +1374,7 @@ export function wordsHast(text: string, options: WordsHastOptions): Root {
normalizeLocalFileLinks(tree as never);
const hast = sanitizeChatHast(tree);
if (walk.math.size > 0) restoreMath(hast, walk.math);
if (spaced) restorePositions(hast, spaced.toOriginal);
if (insights) restorePositions(hast, insights.toOriginal);
if (extensions.footnotes) applyFootnoteMode(hast, extensions.footnotes);
if (extensions.inert) makeInert(hast);
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@
"types": "./src/proposedPlanText.ts",
"import": "./src/proposedPlanText.ts"
},
"./markdownSpacedFilePaths": {
"types": "./src/markdownSpacedFilePaths.ts",
"import": "./src/markdownSpacedFilePaths.ts"
},
"./markdownWindowsPaths": {
"types": "./src/markdownWindowsPaths.ts",
"import": "./src/markdownWindowsPaths.ts"
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/src/markdownLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,28 @@ describe("extractMarkdownLinkHrefs", () => {
["[a](unclosed(paren) [b](ok)", ["ok"]],
["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]],
["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]],
[
String.raw`![Bust](Z:/Projects/Demo App/Output Shots/bust.jpg) [Details](C:\Users\First Last\src\Details.tsx:12) [Notes](/Users/me/Mobile Documents/notes.md) ![Shot](./img !32.png) ![Rel](screenshots/my shot (1).png)`,
[
"Z:/Projects/Demo App/Output Shots/bust.jpg",
String.raw`C:\Users\First Last\src\Details.tsx:12`,
"/Users/me/Mobile Documents/notes.md",
"./img !32.png",
"screenshots/my shot (1).png",
],
],
[
String.raw`[a](/home/u/My Folder/a.pdf "Title") [b](C:\My Drive\b.html 'it\'s') [c](/tmp/x y.md "Revision )") [d](/home/u/Bob's Files/d.md)`,
[
"/home/u/My Folder/a.pdf",
String.raw`C:\My Drive\b.html`,
"/tmp/x y.md",
"/home/u/Bob's Files/d.md",
],
],
[`[a](/tmp/x y.md "unclosed) [b](ok)`, ["ok"]],
[`[a](/tmp/x y.md 'unclosed) [b](ok)`, ["ok"]],
[`[a](/tmp/Rock 'n' Roll/a.md)`, ["/tmp/Rock 'n' Roll/a.md"]],
])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => {
expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
});
Expand Down
Loading
Loading