Skip to content

Commit

Permalink
feat: add renderLabel option to mention extension, see #1322
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Jun 14, 2021
1 parent c64761a commit 3b78af4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 5 additions & 4 deletions docs/src/docPages/api/nodes/mention.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ And yes, we plan to support React, too. Meanwhile, you can roll your own `ReactR
It’s also possible to use Vanilla JavaScript, but that is probably a lot more work.

## Settings
| Option | Type | Default | Description |
| -------------- | -------- | ------- | --------------------------------------------------------------------- |
| HTMLAttributes | `Object` | `{}` | Custom HTML attributes that should be added to the rendered HTML tag. |
| suggestion | `Object` | `{ … }` | [Read more](/api/utilities/suggestion) |
| Option | Type | Default | Description |
| -------------- | -------- | -------------------------- | --------------------------------------------------------------------- |
| HTMLAttributes | `Object` | `{}` | Custom HTML attributes that should be added to the rendered HTML tag. |
| renderLabel | `String` | `({ options, node }) => …` | Define how a mention label should be rendered. |
| suggestion | `Object` | `{ … }` | [Read more](/api/utilities/suggestion) |

## Source code
[packages/extension-mention/](https://github.com/ueberdosis/tiptap/blob/main/packages/extension-mention/)
Expand Down
18 changes: 16 additions & 2 deletions packages/extension-mention/src/mention.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Node, mergeAttributes } from '@tiptap/core'
import { Node as ProseMirrorNode } from 'prosemirror-model'
import Suggestion, { SuggestionOptions } from '@tiptap/suggestion'

export type MentionOptions = {
HTMLAttributes: Record<string, any>,
renderLabel: (props: {
options: MentionOptions,
node: ProseMirrorNode,
}) => string,
suggestion: Omit<SuggestionOptions, 'editor'>,
}

Expand All @@ -11,6 +16,9 @@ export const Mention = Node.create<MentionOptions>({

defaultOptions: {
HTMLAttributes: {},
renderLabel({ options, node }) {
return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
},
suggestion: {
char: '@',
command: ({ editor, range, props }) => {
Expand Down Expand Up @@ -95,12 +103,18 @@ export const Mention = Node.create<MentionOptions>({
return [
'span',
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
`${this.options.suggestion.char}${node.attrs.label ?? node.attrs.id}`,
this.options.renderLabel({
options: this.options,
node,
}),
]
},

renderText({ node }) {
return `${this.options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
return this.options.renderLabel({
options: this.options,
node,
})
},

addKeyboardShortcuts() {
Expand Down

0 comments on commit 3b78af4

Please sign in to comment.