notes: convert internal links to Danbooru to relative urls.

This commit is contained in:
evazion 2017-06-16 00:36:31 -05:00
parent 48001c1293
commit 8c7c2a06e5
3 changed files with 28 additions and 2 deletions

View File

@ -73,7 +73,20 @@ module NoteSanitizer
at_rules: [],
protocols: [],
properties: ALLOWED_PROPERTIES,
}
},
:transformers => method(:relativize_links),
)
end
def self.relativize_links(node:, **env)
return unless node.name == "a" && node.attribute("href")
href = node.attribute("href")
url = Addressable::URI.parse(href.value).normalize
if url.authority.in?(Danbooru.config.hostnames)
url.site = nil
href.value = url.to_s
end
end
end

View File

@ -20,11 +20,17 @@ module Danbooru
"Find good anime art fast"
end
# The hostname of the server.
# The canonical hostname of the site.
def hostname
Socket.gethostname
end
# The list of all domain names this site is accessible under.
# Example: %w[danbooru.donmai.us sonohara.donmai.us hijiribe.donmai.us safebooru.donmai.us]
def hostnames
[hostname]
end
# Contact email address of the admin.
def contact_email
"webmaster@#{server_host}"

View File

@ -21,5 +21,12 @@ class NoteSanitizerTest < ActiveSupport::TestCase
body = '<a href="http://www.google.com">google</a>'
assert_equal('<a href="http://www.google.com" rel="nofollow">google</a>', NoteSanitizer.sanitize(body))
end
should "rewrite absolute links to relative links" do
Danbooru.config.stubs(:hostnames).returns(%w[danbooru.donmai.us sonohara.donmai.us hijiribe.donmai.us])
body = '<a href="http://sonohara.donmai.us/posts?tags=touhou#dtext-intro">touhou</a>'
assert_equal('<a href="/posts?tags=touhou#dtext-intro" rel="nofollow">touhou</a>', NoteSanitizer.sanitize(body))
end
end
end