Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added YouTube link support #133

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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