Skip to content

Commit 3a3c307

Browse files
dibas-npclaude
andcommitted
fix(mobile): split at every line break between images
A paragraph such as `![a](a) ![b](b)` followed by `![c](c)` on the next line was skipped whole, because one gap between its images had no line break, so `c` stayed inline. Split each gap that has a line break and leave same-line images together. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 86e536f commit 3a3c307

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,13 @@ function markdownWithImageParagraphs(markdown: string): string {
447447
)
448448
continue;
449449
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) }));
450+
for (const [index, image] of images.slice(1).entries()) {
451+
const start = images[index]!.position?.end.offset;
452+
const end = image.position?.start.offset;
453+
if (start === undefined || end === undefined) continue;
454+
const ending = /\r\n?|\n/.exec(markdown.slice(start, end));
455+
if (ending) edits.push({ start, end, content: ending[0].repeat(2) });
456+
}
457457
}
458458
return applyMarkdownEdits(markdown, edits);
459459
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,19 @@ describe("file embeds in native thread and file Markdown", () => {
248248
it("gives each image on its own line its own native paragraph", async () => {
249249
const images =
250250
"![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`;
251+
const source = `Shots:\n\n${images}\n\n![a](./a.svg) ![b](./b.svg)\n![c](./c.svg)\n\nNext\n\n[dark]: ./after-dark.png`;
252252
const paragraphs = (
253253
await parseMarkdown(markdownForNativeRendering(source, ENRICHED_INLINE_FILE_LINK_REGEX))
254254
).children?.map((block) => block.children?.map((child) => child.type).join(" "));
255-
expect(paragraphs).toEqual(["Text", "Image", "Image", "Image", "Image Text Image", "Text"]);
255+
expect(paragraphs).toEqual([
256+
"Text",
257+
"Image",
258+
"Image",
259+
"Image",
260+
"Image Text Image",
261+
"Image",
262+
"Text",
263+
]);
256264
expect(markdownForNativeParsing("![a](./a.png)\r\n![b](./b.png)")).toBe(
257265
"![a](./a.png)\r\n\r\n![b](./b.png)",
258266
);

0 commit comments

Comments
 (0)