draftjs-to-markdown icon indicating copy to clipboard operation
draftjs-to-markdown copied to clipboard

Outputs incorrect markdown

Open JasperH8g opened this issue 8 years ago β€’ 4 comments

Let's assume a user starts typing, enables bold, types some more words (and ends with a space).

As soon as the user disabled bold, this results in invalid markdown:

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

The space after the word bold should be placed after the two stars instead of before them, or it'll result in a plain text (containing the stars themselves).

Edit: This issue also happens a lot sooner:

  • Enable bold
  • Type a word
  • Type a space

This will result in **word **, instead of the word in bold.

JasperH8g avatar Oct 04 '17 08:10 JasperH8g

Thanks for reporting this @Jpunt , I will try to fix this soon.

jpuri avatar Oct 24 '17 16:10 jpuri

hi @jpuri is there any plan to fix this?

link2malkeet avatar Oct 24 '21 23:10 link2malkeet

Having this issue as well and it's not an issue with this library, but something that is carried from react-draft-wysiwyg. Let me explain (following the same example as OP)

  1. Typed before bold
  2. Typed bold and applied bold style
  3. Disabled bold style and typed no bold Results in: before bold **bold ** no bold.

As it can be seen from editorState (from react-draft-wysiwyg), the length of style is incorrect (should be 4). image

Since this issue is 4 years old, I thought about patching the diff. But the issue is that copy-pasting the text (which leaves it unstyled) and then applying style to the bold word, this is the result:

image

Which is now correct and substracting the lenght would apply bold style to only a substring of the word.

Edit: on top of this, combining styles with emojis will break it even further and emojis will appear as οΏ½ icons (might be related to this

nhevia avatar Jan 26 '22 22:01 nhevia

https://github.com/jpuri/draftjs-to-markdown/pull/43 as a workaround, to make it work directly from this library. It's based in two proposals:

  1. Trims to reduce the incorrect length, if any. Have to be careful before the style characters were applied around whitespaces. One thing to note, this happens for ALL styles, not only bold. Correct:
asd *asd* asd
asd **asd** asd
asd __asd__ asd
asd ~~asd~~ asd
# **asd asd asd**
- **asd**
  1. Delete style is block has any emoji altogether. Unrelated to this particular issue and very opinionated, but better than showing random symbols instead of emojis.

***πŸ˜‡πŸ˜ˆοΏ½***οΏ½πŸ’ͺ😺 -> πŸ˜‡πŸ˜ˆπŸ‘ΈπŸ’ͺ😺

This fix is applied to https://www.npmjs.com/package/@nhevia/draftjs-to-markdown because I need this fix ASAP and they don't look like moving a lot. But if PR is merged I'll go back to this lib.

nhevia avatar Jan 27 '22 02:01 nhevia