1- const { URL } = require ( 'url' ) ;
1+ import { URL } from 'url' ;
22
3- function shouldTransform ( string ) {
3+ export const shouldTransform = string => {
44 const { host } = new URL ( string ) ;
5+
56 return host . endsWith ( 'youtube.com' ) || host . endsWith ( 'youtu.be' ) ;
6- }
7+ } ;
78
8- function getYouTubeHTML ( string ) {
9- const iframeSrc = getYouTubeIFrameSrc ( string ) ;
10- return `<iframe width="100%" height="315" src=" ${ iframeSrc } " frameBorder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe>` ;
11- }
9+ export const getTimeValueInSeconds = timeValue => {
10+ if ( Number ( timeValue ) . toString ( ) === timeValue ) {
11+ return timeValue ;
12+ }
1213
13- function getYouTubeIFrameSrc ( string ) {
14+ const {
15+ 2 : hours = '0' ,
16+ 4 : minutes = '0' ,
17+ 6 : seconds = '0' ,
18+ } = timeValue . match ( / ( ( \d * ) h ) ? ( ( \d * ) m ) ? ( ( \d * ) s ) ? / ) ;
19+
20+ return String ( ( Number ( hours ) * 60 + Number ( minutes ) ) * 60 + Number ( seconds ) ) ;
21+ } ;
22+ export const getYouTubeIFrameSrc = string => {
1423 const url = new URL ( string ) ;
1524 let id = url . searchParams . get ( 'v' ) ;
1625 if ( url . host === 'youtu.be' ) {
@@ -23,28 +32,20 @@ function getYouTubeIFrameSrc(string) {
2332 if ( name === 'v' ) {
2433 return ;
2534 }
35+
2636 if ( name === 't' ) {
27- name = 'start' ;
28- value = getTimeValueInSeconds ( value ) ;
37+ embedUrl . searchParams . append ( 'start' , getTimeValueInSeconds ( value ) ) ;
38+ } else {
39+ embedUrl . searchParams . append ( name , value ) ;
2940 }
30- embedUrl . searchParams . append ( name , value ) ;
3141 } ) ;
42+
3243 return embedUrl . toString ( ) ;
33- }
44+ } ;
45+ const getYouTubeHTML = string => {
46+ const iframeSrc = getYouTubeIFrameSrc ( string ) ;
3447
35- function getTimeValueInSeconds ( timeValue ) {
36- if ( Number ( timeValue ) . toString ( ) === timeValue ) {
37- return timeValue ;
38- }
39- const {
40- 2 : hours = '0' ,
41- 4 : minutes = '0' ,
42- 6 : seconds = '0' ,
43- } = timeValue . match ( / ( ( \d * ) h ) ? ( ( \d * ) m ) ? ( ( \d * ) s ) ? / ) ;
44- return String ( ( Number ( hours ) * 60 + Number ( minutes ) ) * 60 + Number ( seconds ) ) ;
45- }
48+ return `<iframe width="100%" height="315" src="${ iframeSrc } " frameBorder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe>` ;
49+ } ;
4650
47- module . exports = getYouTubeHTML ;
48- module . exports . shouldTransform = shouldTransform ;
49- module . exports . getYouTubeIFrameSrc = getYouTubeIFrameSrc ;
50- module . exports . getTimeValueInSeconds = getTimeValueInSeconds ;
51+ export default getYouTubeHTML ;
0 commit comments