Skip to content

Commit cdba17e

Browse files
committed
Added edge case handling for code mark input rule
1 parent d48a92a commit cdba17e

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

packages/core/src/blocks/defaultBlocks.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { InputRule } from "@tiptap/core";
12
import Bold from "@tiptap/extension-bold";
23
import Code from "@tiptap/extension-code";
34
import Italic from "@tiptap/extension-italic";
@@ -136,7 +137,32 @@ export const defaultStyleSpecs = {
136137
italic: createStyleSpecFromTipTapMark(Italic, "boolean"),
137138
underline: createStyleSpecFromTipTapMark(Underline, "boolean"),
138139
strike: createStyleSpecFromTipTapMark(Strike, "boolean"),
139-
code: createStyleSpecFromTipTapMark(Code, "boolean"),
140+
code: createStyleSpecFromTipTapMark(
141+
Code.extend({
142+
// Extends the Code mark with an extra input rule that fires when a space is
143+
// typed after the closing backtick. The default rule only fires when typing
144+
// the closing backtick itself, so it misses the case where the user opens
145+
// both backticks first, then writes content between them.
146+
addInputRules() {
147+
return [
148+
...(this.parent?.() ?? []),
149+
new InputRule({
150+
find: /(^|[^`])`([^`]+)`(?!`) $/,
151+
handler: ({ state, range, match }) => {
152+
const { tr, schema } = state;
153+
const leadingChar = match[1];
154+
const content = match[2];
155+
tr.replaceWith(range.from + leadingChar.length, range.to, [
156+
schema.text(content, [this.type.create()]),
157+
schema.text(" "),
158+
]);
159+
},
160+
}),
161+
];
162+
},
163+
}),
164+
"boolean",
165+
),
140166
textColor: TextColor,
141167
backgroundColor: BackgroundColor,
142168
} satisfies StyleSpecs;

0 commit comments

Comments
 (0)