Skip to content

Commit dae4c79

Browse files
committed
fix: escape special chars in code text
1 parent 1bc91f9 commit dae4c79

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

src/Renderers/CodeNodeRenderer.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ public function render(): string
5757
return $code;
5858
}
5959

60+
$savedCode = $code;
61+
6062
$language = $this->codeNode->getLanguage() ?? 'php';
6163
$languageMapping = self::LANGUAGES_MAPPING[$language] ?? $language;
6264
$languages = array_unique([$language, $languageMapping]);
6365

6466
if ('text' === $language) {
65-
$highlightedCode = $code;
67+
// Highlighter escapes correctly the code, we need to manually escape only for "text" code
68+
$highlightedCode = $this->escapeForbiddenCharactersInsideCodeBlock($code);
6669
} else {
6770
$this->configureHighlighter();
6871

@@ -117,23 +120,6 @@ public static function isLanguageSupported(string $lang): bool
117120
return \in_array($lang, $supportedLanguages, true);
118121
}
119122

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-
137123
private function configureHighlighter()
138124
{
139125
if (false === self::$isHighlighterConfigured) {
@@ -143,4 +129,15 @@ private function configureHighlighter()
143129

144130
self::$isHighlighterConfigured = true;
145131
}
132+
133+
/**
134+
* Code blocks are displayed in "<pre>" tags, which has some reserved characters:
135+
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
136+
*/
137+
private function escapeForbiddenCharactersInsideCodeBlock(string $code): string
138+
{
139+
$codeEscaped = preg_replace('/&(?!amp;|lt;|gt;|quot;)/', '&amp;', $code);
140+
141+
return strtr($codeEscaped, ['<' => '&lt;', '>' => '&gt;', '"' => '&quot;']);
142+
}
146143
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div translate="no" data-loc="1" class="notranslate codeblock codeblock-length-sm codeblock-text">
22
<div class="codeblock-scroll">
33
<pre class="codeblock-lines">1</pre>
4-
<pre class="codeblock-code"><code>some text</code></pre>
4+
<pre class="codeblock-code"><code>some text with special chars &lt; &gt; " &amp; and some text with special chars already escaped &lt; &gt; " &amp;</code></pre>
55
</div>
66
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22
.. code-block:: text
3-
some text
3+
some text with special chars < > " & and some text with special chars already escaped &lt; &gt; &quot; &amp;

0 commit comments

Comments
 (0)