import blockTools from '@sanity/block-tools';
import sanitizeHTML from './sanitize-html.js';
import blocksToHtml from '@sanity/block-content-to-html';
import Schema from '@sanity/schema';
import { JSDOM } from 'jsdom';
const defaultSchema = Schema.compile({
name: 'myBlog',
types: [
{
type: 'object',
name: 'post',
fields: [
{
title: 'Title',
type: 'string',
name: 'title',
},
{
title: 'Body',
name: 'body',
type: 'array',
of: [{ type: 'block' }],
},
],
},
],
});
function htmlToBlocks(html, options) {
if (!html) {
return [<bunch of rules>];
}
const blockContentType = defaultSchema
.get('post')
.fields.find((field) => field.name === 'body').type;
const blocks = blockTools.htmlToBlocks(sanitizeHTML(html), blockContentType, {
parseHtml: (htmlContent) => new JSDOM(htmlContent).window.document,
rules: [],
});
return blocks;
}
export default (bodyHTML) => htmlToBlocks(bodyHTML);