55namespace Yiisoft \ErrorHandler \Renderer ;
66
77use Alexkart \CurlBuilder \Command ;
8+ use cebe \markdown \GithubMarkdown ;
89use Psr \Http \Message \ServerRequestInterface ;
910use RuntimeException ;
1011use Throwable ;
4950 */
5051final class HtmlRenderer implements ThrowableRendererInterface
5152{
53+ private GithubMarkdown $ markdownParser ;
54+
5255 /**
5356 * @var string The full path to the default template directory.
5457 */
@@ -110,11 +113,14 @@ final class HtmlRenderer implements ThrowableRendererInterface
110113 */
111114 public function __construct (array $ settings = [])
112115 {
116+ $ this ->markdownParser = new GithubMarkdown ();
117+ $ this ->markdownParser ->html5 = true ;
118+
113119 $ this ->defaultTemplatePath = dirname (__DIR__ , 2 ) . '/templates ' ;
114120 $ this ->template = $ settings ['template ' ] ?? $ this ->defaultTemplatePath . '/production.php ' ;
115121 $ this ->verboseTemplate = $ settings ['verboseTemplate ' ] ?? $ this ->defaultTemplatePath . '/development.php ' ;
116- $ this ->maxSourceLines = $ settings ['maxSourceLines ' ] ?? 19 ;
117- $ this ->maxTraceLines = $ settings ['maxTraceLines ' ] ?? 13 ;
122+ $ this ->maxSourceLines = $ settings ['maxSourceLines ' ] ?? 19 ;
123+ $ this ->maxTraceLines = $ settings ['maxTraceLines ' ] ?? 13 ;
118124 $ this ->traceHeaderLine = $ settings ['traceHeaderLine ' ] ?? null ;
119125 }
120126
@@ -146,6 +152,49 @@ public function htmlEncode(string $content): string
146152 return htmlspecialchars ($ content , ENT_QUOTES , 'UTF-8 ' );
147153 }
148154
155+ public function parseMarkdown (string $ content ): string
156+ {
157+ $ html = $ this ->markdownParser ->parse ($ content );
158+ /**
159+ * @psalm-suppress InvalidArgument
160+ *
161+ * @link https://github.com/vimeo/psalm/issues/4317
162+ */
163+ return strip_tags ($ html , [
164+ 'h1 ' ,
165+ 'h2 ' ,
166+ 'h3 ' ,
167+ 'h4 ' ,
168+ 'h5 ' ,
169+ 'h6 ' ,
170+ 'hr ' ,
171+ 'pre ' ,
172+ 'code ' ,
173+ 'blockquote ' ,
174+ 'table ' ,
175+ 'tr ' ,
176+ 'td ' ,
177+ 'th ' ,
178+ 'thead ' ,
179+ 'tbody ' ,
180+ 'strong ' ,
181+ 'em ' ,
182+ 'b ' ,
183+ 'i ' ,
184+ 'u ' ,
185+ 's ' ,
186+ 'span ' ,
187+ 'a ' ,
188+ 'p ' ,
189+ 'br ' ,
190+ 'nobr ' ,
191+ 'ul ' ,
192+ 'ol ' ,
193+ 'li ' ,
194+ 'img ' ,
195+ ]);
196+ }
197+
149198 /**
150199 * Renders the previous exception stack for a given Exception.
151200 *
@@ -286,7 +335,7 @@ public function renderRequest(ServerRequestInterface $request): string
286335
287336 $ output .= "\n" . $ request ->getBody () . "\n\n" ;
288337
289- return '<pre> ' . $ this ->htmlEncode (rtrim ($ output , "\n" )) . '</pre> ' ;
338+ return '<pre class="codeBlock language-text" > ' . $ this ->htmlEncode (rtrim ($ output , "\n" )) . '</pre> ' ;
290339 }
291340
292341 /**
@@ -303,10 +352,10 @@ public function renderCurl(ServerRequestInterface $request): string
303352 ->setRequest ($ request )
304353 ->build ();
305354 } catch (Throwable $ e ) {
306- $ output = 'Error generating curl command: ' . $ e ->getMessage ();
355+ return $ this -> htmlEncode ( 'Error generating curl command: ' . $ e ->getMessage () );
307356 }
308357
309- return $ this ->htmlEncode ($ output );
358+ return ' <div class="codeBlock language-sh"> ' . $ this ->htmlEncode ($ output ) . ' </div> ' ;
310359 }
311360
312361 /**
0 commit comments