Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/secure-77/Perlite into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
secure-77 committed Sep 23, 2024
2 parents ee621a0 + eaf86e4 commit 51cf6ac
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions perlite/.src/PerliteParsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
class PerliteParsedown extends Parsedown
{

public function __construct()
{
$this->BlockTypes['!'] = array('YouTube');
}

function text($text)
{
Expand Down Expand Up @@ -478,6 +482,53 @@ protected function blockHeader($Line)
}
}

protected function blockYouTube($Line)
{

if ( ! isset($Line['text'][1]) or $Line['text'][1] !== '[')
{
return;
}

$Line['text']= substr($Line['text'], 1);

$Link = $this->inlineLink($Line);


if ($Link === null)
{
return;
}

// See: https://stackoverflow.com/a/64320469
$yt = preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $Link['element']['attributes']['href'], $match);

if (! $yt)
{
return;
}

$youtubeId = $match[1];
$Block = array(
'element' => array(
'name' => 'iframe',
'text' => $Line['text'],
'handler' => 'line',

'attributes' => array(
'class' => 'external-embed mod-receives-events', 'sandbox' => 'allow-forms allow-presentation allow-same-origin allow-scripts allow-modals allow-popups',
'allow' => 'fullscreen',
'frameborder' => '0',
'src' => 'https://www.youtube.com/embed/'. $youtubeId,
),

),
);

return $Block;
}


# extend to obsidian tags
protected $inlineMarkerList = '!"*$_#&[:<>`~\\';
protected $InlineTypes = array(
Expand Down

0 comments on commit 51cf6ac

Please sign in to comment.