Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow all marks to coexist with the Code mark #309

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/constants/extension-priorities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ const VIEW_EVENT_HANDLERS_PRIORITY = 105
*/
const BLOCKQUOTE_EXTENSION_PRIORITY = 101

/**
* Priority for the `RichTextCode` extension. This needs to be lower than the default for most
* built-in and official extensions (i.e. `100`), so that other marks wrap the `Code` mark, and not
* the other way around (i.e. prevents `<code><em>code</em></code>` from happening).
*/
const CODE_EXTENSION_PRIORITY = 99

export {
BLOCKQUOTE_EXTENSION_PRIORITY,
CODE_EXTENSION_PRIORITY,
PASTE_EXTENSION_PRIORITY,
SMART_MARKDOWN_TYPING_PRIORITY,
SUGGESTION_EXTENSION_PRIORITY,
Expand Down
18 changes: 18 additions & 0 deletions src/extensions/rich-text/rich-text-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Code } from '@tiptap/extension-code'

import { CODE_EXTENSION_PRIORITY } from '../../constants/extension-priorities'

/**
* 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
* other mark by default).
*
* @see https://tiptap.dev/api/schema#excludes
* @see https://prosemirror.net/docs/ref/#model.MarkSpec.excludes
*/
const RichTextCode = Code.extend({
priority: CODE_EXTENSION_PRIORITY,
excludes: Code.name,
})

export { RichTextCode }
4 changes: 2 additions & 2 deletions src/extensions/rich-text/rich-text-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Extension } from '@tiptap/core'
import { Blockquote } from '@tiptap/extension-blockquote'
import { Bold } from '@tiptap/extension-bold'
import { BulletList } from '@tiptap/extension-bullet-list'
import { Code } from '@tiptap/extension-code'
import { CodeBlock } from '@tiptap/extension-code-block'
import { Dropcursor } from '@tiptap/extension-dropcursor'
import { Gapcursor } from '@tiptap/extension-gapcursor'
Expand All @@ -27,6 +26,7 @@ import { BoldAndItalics } from './bold-and-italics'
import { CurvenoteCodemark } from './curvenote-codemark'
import { PasteEmojis } from './paste-emojis'
import { PasteMarkdown } from './paste-markdown'
import { RichTextCode } from './rich-text-code'
import { RichTextDocument } from './rich-text-document'
import { RichTextImage } from './rich-text-image'
import { RichTextLink } from './rich-text-link'
Expand Down Expand Up @@ -209,7 +209,7 @@ const RichTextKit = Extension.create<RichTextKitOptions>({

if (this.options.code !== false) {
extensions.push(
Code.configure(this.options?.code),
RichTextCode.configure(this.options?.code),

// Enhances the Code extension capabilities with additional features
CurvenoteCodemark,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Helper: Schema', () => {
describe('#computeSchemaId', () => {
test('returns a string ID that matches the given editor schema', () => {
expect(computeSchemaId(getSchema([RichTextKit]))).toBe(
'link,bold,code,italic,boldAndItalics,strike,paragraph,blockquote,bulletList,codeBlock,doc,hardBreak,heading,horizontalRule,image,listItem,orderedList,text',
'link,bold,italic,boldAndItalics,strike,code,paragraph,blockquote,bulletList,codeBlock,doc,hardBreak,heading,horizontalRule,image,listItem,orderedList,text',
)
})
})
Expand Down