Skip to content

Instantly share code, notes, and snippets.

@benbasson
Created April 21, 2014 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbasson/11144340 to your computer and use it in GitHub Desktop.
Save benbasson/11144340 to your computer and use it in GitHub Desktop.
Monkey patch for Kramdown to rewrite relative URLs to absolute URLs
require 'uri'
# Simple Monkey Patch to convert URLs from relative to absolute
# if the option to do so is enabled
class Kramdown::Parser::Kramdown
alias old_add_link add_link
def base_url_as_uri
@base_url_as_uri ||= URI.parse(@options[:base_url])
end
def add_link(el, href, title, alt_text = nil)
if @options[:absolute_urls] and URI.parse(href).relative?
href = base_url_as_uri.merge(href).to_s
end
old_add_link(el, href, title, alt_text)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment