Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markdown to Html

Tags:

html

markdown

I want to allow users to edit articles with markdown, and then convert it to HTML for display. I use pagedown to do that. But I want users to be able to edit their articles later, which means sending them the Markdown code after it was converted to HTML, and I don't know what the best way to do that. I had these ideas in mind:

  • Use toMarkdown to convert HTML to markdown, but it doesn't work very well
  • Store Markdown code only, which mean that the db might contain harmful code and need the user to sanitize the content, that's fine while it is used on my website only, but what if someone wants to access to it and forgets to sanitize the content in his application
  • Store Markdown & HTML, the markdown is converted and the HTML is sanitized on server side, and we send the markdown code when the user wants to edit an article, and the HTML when he wants to view it. The problem is that the db will have to store much more data
  • A better idea ?

A good example of website that do that is stackoverflow, but I have no idea about the method they are using for this.

like image 686
Carl Levasseur Avatar asked Nov 21 '25 03:11

Carl Levasseur


2 Answers

Personally, I'd store the markdown only, and cache the generated HTML with something like memcached to speed up rendering on page load.

Generate the HTML from the markdown on page load -> cache it -> then hit the cache for a while after. When you edit it either; invalidate the cache, or re-warm the cache to save the user doing it for you.

like image 181
Seer Avatar answered Nov 24 '25 19:11

Seer


The problem is that the db will have to store much more data

How much content do you have? Most applications dealing with human generated content you don't have so many Megabytes of content. Unless you want to rebuild en.wikipedia with about 20 GB;)

And either way you need to store the markdown is useful. Beside storing Markdown & HTML with memcached, look at some static site generators, they all store both by definition.

like image 23
klml Avatar answered Nov 24 '25 20:11

klml



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!