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

Allow the mention extension to have custom renderHTML #4082

Merged
merged 4 commits into from
Nov 22, 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
20 changes: 17 additions & 3 deletions docs/api/nodes/mention.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,31 @@ Mention.configure({
})
```

### renderLabel
Define how a mention label should be rendered.
### renderText
Define how a mention text should be rendered.

```js
Mention.configure({
renderLabel({ options, node }) {
renderText({ options, node }) {
return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
}
})
```

### renderHTML
Define how a mention html element should be rendered, this is useful if you want to render an element other than `span` (e.g `a`)

```js
Mention.configure({
renderHTML({ options, node }) {
return [
"a",
{ href: '/profile/1' },
`${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`,
];
}
})
```
### suggestion
[Read more](/api/utilities/suggestion)

Expand Down
57 changes: 45 additions & 12 deletions packages/extension-mention/src/mention.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { mergeAttributes, Node } from '@tiptap/core'
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
import { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model'
import { PluginKey } from '@tiptap/pm/state'
import Suggestion, { SuggestionOptions } from '@tiptap/suggestion'

export type MentionOptions = {
HTMLAttributes: Record<string, any>
renderLabel: (props: { options: MentionOptions; node: ProseMirrorNode }) => string
/** @deprecated use renderText and renderHTML instead */
renderLabel?: (props: { options: MentionOptions; node: ProseMirrorNode }) => string
janthurau marked this conversation as resolved.
Show resolved Hide resolved
renderText: (props: { options: MentionOptions; node: ProseMirrorNode }) => string
bdbch marked this conversation as resolved.
Show resolved Hide resolved
renderHTML: (props: { options: MentionOptions; node: ProseMirrorNode }) => DOMOutputSpec
suggestion: Omit<SuggestionOptions, 'editor'>
}

Expand All @@ -17,9 +20,16 @@ export const Mention = Node.create<MentionOptions>({
addOptions() {
return {
HTMLAttributes: {},
renderLabel({ options, node }) {
renderText({ options, node }) {
janthurau marked this conversation as resolved.
Show resolved Hide resolved
return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
},
renderHTML({ options, node }) {
return [
'span',
this.HTMLAttributes,
`${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`,
]
},
suggestion: {
char: '@',
pluginKey: MentionPluginKey,
Expand Down Expand Up @@ -110,18 +120,41 @@ export const Mention = Node.create<MentionOptions>({
},

renderHTML({ node, HTMLAttributes }) {
return [
'span',
mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),
this.options.renderLabel({
options: this.options,
node,
}),
]
if (this.options.renderLabel !== undefined) {
console.warn('renderLabel is deprecated use renderText and renderHTML instead')
return [
'span',
mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),
this.options.renderLabel({
options: this.options,
node,
}),
]
}
const html = this.options.renderHTML({
options: this.options,
node,
})

if (typeof html === 'string') {
return [
'span',
mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),
html,
]
}
return html
},

renderText({ node }) {
return this.options.renderLabel({
if (this.options.renderLabel !== undefined) {
console.warn('renderLabel is deprecated use renderText and renderHTML instead')
return this.options.renderLabel({
options: this.options,
node,
})
}
return this.options.renderText({
options: this.options,
node,
})
Expand Down