Skip to content

Commit 0605ac9

Browse files
Star Lordmatt2e
authored andcommitted
fix(composer): escape literal asterisks at emphasis boundaries
Keep generated emphasis delimiters separate from adjacent authored asterisks without changing marker selection or encoding ordinary letters. Preserve existing raw escape pairs and protect a trailing backslash from swallowing the next delimiter. Add exact-wire, parsed-output, typed-input and mention-binding regressions. Signed-off-by: Star Lord <b89298dbe87c6b3fd8a425b535d9d161f23c88d555a47925ae0b04dfe02b201e@buzz.block.builderlab.xyz> Signed-off-by: Matt Toohey <[email protected]>
1 parent 0d083be commit 0605ac9

3 files changed

Lines changed: 180 additions & 2 deletions

File tree

‎src/features/messages/EditableInput.test.tsx‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,43 @@ it("follows the parser for intraword delimiters: snake_case stays literal, 5*3*2
295295
});
296296
});
297297

298+
it.each(["a*b**", "a*b**c"])(
299+
"preserves displayed emphasis when sending %s",
300+
async (typed) => {
301+
const h = mount();
302+
await h.user.keyboard(typed);
303+
expect(h.input.querySelector("em")).toHaveTextContent("b");
304+
expect(h.markdown()).toBe(`a*b*\\${typed.slice(4)}`);
305+
expect(fromMarkdown(h.markdown()).children[0]).toMatchObject({
306+
type: "paragraph",
307+
children: [
308+
{ type: "text", value: "a" },
309+
{ type: "emphasis", children: [{ type: "text", value: "b" }] },
310+
{ type: "text", value: typed.slice(4) },
311+
],
312+
});
313+
},
314+
);
315+
316+
it("preserves toolbar italics after a typed literal asterisk", async () => {
317+
const h = mount();
318+
await h.user.keyboard("5*");
319+
act(() => h.input.toggleFormat("italic"));
320+
await h.user.keyboard("x");
321+
act(() => h.input.toggleFormat("italic"));
322+
await h.user.keyboard("y");
323+
expect(h.input.querySelector("em")).toHaveTextContent("x");
324+
expect(h.markdown()).toBe("5\\**x*y");
325+
expect(fromMarkdown(h.markdown()).children[0]).toMatchObject({
326+
type: "paragraph",
327+
children: [
328+
{ type: "text", value: "5*" },
329+
{ type: "emphasis", children: [{ type: "text", value: "x" }] },
330+
{ type: "text", value: "y" },
331+
],
332+
});
333+
});
334+
298335
it("converts a span typed on a heading line, which the timeline renders as a heading", async () => {
299336
const h = mount();
300337
await h.user.keyboard("# Title **bold**");
@@ -392,6 +429,37 @@ it("italicises a mention typed between asterisks and sends a form the timeline s
392429
]);
393430
});
394431

432+
it("keeps an italic mention bound after a literal asterisk", async () => {
433+
const h = mount();
434+
const honey = { pubkey: "a".repeat(64), name: "Honey" };
435+
await h.user.keyboard("a*");
436+
act(() => {
437+
h.input.insertText("", honey);
438+
h.input.setSelectionRange(2, 8);
439+
h.input.toggleFormat("italic");
440+
});
441+
expect(h.input.querySelector('em [data-source="@Honey"]')).not.toBeNull();
442+
expect(h.draft().recipients).toEqual([{ ...honey, start: 2, end: 8 }]);
443+
expect(h.markdown()).toBe("a\\**@Honey* ");
444+
expect(fromMarkdown(h.markdown()).children[0]).toMatchObject({
445+
type: "paragraph",
446+
children: [
447+
{ type: "text", value: "a*" },
448+
{ type: "emphasis", children: [{ type: "text", value: "@Honey" }] },
449+
],
450+
});
451+
expect(
452+
profileMentionParts(
453+
{ content: h.markdown(), mentions: [honey.pubkey] },
454+
new Map([[honey.pubkey, { name: honey.name }]]),
455+
),
456+
).toEqual([
457+
{ text: "a\\**" },
458+
{ text: "@Honey", target: profileTarget(honey.pubkey) },
459+
{ text: "* " },
460+
]);
461+
});
462+
395463
it.each([
396464
["bullet_list", "ul", "-"],
397465
["ordered_list", "ol", "1."],

‎src/features/messages/composer-document.test.ts‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
readComposerSnapshot,
77
} from "./composer-document";
88
import { composerMarkdown } from "./composer-markdown";
9+
import { profileMentionParts } from "./profile-mentions";
10+
import { profileTarget } from "../profiles/target";
911
import { mentionDraft } from "./mention-draft";
1012
import { fromMarkdown } from "mdast-util-from-markdown";
1113
import { gfmStrikethroughFromMarkdown } from "mdast-util-gfm-strikethrough";
@@ -338,6 +340,104 @@ describe("composer Markdown boundary", () => {
338340
],
339341
});
340342
});
343+
it.each([
344+
["a", "b", "*", "a*b*\\*"],
345+
["a", "b", "*c", "a*b*\\*c"],
346+
["5*", "x", "y", "5\\**x*y"],
347+
["a**", "b", "c", "a\\*\\**b*c"],
348+
["a", "b", "**c", "a*b*\\*\\*c"],
349+
])(
350+
"preserves emphasis beside literal asterisks in %s / %s / %s",
351+
(before, inner, after, wire) => {
352+
const output = serialize(
353+
schema.text(before),
354+
schema.text(inner, [schema.marks.italic.create()]),
355+
schema.text(after),
356+
);
357+
expect(output).toBe(wire);
358+
expect(fromMarkdown(output).children[0]).toMatchObject({
359+
type: "paragraph",
360+
children: [
361+
{ type: "text", value: before },
362+
{ type: "emphasis", children: [{ type: "text", value: inner }] },
363+
{ type: "text", value: after },
364+
],
365+
});
366+
},
367+
);
368+
it.each([
369+
["a\\*", "a\\**b*c", "a*"],
370+
["a\\\\*", "a\\\\\\**b*c", "a\\*"],
371+
["a\\**", "a\\*\\**b*c", "a**"],
372+
["a*\\", "a\\*\\\\*b*c", "a*\\"],
373+
["a\\", "a\\\\*b*c", "a\\"],
374+
["a\\\\", "a\\\\*b*c", "a\\"],
375+
])(
376+
"preserves raw escapes before generated emphasis in %s",
377+
(before, wire, rendered) => {
378+
const output = serialize(
379+
schema.text(before),
380+
schema.text("b", [schema.marks.italic.create()]),
381+
schema.text("c"),
382+
);
383+
expect(output).toBe(wire);
384+
expect(fromMarkdown(output).children[0]).toMatchObject({
385+
type: "paragraph",
386+
children: [
387+
{ type: "text", value: rendered },
388+
{ type: "emphasis", children: [{ type: "text", value: "b" }] },
389+
{ type: "text", value: "c" },
390+
],
391+
});
392+
},
393+
);
394+
it("keeps an unmarked recipient's exact name beside emphasis and a literal asterisk", () => {
395+
const honey = { pubkey: "a".repeat(64), name: "Honey" };
396+
const output = serialize(
397+
schema.nodes.token.create({ source: "@Honey", recipient: honey }),
398+
schema.text("b", [schema.marks.italic.create()]),
399+
schema.text("*"),
400+
);
401+
expect(output).toBe("@Honey*b*\\*");
402+
expect(fromMarkdown(output).children[0]).toMatchObject({
403+
type: "paragraph",
404+
children: [
405+
{ type: "text", value: "@Honey" },
406+
{ type: "emphasis", children: [{ type: "text", value: "b" }] },
407+
{ type: "text", value: "*" },
408+
],
409+
});
410+
expect(
411+
profileMentionParts(
412+
{ content: output, mentions: [honey.pubkey] },
413+
new Map([[honey.pubkey, { name: honey.name }]]),
414+
),
415+
).toEqual([
416+
{ text: "@Honey", target: profileTarget(honey.pubkey) },
417+
{ text: "*b*\\*" },
418+
]);
419+
});
420+
it("keeps generated strong delimiters distinct from literal asterisks", () => {
421+
const italic = (text: string) =>
422+
schema.text(text, [schema.marks.italic.create()]);
423+
for (const children of [
424+
[schema.text("a"), italic("b"), bold("c")],
425+
[bold("a"), italic("b"), schema.text("c")],
426+
]) {
427+
const output = serialize(...children);
428+
expect(output).not.toContain("&#");
429+
expect(renderedCharacters(fromMarkdown(output).children)).toEqual(
430+
children.flatMap((child) =>
431+
[...(child.text ?? "")].map((character) => ({
432+
character,
433+
marks: child.marks.map((mark) =>
434+
mark.type.name === "italic" ? "emphasis" : "strong",
435+
),
436+
})),
437+
),
438+
);
439+
}
440+
});
341441
it("uses syntax-aware escaping and delimiter flanking", () => {
342442
expect(serialize(bold("*"))).toBe("**\\***");
343443
const output = serialize(schema.text("x"), bold("!"), schema.text("y"));

‎src/features/messages/composer-markdown.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ function paragraphMarkdown(block: EditorNode): string {
337337
};
338338
const rawText: Handle = (node, parent, state, info) => {
339339
if (node.type === "text" && raw.has(node)) {
340-
// Escape only the boundary ticks/backslash that could absorb a generated
341-
// code delimiter; unrelated authored Markdown remains byte-for-byte source.
340+
// Escape only raw boundaries that could absorb generated delimiters;
341+
// unrelated authored Markdown remains byte-for-byte source.
342342
let value: string = node.value;
343343
if (info.before.endsWith("|"))
344344
value = value.replace(/^\|+/, (pipes) =>
@@ -354,6 +354,16 @@ function paragraphMarkdown(block: EditorNode): string {
354354
value = value.replace(/[`\\]+$/, (tail) =>
355355
tail.replace(/[\\`]/g, "\\$&"),
356356
);
357+
if (info.before.endsWith("*"))
358+
value = value.replace(/^\*+/, (stars) => stars.replaceAll("*", "\\*"));
359+
// Emphasis.peek advertises _ even when the handler later chooses *.
360+
// Keep authored escape pairs; escape bare stars and a final backslash.
361+
if (/^[*_]/.test(info.after))
362+
value = value.replace(/[\\*]+$/, (tail) =>
363+
tail.replace(/\\[\\*]|\*|\\$/g, (part) =>
364+
part.length === 1 ? `\\${part}` : part,
365+
),
366+
);
357367
return value;
358368
}
359369
const value = defaultHandlers.text(node, parent, state, info);

0 commit comments

Comments
 (0)