Skip to content

Commit 13fc219

Browse files
fix(web): format inline code when the opening backtick is typed last
1 parent 6ed47b5 commit 13fc219

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

0 commit comments

Comments
 (0)