Skip to content

Commit c48abc8

Browse files
feat: Code mark input rule edge case (BLO-938) (#2698)
* Added edge case handling for code mark input rule * Explicitly added original input rule instead of inheriting
1 parent 8508f81 commit c48abc8

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

packages/core/src/blocks/defaultBlocks.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { InputRule, markInputRule } 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,39 @@ 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+
addInputRules() {
143+
return [
144+
// Matches any string that starts with a backtick, ends with a
145+
// backtick, and has any non-backtick characters in between. Copied
146+
// from original input rule:
147+
// https://github.com/ueberdosis/tiptap/blob/c27661c148cdbea9e1c80107e10d0a9d1775c4ec/packages/extension-code/src/code.ts#L116
148+
markInputRule({
149+
find: /(^|[^`])`([^`]+)`(?!`)$/,
150+
type: this.type,
151+
}),
152+
// Extends the Code mark with an extra input rule that fires when a space is
153+
// typed after the closing backtick. The default rule only fires when typing
154+
// the closing backtick itself, so it misses the case where the user adds
155+
// both backticks first, then writes content between them.
156+
new InputRule({
157+
find: /(^|[^`])`([^`]+)`(?!`) $/,
158+
handler: ({ state, range, match }) => {
159+
const { tr, schema } = state;
160+
const leadingChar = match[1];
161+
const content = match[2];
162+
tr.replaceWith(range.from + leadingChar.length, range.to, [
163+
schema.text(content, [this.type.create()]),
164+
schema.text(" "),
165+
]);
166+
},
167+
}),
168+
];
169+
},
170+
}),
171+
"boolean",
172+
),
140173
textColor: TextColor,
141174
backgroundColor: BackgroundColor,
142175
} satisfies StyleSpecs;

0 commit comments

Comments
 (0)