New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: A renderer that removes Markdown formatting #79

Closed
toupeira opened this Issue Dec 1, 2011 · 2 comments

Comments

Projects
None yet
2 participants
@toupeira

toupeira commented Dec 1, 2011

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):

  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 quite a lot slower. So I thought it would be nice if such a renderer could be included in Redcarpet itself, optimally implemented in C.

@vmg

This comment has been minimized.

Show comment
Hide comment
@vmg

vmg Dec 3, 2011

Owner

Hey, this is a very interesting feature request. Unfortunately, writing the whole renderer in C implies a huuuuge chunk of boilerplate. I'm going to stick your Ruby version in lib -- if somebody wants to go ahead and rewrite it in C, that'd be great.

Owner

vmg commented Dec 3, 2011

Hey, this is a very interesting feature request. Unfortunately, writing the whole renderer in C implies a huuuuge chunk of boilerplate. I'm going to stick your Ruby version in lib -- if somebody wants to go ahead and rewrite it in C, that'd be great.

@toupeira

This comment has been minimized.

Show comment
Hide comment
@toupeira

toupeira Dec 3, 2011

Fair enough, thanks for adding it!

toupeira commented Dec 3, 2011

Fair enough, thanks for adding it!

@vmg vmg closed this Dec 3, 2011

tjg pushed a commit to tjg/redcarpet that referenced this issue Oct 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment