开发者

Markdown, enable processing of markdown within block level html

From Daring Fireball's Markdown doc

Note that Markdown format开发者_如何学Goting syntax is not processed within block-level HTML tags. E.g., you can’t use Markdown-style emphasis inside an HTML block.

I want to wrap some markdown in div tags and still get it to process that markdown. Is there a way to do this with a switch or something?

e.g

<div>

* * *

The asterisks would still become an <hr/>

</div>

I'm using RDiscount as the markdown filter. Any help is much appreciated.


Maruku supports Markdown inside HTML blocks.


I gave Halst the points for the answer as it's correct, but I encountered several problems with using Maruku in this way, such as it stripping out unicode and reformatting existent html that I didn't want it to touch and caused problems. So, I had to sort it out myself and this is what I came up with:

# encoding: UTF-8
module MarkdownFilters

  # This finds html tags with "markdown='1'" as an attribute, runs markdown over the contents, then removes the markdown attribute
  class InsideBlock
    require 'hpricot'


    def self.run( content, markdown_parser=nil )    
      if markdown_parser.nil?
        require 'rdiscount' 
        markdown_parser=RDiscount
      end
      doc = Hpricot(content) 

      (doc/"*[@markdown='1']").each do |ele|  
        ele.inner_html = markdown_parser.new(ele.inner_html).to_html
        ele.remove_attribute("markdown")
      end

      doc.to_s
    end # run

  end # class
end # module

I stole the idea of marking the html with a "markdown='1'" attribute from Maruku, so it can be interchanged with it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜