]> git.bts.cx Git - cx.git/commitdiff
added youtube embeds
authorBen Sherratt <redacted>
Thu, 14 Mar 2024 12:00:28 +0000 (12:00 +0000)
committerBen Sherratt <redacted>
Thu, 14 Mar 2024 12:00:28 +0000 (12:00 +0000)
cx/lib/markdown.php

index 00d46c29f64dd3d54dc257d6818f9fbd357714b9..771b9476370ed2546633eabdf9aea85454e87d8d 100644 (file)
@@ -6,7 +6,8 @@ cx_require('third_party', 'parsedown', 'Parsedown.php');
 
 class ExtendedParsedown extends Parsedown {
        public function __construct() {
-               $this->InlineTypes['{'][]= 'BlogImage';
+               $this->InlineTypes['{'][] = 'BlogImage';
+               $this->InlineTypes['{'][] = 'Youtube';
                $this->inlineMarkerList .= '{';
        }
 
@@ -33,6 +34,35 @@ class ExtendedParsedown extends Parsedown {
                        );
                }
        }
+
+       protected function inlineYoutube($excerpt) {
+               if (preg_match('/^{yt:([^}]+)}/', $excerpt['text'], $matches)) {
+                       $video_id = $matches[1];
+
+                       if ($video_id == null) {
+                               return;
+                       }
+
+                       return array(
+                               'extent' => strlen($matches[0]) + 1, 
+                               'element' => array(
+                                       'name' => 'iframe',
+                                       'text' => '',
+                                       'attributes' => array(
+                                               'id' => "ytplayer",
+                                               'type' => "text/html",
+                                               'width' => "640",
+                                               'height' => "360",
+                                               'src' => "https://www.youtube.com/embed/" . $video_id,
+                                               'frameborder' => "0",
+                                               'loading' => "lazy",
+                                               'referrerpolicy' => "no-referrer",
+                                               'sandbox' => "allow-same-origin allow-scripts",
+                                       ),
+                               ),
+                       );
+               }
+       }
 }
 
 function cx_markdown_generate_html($markdown) {