Skip to content

fix: escape special chars in code text #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions src/Renderers/CodeNodeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function render(): string
$languages = array_unique([$language, $languageMapping]);

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

Expand Down Expand Up @@ -117,23 +118,6 @@ public static function isLanguageSupported(string $lang): bool
return \in_array($lang, $supportedLanguages, true);
}

private function getLines(string $code): array
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was not used anywhere

{
$lines = preg_split('/\r\n|\r|\n/', $code);
$reversedLines = array_reverse($lines);

// trim empty lines at the end of the code
foreach ($reversedLines as $key => $line) {
if ('' !== trim($line)) {
break;
}

unset($reversedLines[$key]);
}

return array_reverse($reversedLines);
}

private function configureHighlighter()
{
if (false === self::$isHighlighterConfigured) {
Expand All @@ -143,4 +127,15 @@ private function configureHighlighter()

self::$isHighlighterConfigured = true;
}

/**
* Code blocks are displayed in "<pre>" tags, which has some reserved characters:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
*/
private function escapeForbiddenCharactersInsideCodeBlock(string $code): string
{
$codeEscaped = preg_replace('/&(?!amp;|lt;|gt;|quot;)/', '&amp;', $code);

return strtr($codeEscaped, ['<' => '&lt;', '>' => '&gt;', '"' => '&quot;']);
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/expected/blocks/code-blocks/text.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div translate="no" data-loc="1" class="notranslate codeblock codeblock-length-sm codeblock-text">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1</pre>
<pre class="codeblock-code"><code>some text</code></pre>
<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>
</div>
</div>
2 changes: 1 addition & 1 deletion tests/fixtures/source/blocks/code-blocks/text.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

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