Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active January 4, 2025 19:35
Show Gist options
  • Select an option

  • Save gingerbeardman/26886af3961036068f126b40c2007221 to your computer and use it in GitHub Desktop.

Select an option

Save gingerbeardman/26886af3961036068f126b40c2007221 to your computer and use it in GitHub Desktop.
jekyll plugin to enable better youtube embeds with lite-yt-embed
module Jekyll
class YouTubeEmbed < Liquid::Tag
def initialize(tag_name, input, tokens)
super
@input = input.strip
end
def render(context)
parts = @input.split(' ')
# Initialize default values
id = parts[0]
aspect_ratio = '16/9'
start_time = '0'
# Process remaining arguments
parts[1..-1].each do |part|
if part.start_with?('@')
# This is a timestamp
start_time = part.gsub('@', '')
elsif part.match?(%r{\d+/\d+})
# This is an aspect ratio
aspect_ratio = part
end
end
<<~HTML
<lite-youtube
style="aspect-ratio: #{aspect_ratio};"
videoid="#{id}"
params="start=#{start_time}&modestbranding=2">
</lite-youtube>
HTML
end
end
end
Liquid::Template.register_tag('youtube', Jekyll::YouTubeEmbed)
@gingerbeardman

gingerbeardman commented Sep 11, 2024

Copy link
Copy Markdown
Author

Use like this:

  • {% youtube 8_w8elG3w0Y %} (defaults: 16/9, start at zero)
  • {% youtube 8_w8elG3w0Y @503 %} (start at time)
  • {% youtube 8_w8elG3w0Y 4/3 %} (override aspect ratio)
  • {% youtube 8_w8elG3w0Y 4/3 @503 %} (both)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment