npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

contentful-richtext-converter

v1.3.7

Published

Easily convert HTML to Contentful RichText and back to HTML

Downloads

251

Readme

This is a module build to convert HTML data into the Contentful ready RichText JSON, and convert RichText back to HTML. It is intended to be used during migrations or when working with the Contentful Management API.

It handles all kinds of features and usecases to ensure full functionality with all of the aspects of the Contentful RichText Editor. Some are easy, some a quirky - but it's a working progress.

Use at your own risk :)

🚀 Playground

Want to try it out?

Jump to the Playground where you can "write" or "copy-paste" a chunk of HTML and instantly get RichText JSON back.

Try it out, and get busy coding :)

🔎 Current Status

The project is currently still a working progress with possible future changes, but currently set up to support both ESM (browser) and CommonJS with node. The functionality is somewhat feature complete, but further testing, typescript and even better implementations will be coming in the near future.

⚙️ Installation

Using npm:

npm i contentful-richtext-converter

Usage (HTML to RichText)

Node (CommonJS)

const { htmlToRichText } = require("contentful-richtext-converter");
const html = '<p>Hello <strong>World</strong></p>';
const result = htmlToRichText(html);

Browser (ESM)

import { htmlToRichText } from 'contentful-richtext-converter';
const html = '<p>Hello <strong>World</strong></p>';
const result = htmlToRichText(html);

Output:

{
    "nodeType": "document",
    "data": {},
    "content": [
        {
            "nodeType": "paragraph",
            "data": {},
            "content": [
                {
                    "nodeType": "text",
                    "value": "Hello ",
                    "marks": [],
                    "data": {}
                },
                {
                    "nodeType": "text",
                    "value": "World",
                    "marks": [
                        {
                            "type": "bold"
                        }
                    ],
                    "data": {}
                }
            ]
        }
    ]
}

You might also need to validate and test an HTML output from the RichText data. This is just a simple RichText to HTML output. It allows to output - and work with all basic HTML elements of the RichText editor and supports hyperlinks, but isn't the "current" solution for deeper outputs with Entry blocks, Assets and Inline Entries. This will be covered later on, but for now this is just a simple conversion.

Usage (RichText to HTML)

Node (CommonJS)

const { richTextToHtml } = require("contentful-richtext-converter");
const json = {"nodeType": "document", "data": {}, "content": [{"nodeType": "paragraph", "data": {}, "content": [{"nodeType": "text", "value": "Hello ", "marks": [], "data": {}}, {"nodeType": "text", "value": "World", "marks": [{"type": "bold"}], "data": {}}]}]};
const result = richTextToHtml(json);

Browser (ESM)

import { richTextToHtml } from 'contentful-richtext-converter';
const json = {"nodeType": "document", "data": {}, "content": [{"nodeType": "paragraph", "data": {}, "content": [{"nodeType": "text", "value": "Hello ", "marks": [], "data": {}}, {"nodeType": "text", "value": "World", "marks": [{"type": "bold"}], "data": {}}]}]};
const result = richTextToHtml(json);

Output:

<p>Hello <strong>World</strong></p>

✨ Features

The htmlToRichText(html,options) function takes to parameters:

html is a string of HTML

options is an Object to enable or disable default settings for html manipulation.

The default settings:

{
    "fixTagsAndSpaces": true,
    "removeDivsAndSpan": true
}

These features are enabled by default, but can be set to false with the options parameter.

fixTagsAndSpaces does a basic cleanup, closing tags and removing unwanted spacing.

removeDivsAndSpan removes all elements of type DIV and SPAN - leaving only the flat valid HTML structure for the RichText JSON.

Though when a DIV or SPAN has a data-sys-id it can be used to automatically convert the element to a Entry Block or Inline Entry.

This will be covered later on, when thoroughly tested.


😺 Git Repository

https://github.com/t00bstAr/contentful-richtext-converter