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

markdown-to-draft fails when value is blank #33

Closed Jpunt closed 6 years ago

Jpunt commented 7 years ago

When the initial value is blank (which is very likely in my particular case), markdownToDraft throws:

markdownToDraft('')
// Uncaught TypeError: Cannot read property 'getKey' of undefined
Rosey commented 7 years ago

@Jpunt hmm, I just wrote a test case for this in order to start debugging but the test is passing for me. 🤔

    fit('renders an empty string correctly', function () {
      var markdown = '';
      var conversionResult = markdownToDraft(markdown);
      expect(conversionResult.blocks.length).toEqual(0);
    })

I also tried running it against convertFromRaw in case that's where you were hitting issues, but convertFromRaw also seemed to handle it ok. Any thoughts on how/when you saw this?

Jpunt commented 7 years ago

You are correct, in the way that a markdownToDraft with a blank value returns an empty blocks-array. Where it goes wrong is when I put this result into Draft's createWithContent (through convertFromRaw).

constructor(props: Props) {
  super(props);

  const rawData = markdownToDraft(props.value);
  // {entityMap: {…}, blocks: Array(0)}

  const contentState = convertFromRaw(rawData);
  // ContentState {_map: Map}

  this.state = {
    editor: EditorState.createWithContent(contentState),
    // Uncaught TypeError: Cannot read property 'getKey' of undefined
  };
}

Should I be doing this in another way maybe?

Rosey commented 6 years ago

:( Seems weird to me that convertFromRaw wouldn't mind but createWithContent would. Added a work-around to https://github.com/Rosey/markdown-draft-js/pull/35 though.

Jpunt commented 6 years ago

Yeah, I agree it's weird. Thanks! 🎉