Make your rich text portable
Customizing rich text content with JavaScript just got easier thanks to our newly released Rich Text Resolver.
Published on Jan 29, 2024
Customizing rich text content with JavaScript just got easier thanks to our newly released Rich Text Resolver.
Published on Jan 29, 2024
Rich text content plays a key role in the domain of content creation, allowing editors to craft structured and formatted content through a simple WYSIWYG interface. Although the conventional HTML representation within Kontent.ai is robust, there is potential for enhancement, particularly in terms of customization beyond the capabilities of the rich text editor, as well as in the resolution of linked items and components. Let’s explore how our new Rich Text Resolver can improve your experience in this regard!
Kontent.ai rich text resolver provides an option to parse rich text HTML into a simple JSON tree structure. Combined with the helper methods included in the package, this sets up a baseline for the customization of your rich text content. More importantly, though, the JSON tree serves as an intermediary for transformation into a feature-packed, comprehensive format…
A well-defined standard, Portable Text provides an easily extensible alternative to HTML-rich text representation, complemented with a range of external packages for resolution in popular frameworks. Full definition and usage examples can be found in corresponding repositories. Let’s instead explore specific scenarios where Portable Text can come in handy.
Rich text resolution has so far been tied to one of our supported SDKs, in this case, TypeScript/JavaScript. As a result, if you were not using an SDK, either because you opted for our native GraphQL endpoint instead of REST API or have your own custom layer for querying the latter, you had to implement the component/linked item resolution from scratch.
With the resolver, this is no longer the case, as you can pull the rich text value from a GraphQL or REST API response and pass it to a suitable parse method for your environment (browserParse
or nodeParse
). The returned ParseResult
can be subsequently converted to Portable Text with the transformToPortableText
function and resolved using a suitable package depending on your language and framework of choice, such as React or Vue, with no SDK involved. Pretty neat, wouldn’t you say?
Modular content, in the form of either components or linked items, allows you to extend default rich text with more complex structures. Both of these entities are unified under a common ‘component’ custom block in their Portable Text representation, which can be resolved as per your requirements, for example, with @portabletext/to-html package:
On top of components and linked items, the object representation of Portable Text allows customizing default HTML tags as well. One situation this can be leveraged in is creating anchor links. By overriding a header tag (h1-h6
), you can add an ID attribute to it and wrap its inner content into an <a>
tag, thus creating a clickable anchor out of any heading used in the rich text. See example implementation using @portabletext/react package:
Kontent.ai assets in rich text are represented by a custom image
block in Portable Text. For their resolution, you can use a transformImage
helper method, accepting a callback function that should return a valid HTML/JSX, be it a bare-bones img
tag or a more complex and stylized structure. Default callbacks are provided for HTML/React and Vue.
Similarly to assets, tables in rich text are represented by a custom Portable Text table
block, a hierarchical structure with nested rows, columns, and cells that contain the actual content blocks. The package provides a helper function transformTable
, which accepts a callback to modify the content of the table’s cells. For rendering them as-is, default callbacks are again provided for your convenience.
Our new Rich Text Resolver provides a standalone alternative for component/linked items resolution and extends beyond SDK capabilities by allowing customization of default rich text entities. Combined with existing resolution packages for a majority of popular frameworks, it is a handy addition to an existing list of content manipulation tools at your disposal.
You can explore the full range of the tool's capabilities in the GitHub repository, where you can find further usage examples and complete documentation.
What if we told you there was a way to make your website a place that will always be relevant, no matter the season or the year? Two words—evergreen content. What does evergreen mean in marketing, and how do you make evergreen content? Let’s dive into it.
Lucie Simonova
Aug 29, 2022
How can you create a cohesive experience for customers no matter what channel they’re on or what device they’re using? The answer is going omnichannel.
Zaneta Styblova
Mar 10, 2022
In today’s world of content, writing like Shakespeare is not enough. The truth is, there are tons of exceptional writers out there. So what will make you stand out from the sea of articles posted every day? A proper blog post structure.
Lucie Simonova
Jul 22, 2022
American Bath Group gets to market faster with increased content velocity American Bath Group is here to build “a better bathware industry.” They believe that they can always learn more, and that there’s always a better way. When it comes to content management, their goal is no different. Using the Kontent.ai platform, American Bath Group has landed on a better, faster way to create and deliver digital content. Marc Lamothe, Systems Administrator at American Bath Group, and Akshay Sura from partner Konabos, shared how they successfully migrated 10 sites with over 50,000 products in only 8 months.
Faster project delivery and greater ROI Based in Minneapolis, USA, Trusted Choice is a brand that highlights the strengths of independent insurance agents. To better serve their customers, they needed to find a new digital platform that would allow their team to easily maintain content and make it easier to share important information and forms.