syntax-tree / mdast-util-to-markdown

mdast utility to serialize markdown
http://unifiedjs.com
MIT License
100 stars 20 forks source link

roundtripping inline code with list-like text results in list #13

Closed ChristianMurphy closed 3 years ago

ChristianMurphy commented 3 years ago

Subject of the issue

`
-`

is stringified as:

`
- `

which changes the structure

Your environment

Steps to reproduce

parse

`
-`

which has the structure

{
    "type": "root",
    "children": [
        {
            "type": "paragraph",
            "children": [
                {
                    "type": "inlineCode"
                }
            ]
        }
    ]
}

and stringify it:

`
- `

the resulting markdown has a different structure than the original

{
    "type": "root",
    "children": [
        {
            "type": "paragraph",
            "children": [
                {
                    "type": "text"
                }
            ]
        },
        {
            "type": "list",
            "ordered": false,
            "start": null,
            "children": [
                {
                    "type": "listItem",
                    "checked": null,
                    "children": [
                        {
                            "type": "paragraph",
                            "children": [
                                {
                                    "type": "text"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

:notebook: comparing how the two pieces of markdown text are being parsed with https://spec.commonmark.org/dingus it appears in both cases it is parsed as expected.

Expected behavior

structure is the same

Actual behavior

structure is different

wooorm commented 3 years ago

Hmm. Complex.

Inline code is rather fragile when it comes to block markers after newlines:

`
# `

`
1. `

`
1) `

`
+ `

`
- `

`
* `

`
<div `

`
= `

`
> `

Yields:

`

`

`

  1. `

1)

`

`

`

<div

=

`

`

wooorm commented 3 years ago

Maybe, when a newline is found, we can use the unsafe patterns’s atBreaks, and use a space instead of a line endings?