@@ -62,7 +62,8 @@ public function render(): string
62
62
$ languages = array_unique ([$ language , $ languageMapping ]);
63
63
64
64
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 );
66
67
} else {
67
68
$ this ->configureHighlighter ();
68
69
@@ -117,23 +118,6 @@ public static function isLanguageSupported(string $lang): bool
117
118
return \in_array ($ lang , $ supportedLanguages , true );
118
119
}
119
120
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
-
137
121
private function configureHighlighter ()
138
122
{
139
123
if (false === self ::$ isHighlighterConfigured ) {
@@ -143,4 +127,15 @@ private function configureHighlighter()
143
127
144
128
self ::$ isHighlighterConfigured = true ;
145
129
}
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
+ }
146
141
}
0 commit comments