File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11const YouTubeTransformer = {
22 name : "YouTube" ,
33 shouldTransform ( url ) {
4- return url . includes ( "youtube.com" ) || url . includes ( "youtu.be" ) ;
4+ try {
5+ const parsed = new URL ( url ) ;
6+ const hostname = parsed . hostname . toLowerCase ( ) ;
7+ const allowedHosts = [
8+ "youtube.com" ,
9+ "www.youtube.com" ,
10+ "m.youtube.com" ,
11+ "youtu.be" ,
12+ "www.youtu.be"
13+ ] ;
14+ return allowedHosts . includes ( hostname ) ;
15+ } catch ( e ) {
16+ // If the URL cannot be parsed, it cannot be a valid YouTube URL.
17+ return false ;
18+ }
519 } ,
620 getHTML ( url ) {
7- const videoId = url . includes ( "youtu.be" )
8- ? url . split ( "/" ) . pop ( )
9- : new URL ( url ) . searchParams . get ( "v" ) ;
21+ const urlObj = new URL ( url ) ;
22+ const isShort = urlObj . hostname . toLowerCase ( ) === "youtu.be" || urlObj . hostname . toLowerCase ( ) === "www.youtu.be" ;
23+ const videoId = isShort
24+ ? urlObj . pathname . split ( "/" ) . filter ( Boolean ) . pop ( )
25+ : urlObj . searchParams . get ( "v" ) ;
1026
1127 return `
1228 <iframe
You can’t perform that action at this time.
0 commit comments