Skip to content

Commit cedf8b4

Browse files
fix(web): format inline code when the opening backtick is typed last (#23940)
1 parent b4ae3c8 commit cedf8b4

2 files changed

Lines changed: 80 additions & 7 deletions

File tree

‎apps/web/src/composer-rich-text-doc.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,56 @@ describe("inline code entry", () => {
10281028
}
10291029
}
10301030

1031+
it.each(["", "this is a "])(
1032+
"formats code after %j when the opening backtick is typed last",
1033+
(prefix) => {
1034+
const editor = editorFor();
1035+
type(editor, `${prefix}test\` after`);
1036+
editor.commands.setTextSelection(prefix.length + 1);
1037+
type(editor, "`");
1038+
1039+
expect(editor.state.doc.textContent).toBe(`${prefix}test after`);
1040+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`test\` after`);
1041+
expect(editor.state.selection.from).toBe(prefix.length + 1);
1042+
type(editor, "new ");
1043+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`new test\` after`);
1044+
editor.commands.setTextSelection(editor.state.doc.content.size - 1);
1045+
type(editor, " plain");
1046+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`new test\` after plain`);
1047+
const formatted = editor.state.doc.toJSON();
1048+
let undos = 0;
1049+
while (editor.commands.undo()) undos += 1;
1050+
expect(undos).toBeGreaterThan(0);
1051+
expect(editor.state.doc.textContent).toBe("");
1052+
for (let i = 0; i < undos; i += 1) expect(editor.commands.redo()).toBe(true);
1053+
expect(editor.state.doc.toJSON()).toEqual(formatted);
1054+
},
1055+
);
1056+
1057+
it.each([
1058+
{ name: "doubled closing ticks", text: "test``" },
1059+
{ name: "a code block", text: "test`", block: "codeBlock" },
1060+
{ name: "bold text", text: "test`", mark: "bold" },
1061+
{ name: "existing inline code", text: "test`", mark: "code" },
1062+
])("keeps an opening tick literal before $name", ({ text, block, mark }) => {
1063+
const editor = editorFor(block, mark);
1064+
type(editor, text);
1065+
editor.commands.setTextSelection(1);
1066+
type(editor, "`");
1067+
expect(editor.state.doc.textContent).toBe(`\`${text}`);
1068+
});
1069+
1070+
it.each(["hardBreak", "composer-mention"])("does not pair ticks across a %s", (nodeType) => {
1071+
const editor = editorFor();
1072+
type(editor, "before");
1073+
editor.commands.insertContent({ type: nodeType });
1074+
type(editor, "after`");
1075+
editor.commands.setTextSelection(1);
1076+
type(editor, "`");
1077+
expect(editor.state.doc.firstChild?.firstChild?.marks).toEqual([]);
1078+
expect(editor.state.doc.textContent).toBe("`beforeafter`");
1079+
});
1080+
10311081
it.each(["", "prefix ", "@README.md"])(
10321082
"formats a filled pair after %j, leaves following text plain, and keeps the normal input rule",
10331083
(prefix) => {

‎apps/web/src/composer-rich-text-doc.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,37 @@ function codePairCaretPlugin(type: MarkType) {
8181
handleTextInput(view, from, to, text, insert) {
8282
if (view.composing || from !== to || text !== "`") return false;
8383
const $from = view.state.doc.resolve(from);
84-
if (
85-
!$from.parent.isTextblock ||
86-
$from.parent.type.spec.code ||
87-
!/(^|[^`])`$/.test($from.nodeBefore?.text ?? "")
88-
) {
89-
return false;
84+
if (!$from.parent.isTextblock || $from.parent.type.spec.code) return false;
85+
const before = $from.nodeBefore?.text ?? "";
86+
if (/(^|[^`])`$/.test(before)) {
87+
view.dispatch(insert().setMeta(codePairKey, { from: from - 1, to: from + 1 }));
88+
return true;
9089
}
91-
view.dispatch(insert().setMeta(codePairKey, { from: from - 1, to: from + 1 }));
90+
if (before.endsWith("`")) return false;
91+
92+
// The opening tick can be typed after the closing one. Input rules
93+
// cannot recognize that pair because they only read before the caret.
94+
const after = $from.parent.textBetween(
95+
$from.parentOffset,
96+
$from.parent.content.size,
97+
"\n",
98+
"\n",
99+
);
100+
const match = /^([^`\n]+)`(?!`)/.exec(after);
101+
if (!match) return false;
102+
const end = from + match[1]!.length;
103+
let plain = true;
104+
view.state.doc.nodesBetween(from, end + 1, (node) => {
105+
if (node.isInline && (!node.isText || node.marks.length > 0)) plain = false;
106+
});
107+
if (!plain) return false;
108+
const tr = insert()
109+
.delete(end + 1, end + 2)
110+
.delete(from, from + 1);
111+
tr.addMark(from, end, type.create());
112+
tr.setSelection(TextSelection.create(tr.doc, from));
113+
tr.addStoredMark(type.create());
114+
view.dispatch(tr);
92115
return true;
93116
},
94117
},

0 commit comments

Comments
 (0)