Skip to content

Commit

Permalink
Merge pull request #627 from filamentphp/feat/convert-assets-to-video…
Browse files Browse the repository at this point in the history
…-tag
  • Loading branch information
danharrin authored Jan 6, 2025
2 parents cd2bc1f + edf844e commit 82cd0e4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
31 changes: 29 additions & 2 deletions app/Support/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function parse(string $text): static

$static->convert();
$static->removeH1Tags();
$static->convertSpecialBlockQuotes();
$static->convertSpecialBlockquotes();

return $static;
}
Expand All @@ -77,7 +77,7 @@ protected function removeH1Tags(): static
return $this;
}

protected function convertSpecialBlockQuotes(): static
protected function convertSpecialBlockquotes(): static
{
$this->content = preg_replace(
pattern: [
Expand Down Expand Up @@ -111,6 +111,33 @@ public function absoluteImageUrls(string $baseUrl): static
return $this;
}

public function convertVideoToHtml(): static
{
$this->content = preg_replace_callback(
pattern: '/(?<!src=["\'])https:\/\/github\.com\/user-attachments\/assets\/[a-f0-9\-]+/i',
callback: function (array $matches): string {
[$url] = $matches;

/**
* If the asset is already present elsewhere, replace it with an empty string to avoid duplication.
* Some authors kept both the asset and the video tag in the markdown file ¯\_(ツ)_/¯.
*/
if (substr_count($this->content, $url) > 1) {
return '';
}

return <<<HTML
<video width="320" height="240" controls>
<source src="$url" type="video/mp4">
</video>
HTML;
},
subject: $this->content
);

return $this;
}

public function __toString(): string
{
return str($this->content)->sanitizeHtml();
Expand Down
12 changes: 7 additions & 5 deletions resources/views/plugins/view-plugin.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,13 @@ class="block w-32 rounded-lg border border-gray-300 bg-gray-50 p-2 text-sm text-
class="prose selection:bg-stone-500/30 prose-a:break-words prose-blockquote:not-italic prose-code:break-words prose-code:rounded prose-code:bg-merino prose-code:px-1.5 prose-code:py-0.5 prose-code:font-normal prose-code:before:hidden prose-code:after:hidden [&_p]:before:hidden [&_p]:after:hidden"
>
{!!
\App\Support\Markdown::parse($docs)->absoluteImageUrls(
baseUrl: str($plugin->getDocUrl(request()->query('v')))
->lower()
->before('readme.md'),
)
\App\Support\Markdown::parse($docs)
->convertVideoToHtml()
->absoluteImageUrls(
baseUrl: str($plugin->getDocUrl(request()->query('v')))
->lower()
->before('readme.md'),
)
!!}
</div>
</div>
Expand Down

0 comments on commit 82cd0e4

Please sign in to comment.