Skip to content

Commit 86e536f

Browse files
dibas-npclaude
andcommitted
fix(mobile): show every image when Markdown puts one per line
Images on consecutive lines form one Markdown paragraph. The native renderer sizes an image as a block only when it is its paragraph's only child, so on Android the first image was cropped and the rest were not drawn. markdownForNativeParsing now gives each image its own paragraph when a top-level paragraph holds only images, one per line. Images that share a line stay inline, and parseMarkdownDocument keeps the authored paragraphs. Fixes #28063 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e5d0710 commit 86e536f

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,44 @@ function markdownWithWindowsPathDestinations(markdown: string): string {
431431
return applyMarkdownEdits(markdown, edits);
432432
}
433433

434+
const isImage = (node: MarkdownNode) => node.type === "image" || node.type === "imageReference";
435+
const isSpace = (node: MarkdownNode) =>
436+
node.type === "break" || (node.type === "text" && node.value.trim() === "");
437+
438+
// Native Markdown sizes an image as a block only when it is its paragraph's only child.
439+
function markdownWithImageParagraphs(markdown: string): string {
440+
// Skips the extra parse unless an image starts a line.
441+
if (!/\n[ \t]*!\[/.test(markdown)) return markdown;
442+
const edits: MarkdownEdit[] = [];
443+
for (const block of parser.parse(markdown).children) {
444+
if (
445+
block.type !== "paragraph" ||
446+
!block.children.every((child) => isImage(child) || isSpace(child))
447+
)
448+
continue;
449+
const images = block.children.filter(isImage);
450+
const gaps = images.slice(1).map((image, index) => ({
451+
start: images[index]!.position?.end.offset ?? -1,
452+
end: image.position?.start.offset ?? -1,
453+
}));
454+
const lineEnds = gaps.map(({ start, end }) => /\r\n?|\n/.exec(markdown.slice(start, end)));
455+
if (gaps.length === 0 || lineEnds.some((ending) => ending === null)) continue;
456+
gaps.forEach((gap, index) => edits.push({ ...gap, content: lineEnds[index]![0].repeat(2) }));
457+
}
458+
return applyMarkdownEdits(markdown, edits);
459+
}
460+
434461
// HTML wrappers can expose Markdown that wasn't in the first AST, so normalize them first.
435-
export function markdownForNativeParsing(markdown: string): string {
462+
function normalizedMarkdown(markdown: string): string {
436463
return markdownWithWindowsPathDestinations(markdownWithHtml(markdown));
437464
}
438465

466+
export function markdownForNativeParsing(markdown: string): string {
467+
return markdownWithImageParagraphs(normalizedMarkdown(markdown));
468+
}
469+
439470
export function parseMarkdownDocument(markdown: string): Root {
440-
return parser.parse(markdownForNativeParsing(markdown));
471+
return parser.parse(normalizedMarkdown(markdown));
441472
}
442473

443474
export function markdownForNativeRendering(markdown: string, fileLink: RegExp): string {

‎apps/mobile/src/lib/markdownHtml.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22
import {
3+
markdownForNativeParsing,
34
markdownForNativeRendering,
45
markdownHtmlContent,
56
markdownWithHtml,
@@ -244,6 +245,19 @@ describe("file embeds in native thread and file Markdown", () => {
244245
}
245246
});
246247

248+
it("gives each image on its own line its own native paragraph", async () => {
249+
const images =
250+
"![before](./before.png)\n![after light](./after-light.png) \\\n![after dark][dark]";
251+
const source = `Shots:\n\n${images}\n\n![a](./a.svg) ![b](./b.svg)\n\nNext\n\n[dark]: ./after-dark.png`;
252+
const paragraphs = (
253+
await parseMarkdown(markdownForNativeRendering(source, ENRICHED_INLINE_FILE_LINK_REGEX))
254+
).children?.map((block) => block.children?.map((child) => child.type).join(" "));
255+
expect(paragraphs).toEqual(["Text", "Image", "Image", "Image", "Image Text Image", "Text"]);
256+
expect(markdownForNativeParsing("![a](./a.png)\r\n![b](./b.png)")).toBe(
257+
"![a](./a.png)\r\n\r\n![b](./b.png)",
258+
);
259+
});
260+
247261
it("retains standalone audio players and image/video embeds in threads", () => {
248262
const source =
249263
"![sound](./sound.mp3)\n\n![image](./image.png)\n\n![clip](./clip.mp4)\n\ninline ![photo](./photo.png)";

0 commit comments

Comments
 (0)