Last active
January 4, 2025 19:35
-
-
Save gingerbeardman/26886af3961036068f126b40c2007221 to your computer and use it in GitHub Desktop.
jekyll plugin to enable better youtube embeds with lite-yt-embed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)