Skip to content

Commit a5e345b

Browse files
committed
fix: videos were auto played wrongly (#53)
1 parent 8ca9f65 commit a5e345b

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/jekyll-spaceship/processors/media-processor.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def handle_normal_audio(element)
4242
handle_media(element, {
4343
media_type: 'audio',
4444
host: '(https?:\\/\\/)?.*\\/',
45-
id: '(.+?\\.(mp3|wav|ogg|mid|midi|aac|wma))',
45+
id: '(.+?\\.(mp3|wav|ogg|mid|midi|aac|wma))'
4646
})
4747
end
4848

@@ -53,7 +53,7 @@ def handle_normal_audio(element)
5353
# ![video](//techslides.com/demos/sample-videos/small.mp4?width=400)
5454
def handle_normal_video(element)
5555
handle_media(element, {
56-
media_type: 'iframe',
56+
media_type: 'video',
5757
host: '(https?:\\/\\/)?.*\\/',
5858
id: '(.+?\\.(avi|mp4|webm|ogg|ogv|flv|mkv|mov|wmv|3gp|rmvb|asf))'
5959
})
@@ -158,6 +158,10 @@ def handle_media(element, data)
158158
cfg['loop'] = qs['loop'] || data[:loop] || cfg['loop']
159159
cfg['style'] += ';display: none;' if qs['hidden']
160160
handle_audio(element, { cfg: cfg })
161+
when 'video'
162+
cfg['autoplay'] = qs['autoplay'] || data[:autoplay] || cfg['autoplay']
163+
cfg['loop'] = qs['loop'] || data[:loop] || cfg['loop']
164+
handle_video(element, { cfg: cfg })
161165
when 'iframe'
162166
cfg['title'] = title
163167
cfg['width'] = qs['width'] || data[:width] || cfg['width']
@@ -188,6 +192,25 @@ def handle_audio(element, data)
188192
element.replace(doc.at('body').children.first)
189193
end
190194

195+
def handle_video(element, data)
196+
cfg = data[:cfg]
197+
html = "<video"\
198+
" id=\"#{cfg['id']}\""\
199+
" class=\"#{cfg['class']}\""\
200+
" style=\"#{cfg['style']}\""\
201+
" #{cfg['autoplay'] ? 'autoplay' : ''}"\
202+
" #{cfg['loop'] ? 'loop' : ''}"\
203+
" controls>" \
204+
" <source src=\"#{cfg['src']}\">" \
205+
" Your browser doesn't support HTML5 video."\
206+
" Here is a <a href=\"#{cfg['src']}\">link to download the video</a>"\
207+
" instead."\
208+
"</video>"
209+
doc = Nokogiri::HTML(html)
210+
return if element.parent.nil?
211+
element.replace(doc.at('body').children.first)
212+
end
213+
191214
def handle_iframe(element, data)
192215
cfg = data[:cfg]
193216
html = "<iframe"\

0 commit comments

Comments
 (0)