Skip to content
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

Add support for YouTube embeds #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

idleberg
Copy link

@idleberg idleberg commented Nov 22, 2022

This PR adds basic support for YouTube embeds, so videos can be played directly. I could think of supporting other video services (e.g. Vimeo, Dailymotion), but I first wanted to see where this is going.

There are some shortcomings I don't want to leave unmentioned:

  • this does not support YouTube playlist (Twitter itself only has limited support)
  • the video ratio is fixed, but without accessing the YouTube API, there's no way to determine the correct one (16:9 is probably most common)

I also suggest applying aspect-ratio on the iFrame and maybe change the width/height in your CSS.

@nhoizey
Copy link
Contributor

nhoizey commented Nov 23, 2022

I think it would be better for performance to use Paul Irish's lite-youtube-embed component: https://github.com/paulirish/lite-youtube-embed

@nhoizey nhoizey added the enhancement New feature or request label Nov 23, 2022
@idleberg
Copy link
Author

I think it would be better for performance to use Paul Irish's lite-youtube-embed component

Unfortunately, I have no deeper experience with Eleventy and how to configure it to support custom elements. Simply requiring lite-youtube-embed results in this error:

HTMLElement is not defined (via ReferenceError)

@aarongustafson
Copy link
Contributor

aarongustafson commented Dec 6, 2022

@idleberg Can you drop the version you tried with the custom element embed? I set it up in my own Eleventy site and could probably help get it over the finishline.

@idleberg
Copy link
Author

idleberg commented Dec 6, 2022

@aarongustafson These are my uncommitted changes:

diff --git a/package.json b/package.json
index fc114ae..dafa521 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
     "twitter-lite": "^1.1.0"
   },
   "dependencies": {
-    "chartist": "^0.11.4"
+    "chartist": "^0.11.4",
+    "lite-youtube-embed": "^0.2.0"
   }
 }
diff --git a/src/twitter.js b/src/twitter.js
index 7ad5a6c..2875beb 100644
--- a/src/twitter.js
+++ b/src/twitter.js
@@ -159,7 +159,8 @@ class Twitter {
                                                searchParams.delete('v');
                                        }
 
-                                       let displayUrlHtml = `<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/${id}${searchParams.keys().length ? '?' + searchParams.toString() : ''}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen crossorigin="anonymous" />`
+                                       await import('lite-youtube-embed');
+                                       let displayUrlHtml = `<lite-youtube videoid="${id}" />`
                                        text = text.replace(url.url, displayUrlHtml);
                                } else {
                                        let {targetUrl, className, displayUrl} = this.getUrlObject(url);

@bramus bramus mentioned this pull request Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants