Created
August 8, 2019 00:31
-
-
Save newjam/b246919a6302c0a09d83d2dbbb81dc61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'redcarpet' | |
class CustomRenderHTML < Redcarpet::Render::HTML | |
def initialize(args) | |
@link_prefix = args[:link_prefix] || '' | |
super | |
end | |
def link(link, title, content) | |
link = @link_prefix + link unless link.start_with? 'http' | |
"<a href='#{link}'>#{content}</a>" | |
end | |
end | |
render = CustomRenderHTML.new(link_prefix: 'http://google.com/search?q=') | |
markdown = Redcarpet::Markdown.new(render) | |
puts markdown.render(STDIN.read) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just an example of extending redcarpets html renderer to add a prefix to hrefs in anchor tags.
I wanted to call super on line 11, but that's impossible in redcarpet because of vmg/redcarpet#51