How can I convert HTML to markdown?

ε祈祈猫儿з 提交于 2020-02-23 09:55:25

问题


I have a website like stackoverflow. There is a textarea which people write answers. I use this PHP library to convert markdown. I mean I use that function to convert *italic* to <i>italic</i>.

// include that library
$Parsedown = new Parsedown();
echo $Parsedown->text('*italic*'); # prints: <i>italic</i>

Well All fine. It should be noted I convert that answer (to html tags, not markdown symbols) befor storing, in other word, all data in the database are containing HTML tags, not markdown symbols.

Now I want to implement Editing-System for answers. Something exactly like stackoverflow. So I need to convert saved answer to markdown-style again.

Now I want to know, how can I reconvert it? I mean I want to convert <i>italic</i> to *italic*, How can I do that?


回答1:


Rather than storing the original input as parsed markdown, you should store the actual markdown and convert it to HTML only when rendering it to the page.

This way you retain the original markdown for editing as required. It is also more logical.




回答2:


I needed this too, I found these 2 github projects:

  • https://github.com/Elephant418/Markdownify
  • https://github.com/thephpleague/html-to-markdown

I used the first one, because it has support for Markdown Extra (converted with ParsedownExtra), and it worked well for me.



来源:https://stackoverflow.com/questions/36049936/how-can-i-convert-html-to-markdown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!