simple formatting/parsing in markdown for blockquotes

1.4k Views Asked by At

I'm using markdown in my site and I would like to do some simple parsing for news articles.

How can I parse markdown to pull all blockquotes and links, so I can highlight them separately from the rest of the document

For example I would like to parse the first blockquote ( >) in the document so I can push it to the top no matter where it occurs in the document. (Similar to what many news sites do, to highlight certain parts of an article.) but then de-blockquote it for the main body. So it occurs twice (once in the highlighted always at the top and then normally as it occurs in the document).

2

There are 2 best solutions below

2
On

I will assume you're trying to do this at render-time, when the markdown is going to be converted to HTML. To point you in the right direction, one way you could go about this would be to

  1. Convert the markdown to HTML
  2. Pass the HTML to Nokogiri
  3. Grab the first <blockquote>, copy it, and inject it into the top of the Nokogiri node tree

The result would be a duplicate of the first <blockquote>.

Redcarpet 2 is a great gem for converting Markdown to HTML. Nokogiri is your best bet for HTML parsing.

I can write sample code if necessary, but the documentation for both gems is thorough and this task is trivial enough to just piece together bits from examples within the docs. This at least answers your question of how to go about doing it.

Edit

Depending on the need, this could be done with a line of jQuery too.

$('article').prepend($($('article blockquote').get(0)).clone())

Given the <article> DOM element for an article on your page, grab the first <blockquote>, clone it, and prepend it to the top of the <article>.

0
On

I know wiki markup (i.e. wikicloth for ruby) has similar implementations as you're after for parsing links, categories, and references. Though I'm not sure about block quotes, but it may be better suited.

Something like:

data = "[[ this ]] is a [[ link ]] and another [http://www.google.com Google].  This is a <ref>reference</ref>, but this is a [[Category:Test]].  This is in another [[de:Sprache]]"

wiki = WikiCloth::Parser.new(:data => data)
wiki.to_html

puts "Internal Links: #{wiki.internal_links.size}"
puts "External Links: #{wiki.external_links.size}"
puts "References:     #{wiki.references.size}"
puts "Categories:     #{wiki.categories.size} [#{wiki.categories.join(",")}]"
puts "Languages:      #{wiki.languages.size} [#{wiki.languages.keys.join(",")}]"

I haven't seen any such parsers available for markdown. Using redcarpet, converting to HTML, then using Nokogiri does seem a bit convoluted.