@automattic/rtjson-to-wpblocks

3.0.0 • Public • Published

rtjson-to-wpblocks

This module converts Rich Text JSON (RTJson) format used by Day One to WordPress Gutenberg blocks.

It writes out the gutenberg-compatible HTML by hand instead of using official Gutenberg JS libraries because as of October 2024 those libraries are having trouble running in a server-side JS context (relying on Window APIs).

Installation

npm install @automattic/rtjson-to-wpblocks

Usage

rtjNodesToGBHtml(nodes, mediaLookup)

Converts an array of RTJson nodes to WordPress Gutenberg HTML blocks.

const { rtjNodesToGBHtml } = require('@automattic/rtjson-to-wpblocks');

const nodes = [...]; // Your RTJson nodes
const mediaLookup = {...}; // Your media lookup object

const gutenbergHtml = rtjNodesToGBHtml(nodes, mediaLookup);
  • nodes: An array of RTJson nodes to convert.
  • mediaLookup: (Optional) A lookup table for media items. Given an embedded object ID, it returns the WordPress ID and URL for the corresponding media item on WordPress.

Returns a string of WordPress Gutenberg HTML blocks.

rtjDocToGBHtml(rtj, mediaLookup)

Converts a complete RTJson document to WordPress Gutenberg HTML blocks.

const { rtjDocToGBHtml } = require('@automattic/rtjson-to-wpblocks');

const rtjDoc = {...}; // Your RTJson document
const mediaLookup = {...}; // Your media lookup object

const gutenbergHtml = rtjDocToGBHtml(rtjDoc, mediaLookup);
  • rtj: The RTJson document to convert.
  • mediaLookup: (Optional) A lookup table for media items.

Returns a string of WordPress Gutenberg HTML blocks.

parseAndValidateRTJson(rtjson)

Parses and validates a RTJson string.

const { parseAndValidateRTJson } = require("@automattic/rtjson-to-wpblocks");

const rtjsonString = "..."; // Your RTJson string

const result = parseAndValidateRTJson(rtjsonString);
  • rtjson: The RTJson string to parse and validate.

Returns either a validated RTJson document object or an array of validation errors.

rtjsonDocStringToWPBlocksString(rtjsonString, mediaLookup)

Converts a RTJson document string directly to WordPress Gutenberg HTML blocks.

const { rtjsonDocStringToWPBlocksString } = require('@automattic/rtjson-to-wpblocks');

const rtjsonString = '...'; // Your RTJson string
const mediaLookup = {...}; // Your media lookup object

const gutenbergHtml = rtjsonDocStringToWPBlocksString(rtjsonString, mediaLookup);
  • rtjsonString: The RTJson document string to convert.
  • mediaLookup: A lookup table for media items.

Returns a string of WordPress Gutenberg HTML blocks. Throws an error if the RTJson fails validation.

JSON Schema & TypeScript types

This package provides a typescript definition for the RTJson format, as well as a JSON Schema for validation.

To generate the JSON Schema from the typescript types, run:

npm run generate-schema

🚧 THIS SHOULD BE DONE EVERY TIME THE TYPE DEFINITIONS FOR RTJson ARE CHANGED. 🚧

Testing

npm test

License

MIT

Contributing

If you have admin access to this repo, and permission to publish packages for the @automattic scope on npm, you can release a new version by running:

npm version <patch|minor|major>
npm publish

/@automattic/rtjson-to-wpblocks/

    Package Sidebar

    Install

    npm i @automattic/rtjson-to-wpblocks

    Weekly Downloads

    126

    Version

    3.0.0

    License

    MIT

    Unpacked Size

    88.6 kB

    Total Files

    18

    Last publish

    Collaborators

    • macbre
    • yuliyan
    • mjangda
    • matticbot
    • a8c
    • blowery
    • ehg_
    • sgomes
    • tyxla
    • saroshaga
    • nejclovrencic
    • sirbrillig
    • chriszarate
    • robersongomes
    • johngodley
    • jeherve
    • daledupreez-a8c
    • t2dw4t
    • luismulinari
    • andrea-sdl
    • elazzabi
    • fmfernandes
    • sirreal
    • wwa
    • chihsuan
    • manzoorwanijk
    • msurdi-a8c
    • newspack
    • dsmartg
    • kthai15
    • bgrgicak
    • robertsreberski_a8c
    • artpi
    • gmjuhasz
    • kat3samsin
    • brunobasto
    • dhenridev
    • micbosia8c
    • mrmurphy
    • wpvip-bot
    • etobiesen
    • alshakero
    • arthur791004
    • dilirity
    • mattwest-a8c
    • mehmoodak
    • natalia.vidal
    • chriskmnds
    • oandregal
    • galatanovidiu
    • kangzj