Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 6e958ea

Browse files
authored
fix: ensure consistent code output
1 parent fa1b850 commit 6e958ea

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/Extensions/Highlighter/CodeBlockHighlighter.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ class CodeBlockHighlighter
88
{
99
public function highlight(string $codeBlock, ?string $language = null)
1010
{
11-
if ($language === "xml") {
12-
return $codeBlock;
11+
if (str_contains($codeBlock, "<")) {
12+
preg_match('#<\s*?code\b[^>]*>(.*?)</code\b[^>]*>#s', $codeBlock, $matches);
13+
14+
$codeBlock = $matches[1];
15+
} else {
16+
$codeBlock = trim(htmlspecialchars_decode(strip_tags($codeBlock)));
1317
}
1418

1519
try {
16-
return vsprintf('<code class="hljs-copy language-%s">%s</code>', [
17-
$language,
18-
trim(htmlspecialchars_decode(strip_tags($codeBlock))),
19-
]);
20+
return vsprintf('<code class="hljs-copy language-%s">%s</code>', [$language, $codeBlock]);
2021
} catch (DomainException $e) {
2122
return $codeBlock;
2223
}

0 commit comments

Comments
 (0)