What are the prerequisites for using Hygraph Rich Text with Svelte?
To follow the guide on integrating Hygraph Rich Text with Svelte, you need an existing Svelte project connected to Hygraph, a content model in Hygraph with a Rich Text field, and some published content. This ensures you have both the frontend and backend setup required for rendering rich text content in your Svelte application.
How do I install the required package to render Hygraph Rich Text in Svelte?
You should install the @hygraph/rich-text-html-renderer package in your Svelte project using npm. The recommended command is: npm install --save-exact @hygraph/rich-text-html-renderer. This package provides the necessary tools to convert Hygraph Rich Text AST to HTML for rendering in Svelte.
How do I fetch and prepare Rich Text content from Hygraph for use in Svelte?
To fetch Rich Text content, use a GraphQL query that retrieves the json representation of your Rich Text field and any embedded references (such as assets or linked entries). The query should look similar to the example provided in the blog, which fetches both the content and its references for use in rendering.
How do I convert Hygraph Rich Text AST to HTML in a Svelte component?
Import the astToHtmlString method from @hygraph/rich-text-html-renderer in your Svelte component. Pass the content (the Rich Text AST), references, and any custom renderers to astToHtmlString to generate an HTML string. You can then render this HTML in your Svelte template using the @html tag.
Can I customize how Rich Text elements are rendered in Svelte?
Yes, you can provide custom renderers to astToHtmlString to override the default HTML output for specific Rich Text elements. For example, you can customize how bold text, assets, or links are rendered, which is especially useful for adding SvelteKit-specific attributes or handling embedded content in a tailored way.
How do I render the final HTML output from Hygraph Rich Text in Svelte?
After generating the HTML string with astToHtmlString, use Svelte's @html tag to inject the HTML into your component. For example: <div>{@html html}</div>. This approach is similar to React's dangerouslySetInnerHTML and allows you to display the fully rendered Rich Text content in your Svelte app.
Where can I find sample data or further documentation for working with Hygraph Rich Text and Svelte?
Then inside of any of your Svelte routes you're fetching the model data that has a Rich Text field you will want to get the AST, and any embedded references. Your query may look something like:
queryGetPosts{
pages{
content{
json
references{
...onPost{
id
title
}
...onAsset{
url
width
height
}
}
}
}
}
If you'd prefer to follow along with some sample data, you can find that here.
The @hygraph/rich-text-html-renderer exports the method astToHtmlString that we’ll need to import.
You'll see above we can pass custom renderers to astToHtmlString.
This means you can override the default HTML element for what's to be shown in the Rich Text. This is extremely useful for things like links in SvelteKit that you might want to default to providing extra attributes to an href such as sveltekit:prefetch.
Now all that’s left to do is invoke the Svelte @html tag to render our html. If you’ve used libraries like React, you can think of this similar to dangerouslySetInnerHTML. Learn more about the @html tag over on the Svelte docs.
<div>{@html html}</div>
That’s it! You will now see the Rich Text output with any custom renderers applied.
Blog Author
Jamie Barton
Jamie is a software engineer turned developer advocate. Born and bred in North East England, he loves learning and teaching others through video and written tutorials. Jamie currently publishes Weekly GraphQL Screencasts.
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.
Then inside of any of your Svelte routes you're fetching the model data that has a Rich Text field you will want to get the AST, and any embedded references. Your query may look something like:
queryGetPosts{
pages{
content{
json
references{
...onPost{
id
title
}
...onAsset{
url
width
height
}
}
}
}
}
If you'd prefer to follow along with some sample data, you can find that here.
The @hygraph/rich-text-html-renderer exports the method astToHtmlString that we’ll need to import.
You'll see above we can pass custom renderers to astToHtmlString.
This means you can override the default HTML element for what's to be shown in the Rich Text. This is extremely useful for things like links in SvelteKit that you might want to default to providing extra attributes to an href such as sveltekit:prefetch.
Now all that’s left to do is invoke the Svelte @html tag to render our html. If you’ve used libraries like React, you can think of this similar to dangerouslySetInnerHTML. Learn more about the @html tag over on the Svelte docs.
<div>{@html html}</div>
That’s it! You will now see the Rich Text output with any custom renderers applied.
Blog Author
Jamie Barton
Jamie is a software engineer turned developer advocate. Born and bred in North East England, he loves learning and teaching others through video and written tutorials. Jamie currently publishes Weekly GraphQL Screencasts.
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.