Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@graphcms/html-to-slate-ast
Advanced tools
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.
Note: If you're using this package with Node.js, you'll need to use version 18 or higher.
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 slate@0.66.1 slate-hyperscript@0.67.0 @graphcms/html-to-slate-ast
# for Node.js or isomorphic usage, jsdom is required
npm install jsdom
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));
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.
Licensed under the MIT License.
Made with 💜 by Hygraph 👋 join our community!
FAQs
Convert HTML to Hygraph's RichTextAST (slate)
The npm package @graphcms/html-to-slate-ast receives a total of 1,121 weekly downloads. As such, @graphcms/html-to-slate-ast popularity was classified as popular.
We found that @graphcms/html-to-slate-ast demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.