⚠️Draft Content

Storyblok Raises $80M Series C - Read News

What’s the True Total Price of Enterprise CMS? Find out here.

Skip to main content

Converting HTML to Richtext JSON

When working with the Storyblok Richtext field, directly adding raw HTML does not work. Hence, it's necessary to convert HTML content to a format compatible with the Storyblok Richtext field. This can be achieved as follows:

  1. HTML to Markdown Conversion: First, the HTML data needs to be converted to Markdown format. Various libraries, such as turndown, facilitate this conversion process effectively.
  2. Utilize Storyblok Markdown Richtext Converter: Once the content is available in Markdown format, the storyblok-markdown-richtext library can be utilized. This tool aids in transforming Markdown data into the JSON format expected by the Storyblok Richtext field.

Following these steps ensures seamless integration of our HTML content with the Storyblok RichText editor, enabling efficient content management and editing capabilities.

Code Example
        
      import TurndownService from 'turndown';
import { markdownToRichtext } from 'storyblok-markdown-richtext';

// Sample HTML content
const htmlContent = `
    <h1>Hello, World!</h1>
    <p>This is a <strong>sample</strong> HTML content.</p>
`;

// Step 1: Convert HTML to Markdown
const turndownService = new TurndownService();
const markdownContent = turndownService.turndown(htmlContent);

// Step 2: Convert Markdown to Storyblok RichText JSON format
const richtextJson = markdownToRichtext(markdownContent);

console.log(richtextJson);
    
hint:

For further information, please refer to this detailed guide on migrating data from any platform to Storyblok. It provides insights into the conversion process and offers strategies for seamless data migration.


Author

Dipankar Maikap

Dipankar Maikap

Dipankar is a seasoned Developer Relations Engineer at Storyblok, with a specialization in frontend development. His expertise spans across various JavaScript frameworks such as Astro, Next.js, and Remix. Passionate about web development and JavaScript, he remains at the forefront of the ever-evolving tech landscape, continually exploring new technologies and sharing insights with the community.