ueberdosis / tiptap

The headless rich text editor framework for web artisans.
https://tiptap.dev
MIT License
27.63k stars 2.3k forks source link

Parse nested lists from html #1731

Closed TNick closed 3 years ago

TNick commented 3 years ago

Description Parsing nested html lists results in wrong content.

Steps to reproduce the bug Steps to reproduce the behavior:

  1. Create a nested list in html.
  2. Feed it to content parameter.
  3. See the result in editor.

CodeSandbox I created a CodeSandbox to help you debug the issue:

Edit tiptap2-react (forked)

Expected behavior Nested lists should look like the input data.

Environment?

Additional context

<p>Paragraph 1 </p>
<ul>
  <li>list item 1</li>
  <li>
    <ul>
      <li>nested list item 1</li>
      <li>nested list item 2</li>
    </ul>
  </li>
</ul>

gets converted into this:

{
  "doc": {
    "type": "doc",
    "content": [
      {
        "type": "paragraph",
        "content": [
          {
            "type": "text",
            "text": "Paragraph 1"
          }
        ]
      },
      {
        "type": "bulletList",
        "content": [
          {
            "type": "listItem",
            "content": [
              {
                "type": "paragraph",
                "content": [
                  {
                    "type": "text",
                    "text": "list item 1"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "content": [
              {
                "type": "paragraph",
                "content": [
                  {
                    "type": "text",
                    "text": "nested list item 1"
                  }
                ]
              },
              {
                "type": "bulletList",
                "content": [
                  {
                    "type": "listItem",
                    "content": [
                      {
                        "type": "paragraph",
                        "content": [
                          {
                            "type": "text",
                            "text": "nested list item 2"
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}
philippkuehn commented 3 years ago

(removed)

philippkuehn commented 3 years ago

oh sorry, didn't see that wrong json. have to look at this later.

philippkuehn commented 3 years ago

Ok, this is because we define the content for list items as paragraph block*. ProseMirror suggests that.

ProseMirror expects a list structure like this:

<p>Paragraph 1 </p>
<ul>
  <li>list item 1</li>
  <li>list item 2
    <ul>
      <li>nested list item 1</li>
      <li>nested list item 2</li>
    </ul>
  </li>
</ul>

You can overwrite the content option for list items with block* which will fix the parsing of your HTML but after that, some commands like lifting and sinking lists doesn’t work as expected.

Since this is the suggested way of ProseMirror I’ll mark that as wontfix for now.