diff --git a/plugins/video/lib/video_block.rb b/plugins/video/lib/video_block.rb index 486741e..05cdc69 100644 --- a/plugins/video/lib/video_block.rb +++ b/plugins/video/lib/video_block.rb @@ -4,8 +4,10 @@ class VideoBlock < Block settings_items :width, :type => :integer, :default => 400 settings_items :height, :type => :integer, :default => 315 + YOUTUBE_ID_FORMAT = 'a-zA-Z0-9_-' + def is_youtube? - url.match(/.*(youtube.com.*v=[[:alnum:]]+|youtu.be\/[[:alnum:]]+).*/) ? true : false + url.match(/.*(youtube.com.*v=[#{YOUTUBE_ID_FORMAT}]+|youtu.be\/[#{YOUTUBE_ID_FORMAT}]+).*/) ? true : false end def is_vimeo? @@ -44,8 +46,8 @@ class VideoBlock < Block def extract_youtube_id return nil unless is_youtube? - youtube_match = url.match('v=([[:alnum:]]*)') - youtube_match ||= url.match('youtu.be\/([[:alnum:]]*)') + youtube_match = url.match("v=([#{YOUTUBE_ID_FORMAT}]*)") + youtube_match ||= url.match("youtu.be\/([#{YOUTUBE_ID_FORMAT}]*)") youtube_match[1] unless youtube_match.nil? end diff --git a/plugins/video/test/unit/video_block_test.rb b/plugins/video/test/unit/video_block_test.rb index a7e3587..0ebdae4 100644 --- a/plugins/video/test/unit/video_block_test.rb +++ b/plugins/video/test/unit/video_block_test.rb @@ -70,6 +70,13 @@ class VideoBlockTest < ActiveSupport::TestCase assert_equal id, block.send('extract_youtube_id') end + should "extract youtube id from youtube video url's if it has underline and hyphen" do + block = VideoBlock.new + id = 'oi43_re-d2' + block.url = "youtube.com/?v=#{id}" + assert_equal id, block.send('extract_youtube_id') + end + should "extract youtube id from youtube video url's if it's a valid youtube short url" do block = VideoBlock.new id = 'oi43jre2d2' -- libgit2 0.21.2