mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-06-11 17:56:26 +08:00
Without this onebox ignored URLs like this one: http://www.openstreetmap.org/#map=12/48.8557/2.3186
29 lines
718 B
Ruby
Vendored
29 lines
718 B
Ruby
Vendored
module Onebox
|
|
class Matcher
|
|
def initialize(link)
|
|
@url = link
|
|
end
|
|
|
|
def ordered_engines
|
|
@ordered_engines ||= Engine.engines.sort_by do |e|
|
|
e.respond_to?(:priority) ? e.priority : 100
|
|
end
|
|
end
|
|
|
|
def oneboxed
|
|
uri = URI(@url)
|
|
|
|
# A onebox needs a path, query or fragment string to be considered
|
|
return if (uri.query.nil? || uri.query.size == 0) &&
|
|
(uri.fragment.nil? || uri.fragment.size == 0) &&
|
|
(uri.path.size == 0 || uri.path == "/")
|
|
|
|
ordered_engines.select do |engine|
|
|
engine === uri
|
|
end.first
|
|
rescue URI::InvalidURIError
|
|
# If it's not a valid URL, don't even match
|
|
nil
|
|
end
|
|
end
|
|
end
|