Add comments to code
To comment on inline code or text inside code blocks, extend the code extensions to allow the inlineThread mark from CommentsKit.
By default, Code excludes all other marks, and CodeBlock allows no marks. These rules remove comment anchors when you add comments to code or format commented text as code. See issue #6398 for the reported behavior and maintainer guidance.
Allow comments on inline code
Extend Code with an empty excludes value so the code mark can coexist with inlineThread:
import Code from '@tiptap/extension-code'
const CodeWithComments = Code.extend({
excludes: '',
})This also allows code to coexist with other marks, such as bold and italic. To keep those exclusions, set excludes to a space-separated list of the mark names you want to exclude. Leave inlineThread out of that list.
Allow comments inside code blocks
Extend CodeBlock to allow inlineThread on its text:
import CodeBlock from '@tiptap/extension-code-block'
const CodeBlockWithComments = CodeBlock.extend({
marks: 'inlineThread',
})This allows comment anchors while keeping other marks out of code blocks. If you use CodeBlockLowlight, apply the same marks: 'inlineThread' override to that extension instead.
Register the extended extensions
If you use StarterKit, disable its built-in Code and CodeBlock extensions before registering your replacements:
import { Editor } from '@tiptap/core'
import StarterKit from '@tiptap/starter-kit'
import { CommentsKit } from '@tiptap-pro/extension-comments'
const editor = new Editor({
extensions: [
StarterKit.configure({
code: false,
codeBlock: false,
}),
CodeWithComments,
CodeBlockWithComments,
CommentsKit.configure({
provider: collabProvider,
}),
],
})Use your existing TiptapCollabProvider instance as collabProvider. Keep the collaboration setup from the installation guide.
Apply the same schema overrides on every collaborating client. Then use the usual setThread command to comment on selected code. The overrides preserve comment anchors when you format commented text as code, but they do not restore threads already deleted under the default schema.