I'm trying to use redcarpet to make a custom renderer that transforms [[id]] blocks into links to other pages in my app.
Documentation of custom renderers is rather concise, so I'm not sure how to use this.
here's what I got so far (which doesn't work)
class LinksRenderer < Redcarpet::Render::HTML
def link(link, title, content)
# Look for text enclosed in double square brackets and wrap it in an <a> tag
if content =~ /\[\[(.*?)\]\]/
text = $1
"<a href='#{link}' title='#{title}'>#{text}</a>"
else
# Use the default link rendering behavior for other links
super
end
end
end
which I call via a helper:
def md(text)
markdown = Redcarpet::Markdown.new(LinksRenderer.new)
markdown.render(text).html_safe
end
any ideas?
The
linkmethod will only be called when a link (atag) is found.To accomplish what you want, you would have to use the
preprocess(full_document)orpostprocess(full_document)described here. These methods will provide the entire content and you'll be able to replace the tags.