Skip to content
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

Add Options#base_url to allow resolving relative links #26

Merged
merged 2 commits into from Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions spec/api_spec.cr
@@ -0,0 +1,21 @@
require "spec"
require "../src/markd"

describe Markd::Options do
describe "#base_url" do
it "it disabled by default" do
options = Markd::Options.new
Markd.to_html("[foo](bar)", options).should eq %(<p><a href="bar">foo</a></p>\n)
Markd.to_html("![](bar)", options).should eq %(<p><img src="bar" alt="" /></p>\n)
end

it "absolutizes relative urls" do
options = Markd::Options.new
options.base_url = URI.parse("http://example.com")
Markd.to_html("[foo](bar)", options).should eq %(<p><a href="http://example.com/bar">foo</a></p>\n)
Markd.to_html("[foo](https://example.com/baz)", options).should eq %(<p><a href="https://example.com/baz">foo</a></p>\n)
Markd.to_html("![](bar)", options).should eq %(<p><img src="http://example.com/bar" alt="" /></p>\n)
Markd.to_html("![](https://example.com/baz)", options).should eq %(<p><img src="https://example.com/baz" alt="" /></p>\n)
end
end
end
10 changes: 9 additions & 1 deletion src/markd/options.cr
@@ -1,15 +1,23 @@
require "uri"

module Markd
struct Options
property time, gfm, toc, smart, source_pos, safe, prettyprint

# If `base_url` is not `nil`, it is used to resolve URLs of relative
# links. It act's like HTML's `<base href="base_url">` in the context
# of a Markdown document.
property base_url : URI?

def initialize(
@time = false,
@gfm = false,
@toc = false,
@smart = false,
@source_pos = false,
@safe = false,
@prettyprint = false
@prettyprint = false,
@base_url = nil
)
end
end
Expand Down
23 changes: 19 additions & 4 deletions src/markd/renderers/html_renderer.cr
Expand Up @@ -95,9 +95,12 @@ module Markd
def link(node : Node, entering : Bool)
if entering
attrs = attrs(node)
if !(@options.safe && potentially_unsafe(node.data["destination"].as(String)))
destination = node.data["destination"].as(String)

unless @options.safe && potentially_unsafe(destination)
attrs ||= {} of String => String
attrs["href"] = escape(node.data["destination"].as(String))
destination = resolve_uri(destination)
attrs["href"] = escape(destination)
end

if (title = node.data["title"].as(String)) && !title.empty?
Expand All @@ -111,13 +114,25 @@ module Markd
end
end

private def resolve_uri(destination)
base_url = @options.base_url
return destination unless base_url

uri = URI.parse(destination)
return destination if uri.absolute?

base_url.resolve(uri).to_s
end

def image(node : Node, entering : Bool)
if entering
if @disable_tag == 0
if @options.safe && potentially_unsafe(node.data["destination"].as(String))
destination = node.data["destination"].as(String)
if @options.safe && potentially_unsafe(destination)
lit(%(<img src="" alt=""))
else
lit(%(<img src="#{escape(node.data["destination"].as(String))}" alt="))
destination = resolve_uri(destination)
lit(%(<img src="#{escape(destination)}" alt="))
end
end
@disable_tag += 1
Expand Down