|
| 1 | +import { InputRule, markInputRule } from "@tiptap/core"; |
1 | 2 | import Bold from "@tiptap/extension-bold"; |
2 | 3 | import Code from "@tiptap/extension-code"; |
3 | 4 | import Italic from "@tiptap/extension-italic"; |
@@ -136,7 +137,39 @@ export const defaultStyleSpecs = { |
136 | 137 | italic: createStyleSpecFromTipTapMark(Italic, "boolean"), |
137 | 138 | underline: createStyleSpecFromTipTapMark(Underline, "boolean"), |
138 | 139 | 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 | + ), |
140 | 173 | textColor: TextColor, |
141 | 174 | backgroundColor: BackgroundColor, |
142 | 175 | } satisfies StyleSpecs; |
|
0 commit comments