Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Open youtube video link #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/qiita/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
require "qiita/markdown/filters/image_link"
require "qiita/markdown/filters/inline_code_color"
require "qiita/markdown/filters/mention"
require "qiita/markdown/filters/open_content"
require "qiita/markdown/filters/simplify"
require "qiita/markdown/filters/syntax_highlight"
require "qiita/markdown/filters/toc"
Expand Down
43 changes: 43 additions & 0 deletions lib/qiita/markdown/filters/open_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Qiita
module Markdown
module Filters
class OpenContent < HTML::Pipeline::Filter
def call
doc.search("a.autolink").each do |node|
next if node["href"].blank?
next unless simple_anchor_tag?(node)

href = node["href"].strip
href_host = host_of(href)
next if href_host.blank?

if href.include?("https://www.youtube.com/watch?v=")
embed_href = href.gsub("www.youtube.com/watch?v=", "www.youtube.com/embed/")
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>")
end
end
doc
end

private

def simple_anchor_tag?(node)
return false if node.children.any? { |child| child.name != "text" }
return false if node.ancestors.any? do |ancestor|
next false if ancestor.name == "#document-fragment"
ancestor.name != "p" || ancestor.children.size > 1
end

true
end

def host_of(url)
uri = ::Addressable::URI.parse(url)
uri.host
rescue ::Addressable::URI::InvalidURIError
nil
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/qiita/markdown/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def self.default_filters
Filters::GroupMention,
Filters::ExternalLink,
Filters::InlineCodeColor,
Filters::OpenContent,
Filters::FinalSanitizer,
]
end
Expand Down
35 changes: 30 additions & 5 deletions spec/qiita/markdown/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@
end
end

shared_examples_for "override embed code attributes" do |allowed:|
shared_examples_for "override embed code attributes" do |allowed:, script:|
context "with HTML embed code for CodePen using old script url" do
let(:markdown) do
<<-MARKDOWN.strip_heredoc
Expand Down Expand Up @@ -1511,6 +1511,31 @@
end
end
end

context "when url is not embed code" do
let(:markdown) do
<<-MARKDOWN.strip_heredoc
#{url}
MARKDOWN
end

let(:url) { "https://www.youtube.com/watch?v=xxxxxx" }

if allowed || script
puts context
it "does not sanitize embed code" do
should eq <<-HTML.strip_heredoc
<p><iframe width="560" height="420" src="https://www.youtube.com/embed/xxxxxx" frameborder="0" allowfullscreen></iframe></p>
HTML
end
else
it "forces width attribute on iframe" do
should eq <<-HTML.strip_heredoc
<p><iframe width="100%" height="420" src="https://www.youtube.com/embed/xxxxxx" frameborder="0" allowfullscreen></iframe></p>
HTML
end
end
end
end

context "with scheme" do
Expand Down Expand Up @@ -1799,7 +1824,7 @@
include_examples "data-attributes", allowed: false
include_examples "class attribute", allowed: true
include_examples "background-color", allowed: true
include_examples "override embed code attributes", allowed: false
include_examples "override embed code attributes", allowed: false, script: false
include_examples "custom block", allowed: false
end

Expand All @@ -1816,7 +1841,7 @@
include_examples "data-attributes", allowed: true
include_examples "class attribute", allowed: true
include_examples "background-color", allowed: true
include_examples "override embed code attributes", allowed: true
include_examples "override embed code attributes", allowed: true, script: true
include_examples "custom block", allowed: true
end

Expand All @@ -1833,7 +1858,7 @@
include_examples "data-attributes", allowed: false
include_examples "class attribute", allowed: false
include_examples "background-color", allowed: false
include_examples "override embed code attributes", allowed: false
include_examples "override embed code attributes", allowed: false, script: false
include_examples "custom block", allowed: false
end

Expand All @@ -1850,7 +1875,7 @@
include_examples "data-attributes", allowed: false
include_examples "class attribute", allowed: false
include_examples "background-color", allowed: false
include_examples "override embed code attributes", allowed: false
include_examples "override embed code attributes", allowed: false, script: true
include_examples "custom block", allowed: true
end
end
Expand Down