Skip to content

Commit edf844e

Browse files
committed
feat: add video conversion to HTML in Markdown parser
1 parent cd2bc1f commit edf844e

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

app/Support/Markdown.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function parse(string $text): static
5252

5353
$static->convert();
5454
$static->removeH1Tags();
55-
$static->convertSpecialBlockQuotes();
55+
$static->convertSpecialBlockquotes();
5656

5757
return $static;
5858
}
@@ -77,7 +77,7 @@ protected function removeH1Tags(): static
7777
return $this;
7878
}
7979

80-
protected function convertSpecialBlockQuotes(): static
80+
protected function convertSpecialBlockquotes(): static
8181
{
8282
$this->content = preg_replace(
8383
pattern: [
@@ -111,6 +111,33 @@ public function absoluteImageUrls(string $baseUrl): static
111111
return $this;
112112
}
113113

114+
public function convertVideoToHtml(): static
115+
{
116+
$this->content = preg_replace_callback(
117+
pattern: '/(?<!src=["\'])https:\/\/github\.com\/user-attachments\/assets\/[a-f0-9\-]+/i',
118+
callback: function (array $matches): string {
119+
[$url] = $matches;
120+
121+
/**
122+
* If the asset is already present elsewhere, replace it with an empty string to avoid duplication.
123+
* Some authors kept both the asset and the video tag in the markdown file ¯\_(ツ)_/¯.
124+
*/
125+
if (substr_count($this->content, $url) > 1) {
126+
return '';
127+
}
128+
129+
return <<<HTML
130+
<video width="320" height="240" controls>
131+
<source src="$url" type="video/mp4">
132+
</video>
133+
HTML;
134+
},
135+
subject: $this->content
136+
);
137+
138+
return $this;
139+
}
140+
114141
public function __toString(): string
115142
{
116143
return str($this->content)->sanitizeHtml();

resources/views/plugins/view-plugin.blade.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,13 @@ class="block w-32 rounded-lg border border-gray-300 bg-gray-50 p-2 text-sm text-
344344
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"
345345
>
346346
{!!
347-
\App\Support\Markdown::parse($docs)->absoluteImageUrls(
348-
baseUrl: str($plugin->getDocUrl(request()->query('v')))
349-
->lower()
350-
->before('readme.md'),
351-
)
347+
\App\Support\Markdown::parse($docs)
348+
->convertVideoToHtml()
349+
->absoluteImageUrls(
350+
baseUrl: str($plugin->getDocUrl(request()->query('v')))
351+
->lower()
352+
->before('readme.md'),
353+
)
352354
!!}
353355
</div>
354356
</div>

0 commit comments

Comments
 (0)