@@ -62,7 +62,8 @@ public function render(): string
6262 $ languages = array_unique ([$ language , $ languageMapping ]);
6363
6464 if ('text ' === $ language ) {
65- $ highlightedCode = $ code ;
65+ // Highlighter escapes correctly the code, we need to manually escape only for "text" code
66+ $ highlightedCode = $ this ->escapeForbiddenCharactersInsideCodeBlock ($ code );
6667 } else {
6768 $ this ->configureHighlighter ();
6869
@@ -117,23 +118,6 @@ public static function isLanguageSupported(string $lang): bool
117118 return \in_array ($ lang , $ supportedLanguages , true );
118119 }
119120
120- private function getLines (string $ code ): array
121- {
122- $ lines = preg_split ('/\r\n|\r|\n/ ' , $ code );
123- $ reversedLines = array_reverse ($ lines );
124-
125- // trim empty lines at the end of the code
126- foreach ($ reversedLines as $ key => $ line ) {
127- if ('' !== trim ($ line )) {
128- break ;
129- }
130-
131- unset($ reversedLines [$ key ]);
132- }
133-
134- return array_reverse ($ reversedLines );
135- }
136-
137121 private function configureHighlighter ()
138122 {
139123 if (false === self ::$ isHighlighterConfigured ) {
@@ -143,4 +127,15 @@ private function configureHighlighter()
143127
144128 self ::$ isHighlighterConfigured = true ;
145129 }
130+
131+ /**
132+ * Code blocks are displayed in "<pre>" tags, which has some reserved characters:
133+ * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
134+ */
135+ private function escapeForbiddenCharactersInsideCodeBlock (string $ code ): string
136+ {
137+ $ codeEscaped = preg_replace ('/&(?!amp;|lt;|gt;|quot;)/ ' , '& ' , $ code );
138+
139+ return strtr ($ codeEscaped , ['< ' => '< ' , '> ' => '> ' , '" ' => '" ' ]);
140+ }
146141}
0 commit comments