Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML to markdown

I have an app and the admin can create article , and i use the markitup markdown editor for add title etc. Now in my view i want convert this markdown text in html.

So in my view if for exemple when admin write the article he write exemple , in the view the text are in bold.

I hope you understand and you can help me.

I install redcarpet and i put in my application helper this :

module ApplicationHelper


 def markdown(text)
if text
  markdown = Redcarpet::Markdown.new(
    Redcarpet::Render::HTML.new
  )
  markdown.render(text).html_safe
end

end

and in my show view this :

 <%= markdown(@article.content) %>

I restarted my server but i have one error :

uninitialized constant ApplicationHelper::Redcarpet EDIT 2 :

THANK's All works !!! !!!!!

like image 758
Florian Dano Clement Avatar asked Jun 05 '13 15:06

Florian Dano Clement


2 Answers

The kramdown gem provides an HTML to Markdown solution in pure Ruby.

irb> html = 'How to convert <b>HTML</b> to <i>Markdown</i> on <a href="http://stackoverflow.com">Stack Overflow</a>.'
=> "How to convert <b>HTML</b> to <i>Markdown</i> on <a href=\"http://stackoverflow.com\">Stack Overflow</a>."
irb> document = Kramdown::Document.new(html, :html_to_native => true)
=> <KD:Document: ... >
irb> document.to_kramdown
=> "How to convert **HTML** to *Markdown* on [Stack Overflow][1].\n\n\n\n[1]: http://stackoverflow.com\n"
like image 119
Rob Avatar answered Sep 28 '22 02:09

Rob


It seems you need this gem

  • reverse_markdown

Transform existing html into markdown in a simple way, for example if you want to import existings tags into your markdown based application.

  • html2markdown

Simple html to Markdown ruby gem We love markdown, cause it is friendly to edit. So we want everything to be markdown

  • upmark

A HTML to Markdown converter.

Upmark defines a parsing expression grammar (PEG) using the very awesome Parslet gem. This PEG is then used to convert HTML into Markdown in 4 steps:

  1. Parse the XHTML into an abstract syntax tree (AST).
  2. Normalize the AST into a nested hash of HTML elements.
  3. Mark the block and span-level subtrees which should be ignored (table, div, span, etc).
  4. Convert the AST leaves into Markdown.

uninitialized constant ApplicationHelper::Redcarpet

Add require 'redcarpet' before module ApplicationHelper

require 'redcarpet'
module ApplicationHelper


  def markdown(text)
    Redcarpet.new(text).html_safe
  end
end
like image 40
rails_id Avatar answered Sep 28 '22 02:09

rails_id