Reputation: 257
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
Upvotes: 11
Views: 9538
Reputation: 8220
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:
- Parse the XHTML into an abstract syntax tree (AST).
- Normalize the AST into a nested hash of HTML elements.
- Mark the block and span-level subtrees which should be ignored (table, div, span, etc).
- 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
Upvotes: 18
Reputation: 933
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"
Upvotes: 18
Reputation: 4173
You can use the redcarpet gem to compile markdown into html in rails.
With redcarpet you can than do the following:
# application_helper.rb
module ApplicationHelper
def markdown(text)
if text
markdown = Redcarpet::Markdown.new(
Redcarpet::Render::HTML.new
)
markdown.render(text).html_safe
end
end
end
# some_view.html.erb
<%= markdown(@article.body) %>
Upvotes: 4