Skip to content

Commit 8c0027f

Browse files
committed
Open youtube video link
1 parent 1a41bf9 commit 8c0027f

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

lib/qiita/markdown.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
require "qiita/markdown/filters/image_link"
3131
require "qiita/markdown/filters/inline_code_color"
3232
require "qiita/markdown/filters/mention"
33+
require "qiita/markdown/filters/open_content"
3334
require "qiita/markdown/filters/simplify"
3435
require "qiita/markdown/filters/syntax_highlight"
3536
require "qiita/markdown/filters/toc"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module Qiita
2+
module Markdown
3+
module Filters
4+
class OpenContent < HTML::Pipeline::Filter
5+
def call
6+
doc.search("a.autolink").each do |node|
7+
next if node["href"].blank?
8+
next unless simple_anchor_tag?(node)
9+
10+
href = node["href"].strip
11+
href_host = host_of(href)
12+
next if href_host.blank?
13+
14+
if href.include?('https://www.youtube.com/watch?v=')
15+
embed_href = href.gsub('www.youtube.com/watch?v=', 'www.youtube.com/embed/')
16+
node.replace("<iframe width=\"560\" height=\"420\" src=\"#{embed_href}\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>")
17+
end
18+
end
19+
doc
20+
end
21+
22+
private
23+
24+
def simple_anchor_tag?(node)
25+
return false if node.children.any? { |child| child.name != 'text' }
26+
return false if node.ancestors.any? do |ancestor|
27+
next false if ancestor.name == '#document-fragment'
28+
ancestor.name != 'p' || ancestor.children.size > 1
29+
end
30+
31+
true
32+
end
33+
34+
def host_of(url)
35+
uri = ::Addressable::URI.parse(url)
36+
uri.host
37+
rescue ::Addressable::URI::InvalidURIError
38+
nil
39+
end
40+
end
41+
end
42+
end
43+
end

lib/qiita/markdown/processor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def self.default_filters
2323
Filters::GroupMention,
2424
Filters::ExternalLink,
2525
Filters::InlineCodeColor,
26+
Filters::OpenContent,
2627
Filters::FinalSanitizer,
2728
]
2829
end

spec/qiita/markdown/processor_spec.rb

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@
13931393
end
13941394
end
13951395

1396-
shared_examples_for "override embed code attributes" do |allowed:|
1396+
shared_examples_for "override embed code attributes" do |allowed:, script:|
13971397
context "with HTML embed code for CodePen using old script url" do
13981398
let(:markdown) do
13991399
<<-MARKDOWN.strip_heredoc
@@ -1511,6 +1511,31 @@
15111511
end
15121512
end
15131513
end
1514+
1515+
context "when url is not embed code" do
1516+
let(:markdown) do
1517+
<<-MARKDOWN.strip_heredoc
1518+
#{url}
1519+
MARKDOWN
1520+
end
1521+
1522+
let(:url) {'https://www.youtube.com/watch?v=xxxxxx'}
1523+
1524+
if allowed || script
1525+
puts context
1526+
it "does not sanitize embed code" do
1527+
should eq <<-HTML.strip_heredoc
1528+
<p><iframe width="560" height="420" src="https://www.youtube.com/embed/xxxxxx" frameborder="0" allowfullscreen></iframe></p>
1529+
HTML
1530+
end
1531+
else
1532+
it "forces width attribute on iframe" do
1533+
should eq <<-HTML.strip_heredoc
1534+
<p><iframe width="100%" height="420" src="https://www.youtube.com/embed/xxxxxx" frameborder="0" allowfullscreen></iframe></p>
1535+
HTML
1536+
end
1537+
end
1538+
end
15141539
end
15151540

15161541
context "with scheme" do
@@ -1799,7 +1824,7 @@
17991824
include_examples "data-attributes", allowed: false
18001825
include_examples "class attribute", allowed: true
18011826
include_examples "background-color", allowed: true
1802-
include_examples "override embed code attributes", allowed: false
1827+
include_examples "override embed code attributes", allowed: false, script: false
18031828
include_examples "custom block", allowed: false
18041829
end
18051830

@@ -1816,7 +1841,7 @@
18161841
include_examples "data-attributes", allowed: true
18171842
include_examples "class attribute", allowed: true
18181843
include_examples "background-color", allowed: true
1819-
include_examples "override embed code attributes", allowed: true
1844+
include_examples "override embed code attributes", allowed: true, script: true
18201845
include_examples "custom block", allowed: true
18211846
end
18221847

@@ -1833,7 +1858,7 @@
18331858
include_examples "data-attributes", allowed: false
18341859
include_examples "class attribute", allowed: false
18351860
include_examples "background-color", allowed: false
1836-
include_examples "override embed code attributes", allowed: false
1861+
include_examples "override embed code attributes", allowed: false, script: false
18371862
include_examples "custom block", allowed: false
18381863
end
18391864

@@ -1850,7 +1875,7 @@
18501875
include_examples "data-attributes", allowed: false
18511876
include_examples "class attribute", allowed: false
18521877
include_examples "background-color", allowed: false
1853-
include_examples "override embed code attributes", allowed: false
1878+
include_examples "override embed code attributes", allowed: false, script: true
18541879
include_examples "custom block", allowed: true
18551880
end
18561881
end

0 commit comments

Comments
 (0)