@@ -23,22 +23,35 @@ public function parse(InlineParserContext $inlineContext): bool
2323
2424 // Regex to ensure that we got a valid YouTube url
2525 // and the required `youtube:` prefix exists
26- $ regex = '/^ (?:youtube)\s(?:https?\:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v= |youtu\.be\/)([^&#\s\? ]+)(?:\?.[^\s]+)? / ' ;
26+ $ regex = '/(?:youtube)\s(?:https?\:\/\/)?(?:www\.)?(?:youtube\.com\/watch|youtu\.be\/)([^\s ]+)/ ' ;
2727 $ validate = $ cursor ->match ($ regex );
2828
2929 // The computer says no
3030 if (!$ validate ) {
3131 $ cursor ->restoreState ($ savedState );
32-
3332 return false ;
3433 }
3534
36- $ matches = [];
37- preg_match ($ regex , $ validate , $ matches );
38- $ videoId = $ matches [1 ];
35+ $ videoRegex = '/[^\s]*(?:v\=)([^\s\&]+)[^\s]*/ ' ;
36+ $ timeRegex = '/[^\s]*(?:t\=)([^\s\&]+)s[^\s]*/ ' ;
37+ $ timeContinueRegex = '/[^\s]*(?:time_continue\=)([^\s\&]+)[^\s]*/ ' ;
38+ $ videoMatches = [];
39+ $ timeMatches = [];
40+ $ timeContinueMatches = [];
41+ preg_match ($ videoRegex , $ validate , $ videoMatches );
42+ preg_match ($ timeRegex , $ validate , $ timeMatches );
43+ preg_match ($ timeContinueRegex , $ validate , $ timeContinueMatches );
44+ $ videoId = $ videoMatches [1 ];
45+ $ startTime = '' ;
46+
47+ if ($ timeMatches ) {
48+ $ startTime = "?start= {$ timeMatches [1 ]}" ;
49+ } elseif ($ timeContinueMatches ) {
50+ $ startTime = "?start= {$ timeContinueMatches [1 ]}" ;
51+ }
3952
4053 // Generates a valid YouTube embed url with the parsed video id from the given url
41- $ inlineContext ->getContainer ()->appendChild (new YouTube ("https://www.youtube.com/embed/ $ videoId " ));
54+ $ inlineContext ->getContainer ()->appendChild (new YouTube ("https://www.youtube.com/embed/ $ videoId$ startTime " ));
4255
4356 return true ;
4457 }
0 commit comments