Skip to content
TypeScript

fix(web): format inline code when the opening backtick is typed last (#23940)

Saikrishna Ambeticedf8b4c1cd5
diff --git a/apps/web/src/composer-rich-text-doc.test.ts b/apps/web/src/composer-rich-text-doc.test.ts
index 3ad569e4b8a..ae120c8a925 100644
--- a/apps/web/src/composer-rich-text-doc.test.ts
+++ b/apps/web/src/composer-rich-text-doc.test.ts
@@ -1028,6 +1028,56 @@ describe("inline code entry", () => {
}
}
+ it.each(["", "this is a "])(
+ "formats code after %j when the opening backtick is typed last",
+ (prefix) => {
+ const editor = editorFor();
+ type(editor, `${prefix}test\` after`);
+ editor.commands.setTextSelection(prefix.length + 1);
+ type(editor, "`");
+
+ expect(editor.state.doc.textContent).toBe(`${prefix}test after`);
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`test\` after`);
+ expect(editor.state.selection.from).toBe(prefix.length + 1);
+ type(editor, "new ");
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`new test\` after`);
+ editor.commands.setTextSelection(editor.state.doc.content.size - 1);
+ type(editor, " plain");
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`new test\` after plain`);
+ const formatted = editor.state.doc.toJSON();
+ let undos = 0;
+ while (editor.commands.undo()) undos += 1;
+ expect(undos).toBeGreaterThan(0);
+ expect(editor.state.doc.textContent).toBe("");
+ for (let i = 0; i < undos; i += 1) expect(editor.commands.redo()).toBe(true);
+ expect(editor.state.doc.toJSON()).toEqual(formatted);
+ },
+ );
+
+ it.each([
+ { name: "doubled closing ticks", text: "test``" },
+ { name: "a code block", text: "test`", block: "codeBlock" },
+ { name: "bold text", text: "test`", mark: "bold" },
+ { name: "existing inline code", text: "test`", mark: "code" },
+ ])("keeps an opening tick literal before $name", ({ text, block, mark }) => {
+ const editor = editorFor(block, mark);
+ type(editor, text);
+ editor.commands.setTextSelection(1);
+ type(editor, "`");
+ expect(editor.state.doc.textContent).toBe(`\`${text}`);
+ });
+
+ it.each(["hardBreak", "composer-mention"])("does not pair ticks across a %s", (nodeType) => {
+ const editor = editorFor();
+ type(editor, "before");
+ editor.commands.insertContent({ type: nodeType });
+ type(editor, "after`");
+ editor.commands.setTextSelection(1);
+ type(editor, "`");
+ expect(editor.state.doc.firstChild?.firstChild?.marks).toEqual([]);
+ expect(editor.state.doc.textContent).toBe("`beforeafter`");
+ });
+
it.each(["", "prefix ", "@README.md"])(
"formats a filled pair after %j, leaves following text plain, and keeps the normal input rule",
(prefix) => {
diff --git a/apps/web/src/composer-rich-text-doc.ts b/apps/web/src/composer-rich-text-doc.ts
index c0e0a653016..2d629c3eb74 100644
--- a/apps/web/src/composer-rich-text-doc.ts
+++ b/apps/web/src/composer-rich-text-doc.ts
@@ -81,14 +81,37 @@ function codePairCaretPlugin(type: MarkType) {
handleTextInput(view, from, to, text, insert) {
if (view.composing || from !== to || text !== "`") return false;
const $from = view.state.doc.resolve(from);
- if (
- !$from.parent.isTextblock ||
- $from.parent.type.spec.code ||
- !/(^|[^`])`$/.test($from.nodeBefore?.text ?? "")
- ) {
- return false;
+ if (!$from.parent.isTextblock || $from.parent.type.spec.code) return false;
+ const before = $from.nodeBefore?.text ?? "";
+ if (/(^|[^`])`$/.test(before)) {
+ view.dispatch(insert().setMeta(codePairKey, { from: from - 1, to: from + 1 }));
+ return true;
}
- view.dispatch(insert().setMeta(codePairKey, { from: from - 1, to: from + 1 }));
+ if (before.endsWith("`")) return false;
+
+ // The opening tick can be typed after the closing one. Input rules
+ // cannot recognize that pair because they only read before the caret.
+ const after = $from.parent.textBetween(
+ $from.parentOffset,
+ $from.parent.content.size,
+ "\n",
+ "\n",
+ );
+ const match = /^([^`\n]+)`(?!`)/.exec(after);
+ if (!match) return false;
+ const end = from + match[1]!.length;
+ let plain = true;
+ view.state.doc.nodesBetween(from, end + 1, (node) => {
+ if (node.isInline && (!node.isText || node.marks.length > 0)) plain = false;
+ });
+ if (!plain) return false;
+ const tr = insert()
+ .delete(end + 1, end + 2)
+ .delete(from, from + 1);
+ tr.addMark(from, end, type.create());
+ tr.setSelection(TextSelection.create(tr.doc, from));
+ tr.addStoredMark(type.create());
+ view.dispatch(tr);
return true;
},
},