Skip to content

Commit

Permalink
Added video and time features to YouTube
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyhuy committed Dec 28, 2019
1 parent a8666d0 commit 2b741cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/Inline/Parser/YouTubeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,35 @@ public function parse(InlineParserContext $inlineContext): bool

// Regex to ensure that we got a valid YouTube url
// and the required `youtube:` prefix exists
$regex = '/^(?:youtube)\s(?:https?\:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([^&#\s\?]+)(?:\?.[^\s]+)?/';
$regex = '/(?:youtube)\s(?:https?\:\/\/)?(?:www\.)?(?:youtube\.com\/watch|youtu\.be\/)([^\s]+)/';
$validate = $cursor->match($regex);

// The computer says no
if (!$validate) {
$cursor->restoreState($savedState);

return false;
}

$matches = [];
preg_match($regex, $validate, $matches);
$videoId = $matches[1];
$videoRegex = '/[^\s]*(?:v\=)([^\s\&]+)[^\s]*/';
$timeRegex = '/[^\s]*(?:t\=)([^\s\&]+)s[^\s]*/';
$timeContinueRegex = '/[^\s]*(?:time_continue\=)([^\s\&]+)[^\s]*/';
$videoMatches = [];
$timeMatches = [];
$timeContinueMatches = [];
preg_match($videoRegex, $validate, $videoMatches);
preg_match($timeRegex, $validate, $timeMatches);
preg_match($timeContinueRegex, $validate, $timeContinueMatches);
$videoId = $videoMatches[1];
$startTime = '';

if ($timeMatches) {
$startTime = "?start={$timeMatches[1]}";
} elseif ($timeContinueMatches) {
$startTime = "?start={$timeContinueMatches[1]}";
}

// Generates a valid YouTube embed url with the parsed video id from the given url
$inlineContext->getContainer()->appendChild(new YouTube("https://www.youtube.com/embed/$videoId"));
$inlineContext->getContainer()->appendChild(new YouTube("https://www.youtube.com/embed/$videoId$startTime"));

return true;
}
Expand Down
6 changes: 4 additions & 2 deletions tests/Elements/Inline/YouTubeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public function successfulStrings()
[':youtube https://www.youtube.com/watch?v=52c_QSg64fs', $expected],
[':youtube http://youtube.com/watch?v=52c_QSg64fs', $expected],
[':youtube youtube.com/watch?v=52c_QSg64fs', $expected],
[':youtube http://www.youtube.com/watch?v=52c_QSg64fs?t=10s', $expected],
[':youtube http://www.youtube.com/watch?v=52c_QSg64fs?t=10s&something=123123123SDqweas', $expected],
[':youtube http://www.youtube.com/watch?time_continue=10&v=52c_QSg64fs', '<p><span class="youtube-video"><iframe width="640" height="390" src="https://www.youtube.com/embed/52c_QSg64fs?start=10" type="text/html" frameborder="0"></iframe></span></p>'],
[':youtube http://www.youtube.com/watch?t=10s&v=52c_QSg64fs', '<p><span class="youtube-video"><iframe width="640" height="390" src="https://www.youtube.com/embed/52c_QSg64fs?start=10" type="text/html" frameborder="0"></iframe></span></p>'],
[':youtube http://www.youtube.com/watch?v=52c_QSg64fs&t=10s', '<p><span class="youtube-video"><iframe width="640" height="390" src="https://www.youtube.com/embed/52c_QSg64fs?start=10" type="text/html" frameborder="0"></iframe></span></p>'],
[':youtube http://www.youtube.com/watch?v=52c_QSg64fs&t=10s&something=123123123SDqweas', '<p><span class="youtube-video"><iframe width="640" height="390" src="https://www.youtube.com/embed/52c_QSg64fs?start=10" type="text/html" frameborder="0"></iframe></span></p>'],
];
}

Expand Down

0 comments on commit 2b741cb

Please sign in to comment.