22
33namespace App \Support ;
44
5+ use Illuminate \Contracts \Support \Htmlable ;
56use Illuminate \Support \HtmlString ;
67use League \CommonMark \Environment \Environment ;
78use League \CommonMark \Extension \CommonMark \CommonMarkCoreExtension ;
89use League \CommonMark \Extension \HeadingPermalink \HeadingPermalinkExtension ;
910use League \CommonMark \Extension \Table \TableExtension ;
1011use League \CommonMark \Extension \TableOfContents \TableOfContentsExtension ;
1112use League \CommonMark \MarkdownConverter ;
13+ use Stringable ;
1214use Torchlight \Commonmark \V2 \TorchlightExtension ;
1315
14- class Markdown
16+ class Markdown implements Htmlable, Stringable
1517{
16- public static function parse (string $ text ): HtmlString
17- {
18- $ text = self ::convertSpecialBlockQuotes ($ text );
18+ protected Environment $ environment ;
1919
20- $ environment = new Environment ([
20+ public function __construct (protected string $ content )
21+ {
22+ $ this ->environment = new Environment ([
2123 'allow_unsafe_links ' => false ,
2224 'heading_permalink ' => [
2325 'html_class ' => 'heading-anchor ' ,
@@ -37,35 +39,85 @@ public static function parse(string $text): HtmlString
3739 ],
3840 ]);
3941
40- $ environment ->addExtension (new CommonMarkCoreExtension );
41- $ environment ->addExtension (new TableExtension );
42- $ environment ->addExtension (new HeadingPermalinkExtension );
43- $ environment ->addExtension (new TorchlightExtension );
44- $ environment ->addExtension (new TableOfContentsExtension );
42+ $ this ->environment ->addExtension (new CommonMarkCoreExtension );
43+ $ this ->environment ->addExtension (new TableExtension );
44+ $ this ->environment ->addExtension (new HeadingPermalinkExtension );
45+ $ this ->environment ->addExtension (new TorchlightExtension );
46+ $ this ->environment ->addExtension (new TableOfContentsExtension );
47+ }
48+
49+ public static function parse (string $ text ): static
50+ {
51+ $ static = app (static ::class, ['content ' => $ text ]);
52+
53+ $ static ->convert ();
54+ $ static ->removeH1Tags ();
55+ $ static ->convertSpecialBlockQuotes ();
56+
57+ return $ static ;
58+ }
59+
60+ protected function convert (): static
61+ {
62+ $ this ->content = (new MarkdownConverter ($ this ->environment ))
63+ ->convert ($ this ->content )
64+ ->getContent ();
65+
66+ return $ this ;
67+ }
68+
69+ protected function removeH1Tags (): static
70+ {
71+ $ this ->content = preg_replace (
72+ pattern: '/\<h1(.*)\>(.*)\<\/h1\>/ ' ,
73+ replacement: '' ,
74+ subject: $ this ->content
75+ );
4576
46- $ converter = new MarkdownConverter ($ environment );
77+ return $ this ;
78+ }
4779
48- return new HtmlString ($ converter ->convert ($ text )->getContent ());
80+ protected function convertSpecialBlockQuotes (): static
81+ {
82+ $ this ->content = preg_replace (
83+ pattern: [
84+ '/> \[\!NOTE\]\s*\n> / ' ,
85+ '/> \[\!TIP\]\s*\n> / ' ,
86+ '/> \[\!IMPORTANT\]\s*\n> / ' ,
87+ '/> \[\!WARNING\]\s*\n> / ' ,
88+ '/> \[\!CAUTION\]\s*\n> / ' ,
89+ ],
90+ replacement: [
91+ '> 📝 **Note:** ' ,
92+ '> 💡 **Tip:** ' ,
93+ '> ❗ **Important:** ' ,
94+ '> ⚠️ **Warning:** ' ,
95+ '> ⚠️ **Caution:** ' ,
96+ ],
97+ subject: $ this ->content
98+ );
99+
100+ return $ this ;
101+ }
102+
103+ public function absoluteImageUrls (string $ baseUrl ): static
104+ {
105+ $ this ->content = preg_replace (
106+ pattern: '/src=[" \'](?!https?:\/\/)([^" \']+)[" \'][^>]/i ' ,
107+ replacement: 'src=" ' . $ baseUrl . '$1" ' ,
108+ subject: $ this ->content
109+ );
110+
111+ return $ this ;
112+ }
113+
114+ public function __toString (): string
115+ {
116+ return str ($ this ->content )->sanitizeHtml ();
49117 }
50118
51- protected static function convertSpecialBlockQuotes ( string $ text ): string
119+ public function toHtml ()
52120 {
53- $ searchPatterns = [
54- '/> \[\!NOTE\]\s*\n> / ' ,
55- '/> \[\!TIP\]\s*\n> / ' ,
56- '/> \[\!IMPORTANT\]\s*\n> / ' ,
57- '/> \[\!WARNING\]\s*\n> / ' ,
58- '/> \[\!CAUTION\]\s*\n> / ' ,
59- ];
60- $ replacePatterns = [
61- '> 📝 **Note:** ' ,
62- '> 💡 **Tip:** ' ,
63- '> ❗ **Important:** ' ,
64- '> ⚠️ **Warning:** ' ,
65- '> ⚠️ **Caution:** ' ,
66- ];
67-
68- // Perform the replacement
69- return preg_replace ($ searchPatterns , $ replacePatterns , $ text );
121+ return new HtmlString ($ this ->content );
70122 }
71123}
0 commit comments