Fix markdown code block parser to support language tags and UI classes
Lint / PHP (phpcs PSR-12) (push) Failing after 18s
Lint / JS (eslint) (push) Successful in 7s
Lint / PHP requirements (version + extensions) (push) Successful in 19s
Security / PHP Security (semgrep) (push) Successful in 56s
Lint / Deploy (push) Has been skipped
Lint / Notify on failure (push) Successful in 2s

This commit is contained in:
2026-07-14 23:46:28 -04:00
parent 5dea47cd01
commit 8f7c669b8f
+15 -3
View File
@@ -41,10 +41,22 @@ function parseMarkdown(markdown) {
.replace(/"/g, '"')
.replace(/'/g, ''');
// Code blocks (```code```) - preserve content and don't process further
// Code blocks (```lang\ncode\n```) - preserve content and don't process further
const codeBlocks = [];
html = html.replace(/```([\s\S]*?)```/g, function(match, code) {
codeBlocks.push('<pre class="code-block"><code>' + code + '</code></pre>');
html = html.replace(/```([a-zA-Z0-9_+-]*)\n?([\s\S]*?)```/g, function(match, lang, code) {
lang = lang ? lang.trim() : '';
const displayLang = lang || 'text';
// Build header with optional copy button if one exists in your UI, otherwise just lang
const header = '<div class="lt-code-header"><span class="lt-code-lang">' + displayLang + '</span></div>';
// Remove exactly one trailing newline from code block if it exists
if (code.endsWith('\n')) {
code = code.slice(0, -1);
}
// Wrap in the specific UI classes expected by base.css
codeBlocks.push('<div class="lt-code-block">' + header + '<pre><code>' + code + '</code></pre></div>');
return '%%CODEBLOCK' + (codeBlocks.length - 1) + '%%';
});