Skip to content

Commit b941d4e

Browse files
eimexdevclaude
andcommitted
fix(mobile): text after a split image keeps its literal block markers
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b25a88d commit b941d4e

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ describe("images inside running text", () => {
2525
expect(markdownWithBlockImages("- [x] Done ![a](a.png)")).toBe("- [x] Done\n\n ![a](a.png)");
2626
});
2727

28+
it("keeps text after an image from opening a block", () => {
29+
for (const [suffix, escaped] of [
30+
["# x", "\\# x"],
31+
["- x", "\\- x"],
32+
["* x", "\\* x"],
33+
["2. x", "2\\. x"],
34+
["> x", "\\> x"],
35+
["---", "\\---"],
36+
["***", "\\***"],
37+
["*emphasis* x", "*emphasis* x"],
38+
]) {
39+
expect(markdownWithBlockImages(`See ![a](a.png) ${suffix}`)).toBe(
40+
`See\n\n![a](a.png)\n\n${escaped}`,
41+
);
42+
}
43+
});
44+
2845
it("leaves images that already start their own line", () => {
2946
for (const markdown of [
3047
"Text\n\n![a](a.png)",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ export function markdownWithTableBreaks(markdown: string): string {
348348
return markdown;
349349
}
350350

351+
// Matches up to where a backslash escapes a heading, quote, list or thematic break marker.
352+
const BLOCK_MARKER = /^(?:\d{1,9}(?=[.)])|(?=[#>+-]|\*[ \t]|([*_])(?:[ \t]*\1){2,}[ \t]*$))/m;
353+
351354
/** md4c draws an image that follows text on its line as a 20pt glyph; give each its own paragraph. */
352355
export function markdownWithBlockImages(markdown: string): string {
353356
if (!markdown.includes("![")) return markdown;
@@ -380,6 +383,12 @@ export function markdownWithBlockImages(markdown: string): string {
380383
const after = /^[ \t]*(?:\\?\r?\n[ \t>]*)?/.exec(markdown.slice(imageEnd, end))![0];
381384
previousEnd = imageEnd + after.length;
382385
edits.push({ start: imageEnd, end: previousEnd, content: paragraphBreak });
386+
// The text after the image now starts a paragraph, where a leading marker would open a block.
387+
const marker = BLOCK_MARKER.exec(markdown.slice(previousEnd, end));
388+
if (marker) {
389+
const offset = previousEnd + marker[0].length;
390+
edits.push({ start: offset, end: offset, content: "\\" });
391+
}
383392
}
384393
}
385394
};

0 commit comments

Comments
 (0)