Skip to content

Commit c6391ec

Browse files
committed
feat: Google Drive Video ifrmae support
1 parent e0480a3 commit c6391ec

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

lib/qiita/markdown.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require "qiita/markdown/embed/tweet"
1313
require "qiita/markdown/embed/asciinema"
1414
require "qiita/markdown/embed/youtube"
15+
require "qiita/markdown/embed/google_drive_video"
1516
require "qiita/markdown/embed/slide_share"
1617
require "qiita/markdown/embed/google_slide"
1718
require "qiita/markdown/embed/speeker_deck"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Qiita
2+
module Markdown
3+
module Embed
4+
module GoogleDriveVideo
5+
SCRIPT_HOST = "drive.google.com".freeze
6+
end
7+
end
8+
end
9+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Qiita
2+
module Markdown
3+
module Filters
4+
class GoogleDriveVideo < ::HTML::Pipeline::Filter
5+
GDRIVE_VIDEO_PATTERN = /@\[gdrive_video\]\((?<url>https?:\/\/[^\)]+)\)/
6+
7+
def call
8+
doc.xpath(".//text()").each do |node|
9+
content = node.to_html
10+
next if !content.match?(GDRIVE_VIDEO_PATTERN)
11+
next if has_ancestor?(node, %w(pre code tt))
12+
13+
html = content.gsub(GDRIVE_VIDEO_PATTERN) do
14+
video_url = Regexp.last_match[:url]
15+
embed_url = convert_to_embed_url(video_url)
16+
if embed_url
17+
%(<iframe src="#{embed_url}" width="640" height="480" frameborder="0" allowfullscreen="true"></iframe>)
18+
else
19+
Regexp.last_match[0]
20+
end
21+
node.replace(html)
22+
end
23+
doc
24+
end
25+
26+
private
27+
28+
def convert_to_embed_url(share_url)
29+
if share_url =~ /drive\.google\.com\/file\/d\/([^\/]+)/
30+
file_id = $1
31+
"https://drive.google.com/file/d/#{file_id}/preview"
32+
else
33+
nil
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end

lib/qiita/markdown/transformers/filter_iframe.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class FilterIframe
1010
Embed::GoogleSlide::SCRIPT_HOST,
1111
Embed::Docswell::SCRIPT_HOSTS,
1212
Embed::Figma::SCRIPT_HOST,
13+
Embed::GoogleDriveVideo::SCRIPT_HOST,
1314
].flatten.freeze
1415

1516
def self.call(**args)

spec/qiita/markdown/processor_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,42 @@
15201520
end
15211521
end
15221522

1523+
context "with HTML embed code for Google Drive Video" do
1524+
shared_examples "embed code googledrive example" do
1525+
let(:markdown) do
1526+
<<-MARKDOWN.strip_heredoc
1527+
<iframe src="#{url}" width="640" height="480" frameborder="0" allowfullscreen="true"></iframe>
1528+
MARKDOWN
1529+
end
1530+
let(:file_id) { "DRIVE_FILE_ID_EXAMPLE" }
1531+
let(:url) { "#{scheme}//drive.google.com/file/d/#{file_id}/preview" }
1532+
1533+
if allowed
1534+
it "does not sanitize embed code" do
1535+
should eq <<-HTML.strip_heredoc
1536+
<iframe src="#{url}" width="640" height="480" frameborder="0" allowfullscreen="true"></iframe>
1537+
HTML
1538+
end
1539+
else
1540+
it "forces width attribute on iframe" do
1541+
should eq <<-HTML.strip_heredoc
1542+
<iframe src="#{url}" width="100%" height="480" frameborder="0" allowfullscreen="true"></iframe>
1543+
HTML
1544+
end
1545+
end
1546+
end
1547+
1548+
context "with scheme" do
1549+
let(:scheme) { "https:" }
1550+
include_examples "embed code googledrive example"
1551+
end
1552+
1553+
context "without scheme" do
1554+
let(:scheme) { "" }
1555+
include_examples "embed code googledrive example"
1556+
end
1557+
end
1558+
15231559
context "with HTML embed code for SlideShare" do
15241560
shared_examples "embed code slideshare example" do
15251561
let(:markdown) do

0 commit comments

Comments
 (0)