Migration blocker: Rich text from markdown error with unordered-list

I’m currently migrating our blog. I am converting html → markdown and then using the rich-text-from-markdown package to convert to rich text. however, I continue to get the following error when converting a simple unordered list. here is the error I’m getting: (sorry code ticks make this look worse)

InvalidEntry: {
“status”: 422,
“statusText”: “Unprocessable Entity”,
“message”: “Validation error”,
“details”: {
“errors”: [
{
“name”: “in”,
“details”: “Value must be one of expected values”,
“path”: [
“fields”,
“content”,
“en-US”,
“content”,
14,
“content”,
0,
“nodeType”
],
“value”: “ordered-list”,
“expected”: [
“paragraph”
]
}
]
},


here is an example of the json being used during createEntry:

content: [
{
“nodeType”: “unordered-list”,
“content”: [
{
“nodeType”: “list-item”,
“content”: [
{
“nodeType”: “paragraph”,
“content”: [
{
“nodeType”: “text”,
“value”: "They use ",
“marks”: ,
“data”: {}
},
{
“nodeType”: “text”,
“value”: “new”,
“marks”: [
{
“type”: “italic”
}
],
“data”: {}
}, … more json


Here is the function call for createEntry :

const created = await client.createEntry(“blogPost”, {
fields: {
title: {
[CONTENTFUL_LOCALE]: post.title,
},
slug: {
[CONTENTFUL_LOCALE]: post.slug,
},
seo: {
[CONTENTFUL_LOCALE]: {
sys: { type: “Link”, linkType: “Entry”, id: post.seoId },
},
},
author: {
[CONTENTFUL_LOCALE]: [
{
sys: {
type: “Link”,
linkType: “Entry”,
id: post.authorId,
},
},
],
},
categories: {
[CONTENTFUL_LOCALE]: [
{ sys: { type: “Link”, linkType: “Entry”, id: post.categoryId } },
],
},
content: {
[CONTENTFUL_LOCALE]: convertedContent,
},
featuredImage: post.featuredImage,
},
});


convertedContent is the output from richTextFromMarkdown and is just standard markdown from Turndown with no options other than using atx for headingStyle. I’m not sure where the real problem lies. With my code or if this is a bug since the rich text nodes are coming from the Contentful package.

In hopes that this could help someone else, I was able to get a great answer from Support.

TLDR, rich text does not support lists within a blockquote. This was an invalidation error, but the actual messaging from Contentful wasn’t helping me figure that out. I’m rather new to all the tooling so that was not something I was quick to think of as the problem. Below is the answer I received. There are a number of ways you can solve for this so I’ll leave that up to the reader.


Answer:

As of now, we don’t support adding unordered or ordered lists under blockquotes, and this is your case (if you tried to do this in the web app, our editor will remove the blockquote when you add a list inside it).

What you can do is to remove these blockquotes, maybe with something like .replaceAll('<blockquote>', '').replaceAll('</blockquote>', ''), this worked for the HTML you sent, However, I know this is not ideal.

You need a way to understand how our validation for the rich text data works, for that you have to look at these schemas https://github.com/contentful/rich-text/tree/master/packages/rich-text-types/src/schemas/generated and you need to validate each node with the corresponding schema.

These schemas are following the JSON schema specifications.

You can get the schema for any supported node type by using this:

// yarn add @contentful/rich-text-types
const { getSchemaWithNodeType } = require('@contentful/rich-text-types/dist/schemas')

then use something like ajv (https://ajv.js.org/) to validate each node type, if all of the validations passed you can save the rich text data without any issues.

I’ll open a ticket in our backlog to include validation options in the rich-text-from-markdown package so it tells you that the data is not valid.

Another option is to look at the error message, and see what node type is causing it, then go and read the schema to find what you need to alter to make it work.