Rosey / markdown-draft-js

A tool to convert content created in DraftJS to markdown and vice versa.
https://rosey.github.io/markdown-draft-js/
MIT License
318 stars 70 forks source link

Outputs incorrect markdown #28

Closed Jpunt closed 7 years ago

Jpunt commented 7 years ago

When the user has bold enabled and starts typing, this will result in invalid markdown every time the user types a space:

some words before bold, **some words in bold **and some words after bold

Because the space after the word "bold" is before the two stars instead of after them, this will result in a plain text (containing the actual stars).

Rosey commented 7 years ago

Interesting, this is an issue I'm aware of and have commented on before in code. I actually thought there was a work-around in place for it already but I'll take a look, because it's clearly not working!

Rosey commented 7 years ago

Wait, is this when converting draft to markdown or vice versa? Because I just added some tests and it is working when converting draft to markdown which I thought was the case you were talking about here? I did add a pr with some changes to how it works to handle more tabs and stuff but this basic case I think did already work properly?

This is the test with the raw draft object that is converted to markdown properly:

  it('renders inline styled text with trailing whitespace correctly', function () {
    /* eslint-disable */
    var rawObject = {"entityMap":{},"blocks":[{"key":"dvfr1","text":"Test Bold Text Test","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":5,"length":10,"style":"BOLD"}],"entityRanges":[],"data":{}}]};
    /* eslint-enable */

    var markdown = draftToMarkdown(rawObject);
    expect(markdown).toEqual('Test **Bold Text** Test');
  });
Jpunt commented 7 years ago

Yeah, that is exactly what I mean 🤔 Your test looks great. But the issue seems to be apparent still. You can also see it in the demo. Enable bold, type test and see the result **test **. Right?

Wait, I guess my example was wrong! Sorry about that 😄 When there's text after the bold part, the space gets fixed (which is your test 👍), but as long as there's nothing after the bold part and the space, the space remains inside the bold part (like just and only the value test).

Rosey commented 7 years ago

Ah ok thanks for the additional info! I'll add some tests for that situation as well and make sure my latest PR handles those cases :)

Rosey commented 7 years ago

Fix just got merged - https://github.com/Rosey/markdown-draft-js/pull/31

And I have filed a separate issue to get fixed for a very similar bug: https://github.com/Rosey/markdown-draft-js/issues/32

Will do a bug fix release soon.