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

Conversation

moham96
Copy link
Contributor

@moham96 moham96 commented May 26, 2023

Please describe your changes

Currently the mention extension's renderLabel doesn't allow returning HTML and expect returning string, there is a workaround by tricking the typesystem but this breaks when generating text from JSONContent (i.e using generateText from @tiptap/core , this should allow the user to customize the text and html rendering.

This can be very useful when one wants to render and a tag instead of just a span tag so that it can link to the profile of the mention person

How did you accomplish your changes

Basically introduced two new functions renderText and renderHTML

How have you tested your changes

Yes, tested it on my current project (Laravel with Inertiajs)

How can we verify your changes

It can be tested using the mention demo

Remarks

N/A

Checklist

  • The changes are not breaking the editor
  • Added tests where possible
  • Followed the guidelines
  • Fixed linting issues

Related issues

#3325

@netlify
Copy link

netlify bot commented May 26, 2023

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit 6ff8323
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/655e5add6a684d00081263fd
😎 Deploy Preview https://deploy-preview-4082--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

packages/extension-mention/src/mention.ts Outdated Show resolved Hide resolved
@andrictham
Copy link

Seems like the original PR author isn’t responding. Any chance this could still be merged? @bdbch

@moham96 moham96 requested a review from svenadlung as a code owner July 28, 2023 08:30
@fabioborges
Copy link

Hey guys, is there anything else preventing the merging of this pull request?

Copy link
Contributor

@bdbch bdbch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, cc @svenadlung

@fabioborges
Copy link

Hey guys, it seems like this PR is ready to be integrated into a new release, isn't it?
I'm anxiously waiting for it :)
Tks for the amazing job!

@Skryabind
Copy link

Skryabind commented Nov 8, 2023

Is any way to make a link on mention block while this PR hasn't merged?

@andrictham
Copy link

andrictham commented Nov 14, 2023

@Skryabind Yes, you can extend the stock Mention extension and add whatever methods you want to your extended version.

You can modify the text or HTML being rendered, or render a node view.

See here.

Not sure why this PR isn’t yet merged, though. It’s a fairly small change. Maybe the core team is busy.

@Skryabind
Copy link

@andrictham thank you! I did it by using extend and changing the "name" parameter in config.

@janthurau janthurau merged commit 3e9f9a6 into ueberdosis:develop Nov 22, 2023
14 checks passed
@MichaelGoff
Copy link

Just wanted to give a heads up to anyone that comes across this.

The change to remove renderLabel from the Mention extension's options caused a breaking change in 2.2.0 in my custom Mention extension that was using it. I had to make a code change and switch to use renderText instead.

@rnystrom
Copy link

rnystrom commented Mar 14, 2024

Just a heads up, this completely broke rendering data attributes. We fixed it by extending the mention extension with:

renderHTML({ node, HTMLAttributes }) {
  return [
    'span',
    mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
    `${this.options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
  ]
}

Even the TipTap mentions demo does not render data attributes anymore

https://tiptap.dev/docs/editor/api/nodes/mention

@moham96
Copy link
Contributor Author

moham96 commented Mar 14, 2024

Just wanted to give a heads up to anyone that comes across this.

The change to remove renderLabel from the Mention extension's options caused a breaking change in 2.2.0 in my custom Mention extension that was using it. I had to make a code change and switch to use renderText instead.

can you provide a minimal example with the problem?

@moham96
Copy link
Contributor Author

moham96 commented Mar 14, 2024

Just a heads up, this completely broke rendering data attributes. We fixed it by extending the mention extension with:

renderHTML({ node, HTMLAttributes }) {
  return [
    'span',
    mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
    `${this.options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
  ]
}

Even the TipTap mentions demo does not render data attributes anymore

https://tiptap.dev/docs/editor/api/nodes/mention

Data attributes work fine in the demo, can you provide a reproducible example?

@pondorasti
Copy link

Just a heads up, this completely broke rendering data attributes. We fixed it by extending the mention extension with:

renderHTML({ node, HTMLAttributes }) {
  return [
    'span',
    mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
    `${this.options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
  ]
}

Even the TipTap mentions demo does not render data attributes anymore
https://tiptap.dev/docs/editor/api/nodes/mention

Data attributes work fine in the demo, can you provide a reproducible example?

@moham96 you can inspect mention elements in the demo and you will see that no attributes are being attached with the span. This is not just the case for data-id, but any HTMLAttributes that are passed to the node.

CleanShot 2024-03-14 at 17 33 30@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.