-
Notifications
You must be signed in to change notification settings - Fork 30
feat: Google Drive Video ifrmae support #163
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Qiita | ||
module Markdown | ||
module Embed | ||
module GoogleDriveVideo | ||
SCRIPT_HOST = "drive.google.com".freeze | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||||
module Qiita | ||||||||
module Markdown | ||||||||
module Filters | ||||||||
class GoogleDriveVideo < ::HTML::Pipeline::Filter | ||||||||
GDRIVE_VIDEO_PATTERN = /@\[gdrive_video\]\((?<url>https?:\/\/[^\)]+)\)/ | ||||||||
|
||||||||
def call | ||||||||
doc.xpath(".//text()").each do |node| | ||||||||
content = node.to_html | ||||||||
next if !content.match?(GDRIVE_VIDEO_PATTERN) | ||||||||
next if has_ancestor?(node, %w(pre code tt)) | ||||||||
|
||||||||
html = content.gsub(GDRIVE_VIDEO_PATTERN) do | ||||||||
video_url = Regexp.last_match[:url] | ||||||||
embed_url = convert_to_embed_url(video_url) | ||||||||
if embed_url | ||||||||
%(<iframe src="#{embed_url}" width="640" height="480" frameborder="0" allowfullscreen="true"></iframe>) | ||||||||
else | ||||||||
Regexp.last_match[0] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears the gsub block is missing a closing 'end' after the if/else block; please add an extra 'end' to properly close the block.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
end | ||||||||
node.replace(html) | ||||||||
end | ||||||||
doc | ||||||||
end | ||||||||
|
||||||||
private | ||||||||
|
||||||||
def convert_to_embed_url(share_url) | ||||||||
if share_url =~ /drive\.google\.com\/file\/d\/([^\/]+)/ | ||||||||
file_id = $1 | ||||||||
"https://drive.google.com/file/d/#{file_id}/preview" | ||||||||
else | ||||||||
nil | ||||||||
end | ||||||||
end | ||||||||
Comment on lines
+28
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Could we refactor this method to incorporate these points for better robustness and clarity? For example: # private
#
# def convert_to_embed_url(share_url_string)
# begin
# uri = Addressable::URI.parse(share_url_string)
# rescue Addressable::URI::InvalidURIError
# return nil
# end
#
# return nil unless uri.host == Qiita::Markdown::Embed::GoogleDriveVideo::SCRIPT_HOST
#
# # Match /file/d/FILE_ID/...
# # Google Drive file IDs are typically alphanumeric with hyphens and underscores.
# match_data = uri.path.match(/^\/file\/d\/([-\w]+)/)
# return nil unless match_data
#
# file_id = match_data[1]
# "https://#{Qiita::Markdown::Embed::GoogleDriveVideo::SCRIPT_HOST}/file/d/#{file_id}/preview"
# end This approach also centralizes the host check using the defined constant. def convert_to_embed_url(share_url_string)
begin
uri = Addressable::URI.parse(share_url_string)
rescue Addressable::URI::InvalidURIError
return nil
end
return nil unless uri.host == Qiita::Markdown::Embed::GoogleDriveVideo::SCRIPT_HOST
# Google Drive file IDs are typically alphanumeric with hyphens and underscores.
match_data = uri.path.match(/^\/file\/d\/([-\w]+)/)
return nil unless match_data
file_id = match_data[1]
"https://#{Qiita::Markdown::Embed::GoogleDriveVideo::SCRIPT_HOST}/file/d/#{file_id}/preview"
end |
||||||||
end | ||||||||
end | ||||||||
end | ||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The iframe dimensions
width="640" height="480"
are hardcoded here. I see thatFilterIframe
(which runs later) will change thewidth
to"100%"
. However, theheight="480"
will persist.Is this fixed height of 480px the desired default for all embedded Google Drive videos? If videos have different aspect ratios, this could lead to letterboxing or incorrect display.
Just wanted to confirm if this fixed height is intentional and considered appropriate for various video aspect ratios, or if there are plans or thoughts on making the height more adaptive (though that's often more complex, sometimes requiring JavaScript or CSS aspect ratio tricks).