kadikraman / draftjs-md-converter

Converts content from Draft.js blocks to Markdown and vice versa.
https://kadikraman.github.io/draftjs-md-converter/
MIT License
145 stars 37 forks source link

Incorrect number of newlines #59

Open philer opened 4 years ago

philer commented 4 years ago

Markdown considers a single newline (\n) irrelevant and uses at least one "blank line" to separate paragraphs. Additionally, a visible newline may be forced within a paragraph by appending at least two spaces at the end of a line (followed by a single newline). The original markdown description explains it better than me.

The point is that this module appears to use singular newlines to separate paragraphs and interprets multiple newlines as multiple empty paragraph (creating empty vertical space). This means an editor using this module will generate non standard markdown.

Please let me know if I missed something.

kadikraman commented 4 years ago

Hi there! Good point! To explain the reasoning, I'll have to go into how the converter works.

The content state in draft.js isn't just a string, it's stored in "draft.js blocks" which is a big old object and includes all the metadata about all inline styles and block styles. See this test for an example of what it looks like.

This library assumes what each "content block" equates to a "paragraph" in markdown and vice versa.

As for how we come to define a paragraph: in order to convert md -> draft.js, I used a library called @textlint/markdown-to-ast which handles converting the markdown string to an abstract syntax tree. Then I recurse over the tree to convert it into draft.js blocks. Therefore my definition of what constitutes a paragraph comes from this library.

So ultimately the question is, is "content block" in draft.js semantically equal to a "paragraph" in @textlint/markdown-to-ast?