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

@graphcms/html-to-slate-ast

v0.14.1

Published

Convert HTML to Hygraph's RichTextAST (slate)

Downloads

5,260

Readme

@graphcms/html-to-slate-ast

HTML to Slate AST converter for the Hygraph's RichTextAST format.

⚠️ This converter outputs the custom flavour of Slate AST that is used at Hygraph, and will most likely not produce an output compatible with your own Slate implementation. But feel free to fork it and adapt it to your needs.

⚡ Usage

Note: If you're using this package with Node.js, you'll need to use version 18 or higher.

1. Install

This package needs to have the packages slate and slate-hyperscript installed, and jsdom as well if you need to run the converter in Node.js.

npm install [email protected] [email protected] @graphcms/html-to-slate-ast

# for Node.js or isomorphic usage, jsdom is required
npm install jsdom

2. Convert your data

If you are using Node.js, you will need to use the htmlToSlateAST function, which returns a Promise. If you are using this package in the browser, you can use the htmlToSlateASTSync function, which is synchronous and doesn't require jsdom.

import { htmlToSlateAST } from '@graphcms/html-to-slate-ast';
// Or if you are using this package in the browser
import { htmlToSlateASTSync } from '@graphcms/html-to-slate-ast';

async function main() {
  const htmlString = '<div><p>test</p></div>'; // or import from a file or database
  const ast = await htmlToSlateAST(htmlString);
  console.log(JSON.stringify(ast, null, 2));
}

main()
  .then(() => process.exit(0))
  .catch(e => console.error(e));

3. Use it in your Content API mutations

The output of this converstion is compatible with our RichTextAST GraphQL type and can be used to import content in your Rich Text fields. Here's a mutation example:

mutation newArticle($title: String!, $content: RichTextAST) {
  createArticle(data: { title: $title, content: $content }) {
    id
    title
    content {
      html
      raw
    }
  }
}

The output generated by htmlToSlateAST will represent the children array of the Slate editor object. However, when creating or updating the value of a Rich text field, you are setting the value of the editor node itself. This means that the output should be transformed into a Rich text compatible object, for example:

const data = await client.request(newArticleQuery, {
  title: 'Example title for an article',
  content: { children: ast },
});

Here, in terms of Slate, $content is the editor node, so the $ast array must be assigned to the children key in that object. If you don't assign it to the children key, the mutation will fail with the following error.

ClientError: could not transform richText: Values should be an array of objects containing raw rich text values.

You can see the full example using graphql-request to mutate the data into Hygraph here.

See the docs about the Rich Text field type and Content Api mutations.

📝 License

Licensed under the MIT License.


Made with 💜 by Hygraph 👋 join our community!