Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markdown to plain text in Ruby?

I'm currently using BlueCloth to process Markdown in Ruby and show it as HTML, but in one location I need it as plain text (without some of the Markdown). Is there a way to achieve that?

Is there a markdown-to-plain-text method? Is there an html-to-plain-text method that I could feel the result of BlueCloth?

like image 603
pupeno Avatar asked Mar 30 '10 17:03

pupeno


2 Answers

RedCarpet gem has a Redcarpet::Render::StripDown renderer which "turns Markdown into plaintext".

Copy and modify it to suit your needs.

Or use it like this:

Redcarpet::Markdown.new(Redcarpet::Render::StripDown).render(markdown)
like image 79
lulalala Avatar answered Oct 24 '22 01:10

lulalala


Converting HTML to plain text with Ruby is not a problem, but of course you'll lose all markup. If you only want to get rid of some of the Markdown syntax, it probably won't yield the result you're looking for.

The bottom line is that unrendered Markdown is intended to be used as plain text, therefore converting it to plain text doesn't really make sense. All Ruby implementations that I have seen follow the same interface, which does not offer a way to strip syntax (only including to_html, and text, which returns the original Markdown text).

like image 21
molf Avatar answered Oct 24 '22 02:10

molf