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

How to render custom text in renderHTML() ? #6027

Closed
1 task done
celestial83u83 opened this issue Jan 16, 2025 · 3 comments
Closed
1 task done

How to render custom text in renderHTML() ? #6027

celestial83u83 opened this issue Jan 16, 2025 · 3 comments
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@celestial83u83
Copy link

celestial83u83 commented Jan 16, 2025

Affected Packages

react, extension-link, core

Version(s)

2.11

Bug Description

I'm trying to render custom HTML in read-only view (editable = false) in a custom link extension using renderHTML() for example:

    renderHTML({ HTMLAttributes, node }) {

      const url = HTMLAttributes.href;
      return ['span', HTMLAttributes, `hello world, (${url})`];
    },

However I tried this on a hyperlink and it only displays the hyperlink text itself and not the url or the "hello world" example text I put. For example, <a href="url">link text</a> would only display link text. Is there a way to display the custom text as well?

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

The custom text shows up

Additional Context (Optional)

No response

Dependency Updates

  • Yes, I've updated all my dependencies.
@celestial83u83 celestial83u83 added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Jan 16, 2025
@nperez0111
Copy link
Contributor

The renderHTML looks fine, but does not seem to be applying, you didn't share your whole code, so can't help you further

@celestial83u83
Copy link
Author

celestial83u83 commented Jan 16, 2025

@nperez0111 Sorry, here is my example code.

import { useEditor, EditorContent } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'

const CustomLink = Link.extend({
    renderHTML({ HTMLAttributes }) {
      const url = HTMLAttributes.href;
      return ['span', HTMLAttributes, `hello world, (${url})`];
    },
  });

const Tiptap = () => {
  const editor = useEditor({
    extensions: [
      StarterKit,
      CustomLink
    ],
    editable: false,
    content: '<p>Content here</p><a href="https://www.google.com/">link text</a>',
  })

  return (
    <EditorContent editor={editor} />
  )
}

@nperez0111
Copy link
Contributor

This seems wrong:

'<p>Content here</p><a href="https://www.google.com/">link text</a>',

It should be:

'<p>Content here <a href="https://www.google.com/">link text</a></p>',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
None yet
Development

No branches or pull requests

2 participants