tommoor / slate-md-serializer

A Markdown serializer for the Slate editor framework
MIT License
63 stars 36 forks source link

deserializer/parser issue #35

Open bhirt opened 5 years ago

bhirt commented 5 years ago

It looks like support in deserializer seems to have a problem with markdown that should generate a leaf with multiple marks. This simple code demonstrates the issue.

import MarkdownSerializer from 'slate-md-serializer'

const Serializer = new MarkdownSerializer()
const test1 = "This is **_bold and italic_**."
const d1 = Serializer.deserialize(test1)
console.log(JSON.stringify(d1,null,2))

outputs

{
  "object": "value",
  "document": {
    "object": "document",
    "data": {},
    "nodes": [
      {
        "object": "block",
        "type": "paragraph",
        "data": {},
        "nodes": [
          {
            "object": "text",
            "leaves": [
              {
                "object": "leaf",
                "text": "This is ",
                "marks": []
              }
            ]
          },
          {
            "object": "text",
            "leaves": [
              {
                "object": "leaf",
                "text": "bold and italic",
                "marks": [
                  {
                    "object": "mark",
                    "type": "italic",
                    "data": {}
                  }
                ]
              },
              {
                "object": "leaf",
                "text": ".",
                "marks": []
              }
            ]
          }
        ]
      }
    ]
  }
}
tommoor commented 5 years ago

Some clues as to what caused this in the conversation here, https://github.com/outline/outline/issues/443

bhirt commented 5 years ago

I noticed that https://github.com/markedjs/marked handles my input correctly. It also seem to have more exhaustive rules than slate-md-serializer (but underscore is a new slate-md-serializer rule that cause problems). I am looking into this further. I did try updated the regex rules and captures to what marked is using, butit fails.

A quick debugging sessions shows that Renderer.prototype.strong and Renderer.prototype.em are both being called on my test input, but each time they create a new array of marks. My expected behavior would be the first call to create a marks array and the second call appends a mark to the existing one.

Is this something that would be committed if a sutable fix was found?

tommoor commented 5 years ago

Is this something that would be committed if a sutable fix was found?

Absolutely, that would be great. The library also has extensive regression tests, I'd recommend a TDD approach to working with the lib – writing the failing test first.