htmlruby-on-railsruby-on-rails-3markdown

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


Solution

  • It seems you need this gem

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

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

    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