sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
4.96k stars 390 forks source link

throw new Error("'block' type is not defined in this schema (required)."); #6714

Open deva7madIP opened 2 weeks ago

deva7madIP commented 2 weeks ago

Describe the bug

Error while trying to convert html text to sanity block @sanity/block-tools.

To Reproduce

Steps to reproduce the behavior:

async function convertHtmlToBlocks(html) {
    const slateAst = await htmlToSlateAST(html);
    console.log(slateAst);
    const blog = htmlToBlocks(html, blockContentType, {
        parseHtml: (html) => new JSDOM(html),
            rules: [
            // Special rule for code blocks
            {
                deserialize(el, next, block) {
                    if (el.tagName.toLowerCase() != 'pre') {
                        return undefined
                    }
                    const code = el.children[0]
                    const childNodes =
                        code && code.tagName.toLowerCase() === 'code' ? code.childNodes : el.childNodes
                    let text = ''
                    childNodes.forEach((node) => {
                        text += node.textContent
                    })
                    // Return this as an own block (via block helper function), instead of appending it to a default block's children
                    return block({
                        _type: 'code',
                        language: 'javascript',
                        text: text,
                    })
                },
            },
        ],
    },);
    console.log(blog);
    return blog;
}
const blockContentType = {
    name: 'post_detail',
    type: 'array',
    of: [
        {
            type: 'block',
            styles: [
                { title: 'Normal', value: 'normal' },
                { title: 'H1', value: 'h1' },
                { title: 'H2', value: 'h2' },
                { title: 'H3', value: 'h3' },
                { title: 'H4', value: 'h4' },
                { title: 'H5', value: 'h5' },
                { title: 'H6', value: 'h6' },
                { title: 'Quote', value: 'blockquote' }
            ],
            lists: [
                { title: 'Bullet', value: 'bullet' },
                { title: 'Numbered', value: 'number' }
            ],
            marks: {
                decorators: [
                    { title: 'Strong', value: 'strong' },
                    { title: 'Emphasis', value: 'em' },
                    { title: 'Code', value: 'code' }
                ],
                annotations: [
                    {
                        title: 'URL',
                        name: 'link',
                        type: 'object',
                        fields: [
                            {
                                title: 'URL',
                                name: 'href',
                                type: 'url'
                            }
                        ]
                    }
                ]
            }
        }
    ]
};

Expected behavior

    throw new Error("'block' type is not defined in this schema (required).");
          ^

Error: 'block' type is not defined in this schema (required).
    at blockContentFeatures (/home/deva7mad/Work/driven-properties-app/node_modules/@sanity/block-tools/lib/index.js:144:11)
    at createRuleOptions (/home/deva7mad/Work/driven-properties-app/node_modules/@sanity/block-tools/lib/index.js:385:20)
    at new HtmlDeserializer (/home/deva7mad/Work/driven-properties-app/node_modules/@sanity/block-tools/lib/index.js:978:57)
    at htmlToBlocks (/home/deva7mad/Work/driven-properties-app/node_modules/@sanity/block-tools/lib/index.js:1020:10)
    at convertHtmlToBlocks (/home/deva7mad/Work/driven-properties-app/sanity/utils/importBlogs.js:66:18)
    at async transformBlogData (/home/deva7mad/Work/driven-properties-app/sanity/utils/importBlogs.js:98:20)
    at async importBlogs (/home/deva7mad/Work/driven-properties-app/sanity/utils/importBlogs.js:123:33)

Node.js v18.17.1

Which versions of Sanity are you using?

Run sanity versions in the terminal and copy-paste the result here.

Sanity 3.42.1

What operating system are you using?

Ubuntu 22

Which versions of Node.js / npm are you running?

Node: v18.17.1 NPM: 9.6.7