Skip to content

Commit dc81ffd

Browse files
committed
Allow videos in markdown
1 parent f5f938e commit dc81ffd

File tree

4 files changed

+376
-6
lines changed

4 files changed

+376
-6
lines changed

app/Support/CommonMark/CommonMark.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,62 @@
44

55
use App\Extensions\TorchlightWithCopyExtension;
66
use League\CommonMark\CommonMarkConverter;
7+
use League\CommonMark\Environment\Environment;
8+
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
79
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
10+
use League\CommonMark\Extension\Embed\Bridge\OscaroteroEmbedAdapter;
11+
use League\CommonMark\Extension\Embed\Embed as EmbedNode;
12+
use League\CommonMark\Extension\Embed\EmbedExtension;
13+
use League\CommonMark\Extension\Embed\EmbedRenderer;
14+
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
815
use League\CommonMark\Extension\Table\TableExtension;
16+
use League\CommonMark\MarkdownConverter;
17+
use League\CommonMark\Renderer\HtmlDecorator;
18+
use Torchlight\Commonmark\V2\TorchlightExtension;
919

1020
class CommonMark
1121
{
22+
protected static ?MarkdownConverter $converter = null;
23+
1224
public static function convertToHtml(string $markdown): string
1325
{
14-
$converter = new CommonMarkConverter;
15-
$converter->getEnvironment()->addRenderer(Heading::class, new HeadingRenderer);
16-
$converter->getEnvironment()->addExtension(new TableExtension);
17-
$converter->getEnvironment()->addExtension(new TorchlightWithCopyExtension);
26+
return static::getConverter()->convert($markdown)->getContent();
27+
}
28+
29+
protected static function getConverter(): MarkdownConverter
30+
{
31+
if (static::$converter === null) {
32+
$config = [
33+
'html_input' => 'allow',
34+
'allow_unsafe_links' => false,
35+
'max_nesting_level' => 10,
36+
'embed' => [
37+
'adapter' => new OscaroteroEmbedAdapter,
38+
'fallback' => 'link',
39+
],
40+
];
41+
42+
$environment = new Environment($config);
43+
44+
$environment->addExtension(new CommonMarkCoreExtension);
45+
$environment->addExtension(new GithubFlavoredMarkdownExtension);
46+
$environment->addRenderer(Heading::class, new HeadingRenderer);
47+
$environment->addExtension(new TableExtension);
48+
$environment->addExtension(new EmbedExtension);
49+
50+
$environment->addRenderer(
51+
EmbedNode::class,
52+
new HtmlDecorator(new EmbedRenderer, 'div', ['class' => 'relative aspect-video w-full'])
53+
);
54+
55+
// Add Torchlight extension if available
56+
if (class_exists(TorchlightExtension::class)) {
57+
$environment->addExtension(new TorchlightWithCopyExtension);
58+
}
59+
60+
static::$converter = new MarkdownConverter($environment);
61+
}
1862

19-
return $converter->convert($markdown);
63+
return static::$converter;
2064
}
2165
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"artesaos/seotools": "^1.2",
1313
"blade-ui-kit/blade-heroicons": "^2.3",
1414
"doctrine/dbal": "^3.9",
15+
"embed/embed": "^4.4",
1516
"filament/filament": "^3.2",
1617
"guzzlehttp/guzzle": "^7.2",
1718
"laravel/cashier": "^15.6",

0 commit comments

Comments
 (0)