storyblok / storyblok-js-client

Universal JavaScript client for Storyblok's API
MIT License
128 stars 87 forks source link

Internal link in the RichTextResolver is not rendering the entire URL but just the slug. #793

Open Dawntraoz opened 8 months ago

Dawntraoz commented 8 months ago

Expected Behavior

The link should be rendered as <a href="<URL>" ...>, instead of <a href="<slug>" ...>.

Current Behavior

A user (@mlalitthapa) using our Nuxt SDK reported this issue: https://github.com/storyblok/storyblok-nuxt/issues/756.

He's using the internal link inside a rich text editor and rendering it using renderRichText function. But the anchor link is using the wrong URL in the href. Let's say his contact page has URL /contact and slug of contact. Then the anchor link is just using contact as URL, instead of /contact.

Steps to Reproduce

  1. Render the content of a RichText with an internal link
  2. Check the URL being added to href Internal Link Rendered incorrectly
alvarosabu commented 1 month ago

Hi @Dawntraoz this will be really easy to achieve with the new richtext https://github.com/storyblok/richtext

Dawntraoz commented 1 month ago

Hi @alvarosabu, that sounds amazing! Thanks for caring 🎉 Can you share here or in the Nuxt issue the workaround proposed, and when will it be stable to use?

alvarosabu commented 1 month ago

Yes, @Dawntraoz they could do something like this:

// Vanilla
const options = {
      resolvers: {
        [MarkTypes.LINK]: (node) => {
          if node.attrs?.linktype === 'STORY' {
             return `<a href="/${node.attrs?.href}"`
          }  
      },
}

const html = richTextResolver<string>(options).render(doc)