notes: convert internal links to Danbooru to relative urls.
This commit is contained in:
@@ -73,7 +73,20 @@ module NoteSanitizer
|
|||||||
at_rules: [],
|
at_rules: [],
|
||||||
protocols: [],
|
protocols: [],
|
||||||
properties: ALLOWED_PROPERTIES,
|
properties: ALLOWED_PROPERTIES,
|
||||||
}
|
},
|
||||||
|
:transformers => method(:relativize_links),
|
||||||
)
|
)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -20,11 +20,17 @@ module Danbooru
|
|||||||
"Find good anime art fast"
|
"Find good anime art fast"
|
||||||
end
|
end
|
||||||
|
|
||||||
# The hostname of the server.
|
# The canonical hostname of the site.
|
||||||
def hostname
|
def hostname
|
||||||
Socket.gethostname
|
Socket.gethostname
|
||||||
end
|
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.
|
# Contact email address of the admin.
|
||||||
def contact_email
|
def contact_email
|
||||||
"webmaster@#{server_host}"
|
"webmaster@#{server_host}"
|
||||||
|
|||||||
@@ -21,5 +21,12 @@ class NoteSanitizerTest < ActiveSupport::TestCase
|
|||||||
body = '<a href="http://www.google.com">google</a>'
|
body = '<a href="http://www.google.com">google</a>'
|
||||||
assert_equal('<a href="http://www.google.com" rel="nofollow">google</a>', NoteSanitizer.sanitize(body))
|
assert_equal('<a href="http://www.google.com" rel="nofollow">google</a>', NoteSanitizer.sanitize(body))
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user