tanoku/redcarpet — Issue #79: Feature Request: A NullRenderer that removes Markdown formatting
Author: toupeira
Created: 2011-12-01 11:45:46+00:00
State: open

I wanted to show truncated previews of Markdown-formatted posts in a stream-like overview, and instead of dealing with truncated HTML tags I decided to instead remove all markup, which I also felt would be distracting in this view anyway. I was able to implement this pretty easily with the following code (I left out some tags that I don't use, such as images and tables):

```ruby
class RemoveMarkdownRenderer < Redcarpet::Render::Base
# Methods where the first argument is the text content
[
# block-level calls
:block_code, :block_quote, :block_html, :header, :list, :list_item, :paragraph,
# span-level calls
:autolink, :codespan, :double_emphasis, :emphasis, :raw_html, :triple_emphasis, :strikethrough, :superscript,
# low level rendering
:entity, :normal_text
].each do |method|
define_method method do |*args|
args.first
end
end

# Other methods where the text content is in another argument
def link(link, title, content)
content
end
end
```

My first approach was to render the Markdown and then simply remove all HTML tags with `strip_tags()`, but this turned out to be a lot slower. So I thought it would be nice if such a renderer could be included in Redcarpet itself, optimally implemented in C.