API: htmlToMarkdown() should escape special markdown characters in text

I was very surprised that htmlToMarkdown() doesn’t escape special markdown characters in text.

Steps to reproduce

CallhtmlToMarkdown('&gt; _<i>italic</i>_')

Actual result

It produces: '> __italic__' - which doesn’t look italic, it is bold and quoted, obviously.

Expected result

I expected '\> \__italic_\_' markdown, which is rendered as ‘> _italic_’, matching the original HTML sample.

Additional considerations

I guess htmlToMarkdown() should escape special characters, because I can’t come up with any idea how to workaround this:

  • Maybe one can escape underscopes and other special characters in HTML text before calling htmlToMarkdown(). But escaping even things like &gt;… just looks too insane.
  • And there is no sense in escaping after htmlToMarkdown(), because it will destroy all the markup from HTML decorations.

Proposed solution

For backward compatibility, I would suggest adding an optional parameter that enables escaping of special markdowns characters in htmlToMarkdown() .

Like htmlToMarkdown(html: string|…, escapeMarkdown?: boolean);

Did you read and follow the Troubleshooting Guide?

Yes

1 Like