-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
opengraph.rb
46 lines (37 loc) · 1.22 KB
/
opengraph.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true
class OpenGraphElement < ::HTMLProofer::Element
attr_reader :src
def initialize(obj, check)
super(obj, check)
# Fake up src from the content attribute
instance_variable_set('@src', @content)
@src.insert 0, 'http:' if @src =~ %r{^//}
end
end
class OpenGraphCheck < ::HTMLProofer::Check
def missing_src?
!@opengraph.src
end
def empty_src?
blank?(@opengraph.src)
end
def run
@html.css('meta[property="og:url"], meta[property="og:image"]').each do |m|
@opengraph = OpenGraphElement.new(m, self)
next if @opengraph.ignore?
# does the opengraph exist?
if missing_src?
add_issue('open graph has no content attribute', line: m.line, content: m.content)
elsif empty_src?
add_issue('open graph content attribute is empty', line: m.line, content: m.content)
elsif !@opengraph.valid?
add_issue("#{@opengraph.src} is an invalid URL", line: m.line)
elsif @opengraph.remote?
add_to_external_urls(@opengraph.url)
else
add_issue("internal open graph #{@opengraph.url} does not exist", line: m.line, content: m.content) unless @opengraph.exists?
end
end
external_urls
end
end