diff --git a/libs/timeline.css b/libs/timeline.css index 6107483..c6a39df 100644 --- a/libs/timeline.css +++ b/libs/timeline.css @@ -141,6 +141,11 @@ img.avatar { border: none; } +.embed-video { + width: 100%; + aspect-ratio: 16/9; +} + a.author { text-decoration: none; color: var(--text); diff --git a/libs/twtxt.php b/libs/twtxt.php index 64631e1..4b2068e 100644 --- a/libs/twtxt.php +++ b/libs/twtxt.php @@ -180,8 +180,9 @@ function replaceLinksFromTwt(string $twtString) { // 1. Look into how yarnd handles this // Regular expression pattern to match URLs - $pattern = '/(?)(?\s]+)/is'; + // Replace URLs with clickable links $replacement = '$1'; $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 "
";
+		//print_r($youtubeLinks);
+		//echo "
"; + + foreach ($youtubeLinks as $videoID) { + $twtString .= '
'; + } + } + + $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
\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);