Skip to content

Commit 2b741cb

Browse files
committed
Added video and time features to YouTube
1 parent a8666d0 commit 2b741cb

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/Inline/Parser/YouTubeParser.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/Elements/Inline/YouTubeTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public function successfulStrings()
2424
[':youtube https://www.youtube.com/watch?v=52c_QSg64fs', $expected],
2525
[':youtube http://youtube.com/watch?v=52c_QSg64fs', $expected],
2626
[':youtube youtube.com/watch?v=52c_QSg64fs', $expected],
27-
[':youtube http://www.youtube.com/watch?v=52c_QSg64fs?t=10s', $expected],
28-
[':youtube http://www.youtube.com/watch?v=52c_QSg64fs?t=10s&something=123123123SDqweas', $expected],
27+
[':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>'],
28+
[':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>'],
29+
[':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>'],
30+
[':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>'],
2931
];
3032
}
3133

0 commit comments

Comments
 (0)