feat(ext-blog): Add syntax highlighting for code blocks

Added language-specific syntax highlighting support to the markdown editor and preview. New code-highlighter assets are registered for public pages and admin headers. The PostContentRenderer detects language identifiers in code fences to apply appropriate CSS classes for highlighting. Tests verify the highlighting works correctly and unsafe content is escaped properly.
This commit is contained in:
2026-12-02 11:09:09 +01:00
parent e0767b65e6
commit 8ebed89e73
16 changed files with 202 additions and 11 deletions
@@ -0,0 +1,48 @@
.hljs {
color: var(--color-base-content);
}
.hljs-comment,
.hljs-quote {
opacity: 0.7;
font-style: italic;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-section {
color: var(--color-primary);
}
.hljs-string,
.hljs-title,
.hljs-name,
.hljs-type,
.hljs-attribute,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: var(--color-success);
}
.hljs-number,
.hljs-regexp,
.hljs-variable,
.hljs-template-variable {
color: var(--color-secondary);
}
.hljs-built_in,
.hljs-class .hljs-title {
color: var(--color-info);
}
.hljs-meta,
.hljs-meta .hljs-keyword {
color: var(--color-warning);
}
.hljs-deletion {
color: var(--color-error);
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,33 @@
import hljs from 'highlight.js/lib/core';
import bash from 'highlight.js/lib/languages/bash';
import css from 'highlight.js/lib/languages/css';
import diff from 'highlight.js/lib/languages/diff';
import javascript from 'highlight.js/lib/languages/javascript';
import json from 'highlight.js/lib/languages/json';
import markdown from 'highlight.js/lib/languages/markdown';
import php from 'highlight.js/lib/languages/php';
import python from 'highlight.js/lib/languages/python';
import sql from 'highlight.js/lib/languages/sql';
import typescript from 'highlight.js/lib/languages/typescript';
import xml from 'highlight.js/lib/languages/xml';
import yaml from 'highlight.js/lib/languages/yaml';
Object.entries({ bash, css, diff, javascript, json, markdown, php, python, sql, typescript, xml, yaml })
.forEach(([name, language]) => hljs.registerLanguage(name, language));
window.blogCodeHighlighter = {
highlight(root = document) {
root.querySelectorAll('pre code:not(.hljs)').forEach((code) => {
const language = code.className.match(/(?:^|\s)language-([^\s]+)/)?.[1];
if (language && !hljs.getLanguage(language)) return;
hljs.highlightElement(code);
});
},
};
const highlightPage = () => window.blogCodeHighlighter.highlight(document);
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', highlightPage, { once: true });
} else {
highlightPage();
}