Skip to content

Commit

Permalink
fix(rich-text-code): Retore original Tiptap input/paste rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rfgamaral committed Jan 7, 2025
1 parent 29adb09 commit 80d1a57
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/extensions/rich-text/rich-text-code.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { markInputRule, markPasteRule } from '@tiptap/core'
import { Code } from '@tiptap/extension-code'

import { CODE_EXTENSION_PRIORITY } from '../../constants/extension-priorities'
Expand All @@ -9,6 +10,18 @@ import type { CodeOptions } from '@tiptap/extension-code'
*/
type RichTextCodeOptions = CodeOptions

/**
* The original input regex for Markdown inline code (i.e. `<code>code</code>`) to prevent the issue
* introduced in this PR: https://github.com/ueberdosis/tiptap/pull/4468#issuecomment-2575093998
*/
const inputRegex = /(?:^|\s)(`(?!\s+`)((?:[^`]+))`(?!\s+`))$/

/**
* The original paste regex for Markdown inline code (i.e. `<code>code</code>`) to prevent the issue
* introduced in this PR: https://github.com/ueberdosis/tiptap/pull/4468#issuecomment-2575093998
*/
const pasteRegex = /(?:^|\s)(`(?!\s+`)((?:[^`]+))`(?!\s+`))/g

/**
* Custom extension that extends the built-in `Code` extension to allow all marks (e.g., Bold,
* Italic, and Strikethrough) to coexist with the `Code` mark (as opposed to disallowing all any
Expand All @@ -20,6 +33,22 @@ type RichTextCodeOptions = CodeOptions
const RichTextCode = Code.extend<RichTextCodeOptions>({
priority: CODE_EXTENSION_PRIORITY,
excludes: Code.name,
addInputRules() {
return [
markInputRule({
find: inputRegex,
type: this.type,
}),
]
},
addPasteRules() {
return [
markPasteRule({
find: pasteRegex,
type: this.type,
}),
]
},
})

export { RichTextCode }
Expand Down

0 comments on commit 80d1a57

Please sign in to comment.