Embeds youtube videos

This commit is contained in:
sørenpeter 2024-11-30 14:47:46 +01:00
parent cc7237013c
commit e1f6072b31
2 changed files with 34 additions and 4 deletions

View file

@ -141,6 +141,11 @@ img.avatar {
border: none;
}
.embed-video {
width: 100%;
aspect-ratio: 16/9;
}
a.author {
text-decoration: none;
color: var(--text);

View file

@ -180,8 +180,9 @@ function replaceLinksFromTwt(string $twtString) {
// 1. Look into how yarnd handles this
// Regular expression pattern to match URLs
$pattern = '/(?<!\S)(\b(https?|ftp|gemini|spartan|gopher):\/\/\S+|\b(?!:\/\/)\w+(?:\.\w+)+(?:\/\S+)?)(?!\S)/';
//$pattern = '/(?<!\S)(\b(https?|ftp|gemini|spartan|gopher):\/\/\S+|\b(?!:\/\/)\w+(?:\.\w+)+(?:\/\S+)?)(?!\S)/';
$pattern = '/(?<!\S)(?<!href="|">)(?<!src=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/is';
// Replace URLs with clickable links
$replacement = '<a href="$1">$1</a>';
$result = preg_replace($pattern, $replacement, $twtString);
@ -216,6 +217,29 @@ function replaceTagsFromTwt(string $twtString) {
return $result;
}
function embedYoutubeFromTwt(string $twtString) {
// original regex source: https://gist.github.com/afeld/1254889#gistcomment-1253992
$pattern = '/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/mi';
if(preg_match_all($pattern, $twtString, $youtubeLinks)) {
$youtubeLinks = array_unique($youtubeLinks[1]); // Remove dublicate cause by raw URLs conceverter to links
//echo "<pre>";
//print_r($youtubeLinks);
//echo "</pre>";
foreach ($youtubeLinks as $videoID) {
$twtString .= '<br><iframe loading="lazy" src="https://www.youtube.com/embed/'.$videoID.'" class="embed-video" allow="encrypted-media" title="" allowfullscreen="allowfullscreen" frameborder="0"></iframe>';
}
}
$result = $twtString;
return $result;
}
function getTimeElapsedString($timestamp, $full = false) {
$now = new DateTime;
@ -407,11 +431,12 @@ function getTwtsFromTwtxtString($url) {
// For some reason I was having trouble finding this nomenclature
// that's why I leave the UTF-8 representation for future reference
$twtContent = str_replace("\u{2028}", "\n<br>\n", $twtContent);
$twtContent = replaceMarkdownLinksFromTwt($twtContent);
$twtContent = replaceImagesFromTwt($twtContent);
//$twtContent = Slimdown::render($twtContent);
$twtContent = replaceLinksFromTwt($twtContent); // TODO:
$twtContent = embedYoutubeFromTwt($twtContent); // TODO: Find the right order to embed youtube, so we don't get two video due to links containing URL as link texts
$twtContent = replaceLinksFromTwt($twtContent); // TODO
// Get and remove the hash
$hash = getReplyHashFromTwt($twtContent);