What is one of the best gems out there to implement markdown in user posts?
I am currently looking to allow users to edit their text via profiles, discussions, etc. and the simple_format solution is no longer suitable for me.
RedCarpet is the Ruby community's usual go-to choice for implementing Markdown, though there are other options (Kramdown, for example).
To render your users' posts as Markdown with RedCarpet, you would write:
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true)
markdown.render(#post.content)
With Kramdown, the API is a bit simpler:
Kramdown::Document.new(#post.content).to_html
RedCloth does support a wealth of configuration options, though, which Kramdown does not.
redcarpet + markitup = example
Related
I have a Rails 3.2.13 site that needs to scrape another website to get a product description. What is the best way to do this in Rails 3?
I've heard that nokogiri is fast. Should I use nokogiri? And if I use nokogiri is it possible that I don't have to save the scraped data anymore? I imagine it as just like getting json data from an API, is it like that?
I'd recommend a combination of Nokogiri and open-uri. Require both gems, and then just do something along the lines of doc = Nokogiri::HTML(open(YOUR_URL)). Then find the element you want to capture (using developer tools in chrome (or the equivalent) or something like Selector Gadget. Then you can use doc.at_css(SELECTOR) for a single element, or doc.search(SELECTOR) for multiple selectors. Calling the text method the response should get you the product description you're looking for. No need to save anything to the database (unless you want to) Hope that helps!
mechanize is a wonderful gem for scraping data from other websites as html. It is simple, robust and using nokogiri gem as result wrapper.
the following snippet will show you how you can fetch needed data being seen as Safari browser from url:
require 'htmlentities'
require "mechanize"
a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
#resultHash = {}
a.get(url) do |page|
parsedPage = page.parser
#resultHash[:some_data_name] = parsedPage.at_xpath("//h1[#class='any-class']").text.split(/\s+/).join(" ")
end
Wondering if anyone has a good solution for this. My app is displaying nothing when embedding both the old and new version of YouTube's video embed code. I'm using GitHub's Markdown Gem Redcarpet, and it doesn't appear that there is any information in their 'issues' section that would help solve this problem. There is a similar question on Stack Overflow but it deals with a different issue.
Has anyone figured out how to embed video using the Redcarpet gem for Markdown in Rails 3.2?
Make sure the :filter_html flag is disabled in your renderer.
Redcarpet::Render::HTML.new(:filter_html => false)
EDIT:
If you want to let only certain html tags through, you have to create a custom Renderer (here's how) and define the block_html method.
For example:
class MyRenderer < Redcarpet::Render::HTML
def block_html(raw_html)
if raw_html =~ /^<iframe.*>$/ # You may want to optimize this.
raw_html
end
end
end
Then set :filter_html back to true when you call your own renderer:
MyRenderer.new(:filter_html => true)
I cant get markdown to work with Slim (Rails 3.1 app), I get this error:
Unknown line indicator
:markdown
I have the following gems in my Gemfile:
gem 'bluecloth'
gem 'rdiscount'
gem 'slim'
This is how my template looks like:
:markdown
#hello
Yes. Slim supports Markdown. You need to put markdown: before you use markdown code in your templates.
Example:
markdown:
#Header
Hello from #{"Markdown!"}
Second Line!
You might be able to use Markdown syntax with a single gem: redcarpet. Nothing against the other gems, but after a couple of years dealing with Rails I really believe less is more, especially when it comes to adding gems to your projects, it can get out of hand very quickly.
This page lists the requirements for Markdown support in Slim:
slim-template
My rails app is using RDiscount to generate HTML from user-supplied markdown text, and I noticed that the anchor tags don't have rel="nofollow". This is a big issue for me as my app is open to the public. Is there a way to enable nofollow links, or are there better solutions?
Thanks!
I think this is possible only with Kramdown, which is a ruby Markdown parser with extended syntax. You would do that then as shown in the link:
[link](test.html){:rel='nofollow'}
In the mean time, I am using this hack, by re-parsing the RDiscount output and add a rel="nofollow" to each anchor:
def markdown(input)
html = RDiscount.new(input).to_html
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css("a").each do |link|
link['rel'] = 'nofollow'
end
doc.to_html
end
Though I think this should really be handled by the markdown parser.
I needed to do something similar, add target="_new" to all links. Solved it using Kramdown and a custom Kramdown::Converter::Html class.
Define a Kramdown::Converter::Html subclass (kramdown/converter/my_html.rb in some autoload path)
class Kramdown::Converter::MyHtml < Kramdown::Converter::Html
def convert_a(el, indent)
el.attr['target'] = '_new'
super
end
end
I also have a view helper in app/helpers/application_helper.rb
def markdown(str)
Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe
end
Ideally it should be possible to just use Kramdown::Document.new(str).to_my_html.html_safe but I can't get it to work in rails development mode as Kramdown uses const_defined? to see if a converter is available and that does not trigger the autoloader. Please comment if you know how to fix this.
There is an open feature request on RDiscount to support modifying links in this fashion.
It is scheduled for one of the upcoming RDiscount 2.1.5.x releases.
So I'm putting together a simple forum. I'd like to allow my users limited formatting options and BBCode would be plenty for my users. Knowing that I'm assuredly not the first one to want to use BBCode with RoR I googled but couldn't find a straight forward tutorial on how to create a editor which accepts BBCode nor a way to parse and display BBCode formatted input.
Any help or guides would be appreciated!
You should give bb-ruby a try. Its documentation on the web page seems to be very clear and straightforward.
Here is another gem you may find useful
http://github.com/jarrett/rbbcode
Gemfile
gem 'bb-ruby'
# run `bundle`
In the place (haml):
%h1= put_header_string.bbcode_to_html.html_safe
%p= "[b]bold text[/b]".bbcode_to_html.html_safe
Besides a builtins you could also extend your own bbcode as you need. For example:
module BBRuby
##tags = ##tags.merge({
'Email' => [
/\[email(:.*)?\](.*?)\[\/file\1?\]/mi,
lambda{ |e| "<span class='email'>#{e[2].gsub('#','<i>(at)</i>')}</span>"},
'protect email from spam',
'[email]electronic#test.ru[/email]',
:email
],
})
end
In place
[b]Contact me:[/b][email]email#test.ru[/email]
Contact me: email(at)test.ru
bb-ruby on github | bb-ruby on rubygems | bb-ruby home | tags processed list