Skip to content

Commit 2323814

Browse files
committed
fix(markdown): preserve trailing inline-code whitespace
Keep parser-normalized trailing spaces inside inline code when finalizing Markdown IR, including linked code and a code span containing one space. Use the existing code-span boundary floor for both inline and block code; ordinary trailing prose and generated separators still trim as before. Verified six focused cases, 85 IR owner/sibling checks, and seven pure renderer cases. The focused cases overlap the owner suite. Production changes are net -1 line; tests add 90 lines. Channel-specific trimming and post-IR chunk whitespace policies remain separate follow-ups.
1 parent ace8a13 commit 2323814

3 files changed

Lines changed: 95 additions & 6 deletions

File tree

‎packages/markdown-core/src/ir.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,60 @@ describe("sliceMarkdownIR surrogate pair boundaries", () => {
218218
expect(sliced.text).toBe("");
219219
});
220220
});
221+
222+
describe("Markdown final code content", () => {
223+
it.each([
224+
{
225+
name: "terminal inline code",
226+
markdown: "Copy `name `",
227+
expected: {
228+
text: "Copy name ",
229+
styles: [{ start: 5, end: 10, style: "code" }],
230+
links: [],
231+
},
232+
},
233+
{
234+
name: "terminal inline code in a link label",
235+
markdown: "[`name `](https://example.com)",
236+
expected: {
237+
text: "name ",
238+
styles: [{ start: 0, end: 5, style: "code" }],
239+
links: [{ start: 0, end: 5, href: "https://example.com" }],
240+
},
241+
},
242+
{
243+
name: "standalone one-space inline code",
244+
markdown: "` `",
245+
expected: {
246+
text: " ",
247+
styles: [{ start: 0, end: 1, style: "code" }],
248+
links: [],
249+
},
250+
},
251+
{
252+
name: "inline code followed by prose",
253+
markdown: "Copy `name ` next",
254+
expected: {
255+
text: "Copy name next",
256+
styles: [{ start: 5, end: 10, style: "code" }],
257+
links: [],
258+
},
259+
},
260+
{
261+
name: "ordinary trailing prose whitespace",
262+
markdown: "Copy name ",
263+
expected: { text: "Copy name", styles: [], links: [] },
264+
},
265+
{
266+
name: "fenced code trailing whitespace",
267+
markdown: "```\nname \n```",
268+
expected: {
269+
text: "name \n",
270+
styles: [{ start: 0, end: 6, style: "code_block" }],
271+
links: [],
272+
},
273+
},
274+
])("preserves final code payload: $name", ({ markdown, expected }) => {
275+
expect(markdownToIR(markdown)).toEqual(expected);
276+
});
277+
});

‎packages/markdown-core/src/ir.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,18 +1670,17 @@ export function markdownToIRWithMeta(
16701670
renderTokens(tokens as MarkdownToken[], state);
16711671
closeRemainingStyles(state);
16721672

1673+
// Preserve trailing whitespace inside code; trim generated trailing separators.
16731674
const trimmedText = state.text.trimEnd();
16741675
const trimmedLength = trimmedText.length;
1675-
let codeBlockEnd = 0;
1676+
let codeEnd = 0;
16761677
for (const span of state.styles) {
1677-
if (span.style !== "code_block") {
1678+
if (span.style !== "code" && span.style !== "code_block") {
16781679
continue;
16791680
}
1680-
if (span.end > codeBlockEnd) {
1681-
codeBlockEnd = span.end;
1682-
}
1681+
codeEnd = Math.max(codeEnd, span.end);
16831682
}
1684-
const finalLength = Math.max(trimmedLength, codeBlockEnd);
1683+
const finalLength = Math.max(trimmedLength, codeEnd);
16851684
const finalText =
16861685
finalLength === state.text.length ? state.text : state.text.slice(0, finalLength);
16871686
const annotations = mergeAnnotationSpans(clampAnnotationSpans(state.annotations, finalLength));

‎packages/markdown-core/src/render.crossing.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,36 @@ describe("renderMarkdownWithMarkers crossing spans", () => {
4444
).toBe(html);
4545
});
4646
});
47+
48+
describe("renderMarkdownWithMarkers code content", () => {
49+
it.each([
50+
{
51+
name: "terminal inline code",
52+
markdown: "Copy `name `",
53+
html: "Copy <code>name </code>",
54+
},
55+
{
56+
name: "terminal inline code in a link label",
57+
markdown: "[`name `](https://example.com)",
58+
html: '<a href="https://example.com"><code>name </code></a>',
59+
},
60+
{
61+
name: "inline code followed by prose",
62+
markdown: "Copy `name ` next",
63+
html: "Copy <code>name </code> next",
64+
},
65+
])("renders authored code whitespace: $name", ({ markdown, html }) => {
66+
expect(
67+
renderMarkdownWithMarkers(markdownToIR(markdown), {
68+
styleMarkers: { code: { open: "<code>", close: "</code>" } },
69+
escapeText: (text) => text,
70+
buildLink: (link) => ({
71+
start: link.start,
72+
end: link.end,
73+
open: `<a href="${link.href}">`,
74+
close: "</a>",
75+
}),
76+
}),
77+
).toBe(html);
78+
});
79+
});

0 commit comments

Comments
 (0)