Convert HTML to Markdown. Sometimes you just need to go the other way.
Convert HTML to Markdown with Ruby.
There are several libraries that solve this, but Unmarkdown is simple. It's only 150 lines and handles everything I threw at it with no problem. The other libraries I tried either didn't do recursion correctly or were missing some Markdown extensions I needed. Both were hard to change, so I just whipped this up in a few hours.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install unmarkdown
markdown = Unmarkdown.parse('Some HTML') #=> Some **HTML**
For tags that aren't supported, their content will be added to the output. Basically it treats everything like a
All of the options default to
false. If you'd like to turn additional things on, pass a hash with symbols as the second argument to
Unmarkdown.parse(see example above).
fenced_code_blocks— Uses three backticks before and after instead of four spaces before each line
allow_scripts— By default, script tags are removed. If you set this option to
truetheir original HTML will be included in the output
underline_headers— By default hashes are added before headers. If you turn this option on, it will use equal signs for h1's or hypens for h2's and the rest will remain hashes.
git checkout -b my-new-feature)
git commit -am 'Add some feature')
git push origin my-new-feature)