Files
plugi/extensions/plugi-ext-blog/tests/CodeHighlighterTest.js
T
deepDiverPaul 8ebed89e73 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.
2026-12-02 11:09:09 +01:00

44 lines
1.4 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
const createCodeBlock = (className, text) => {
const classes = new Set(className.split(/\s+/).filter(Boolean));
const code = {
className: [...classes].join(' '),
children: [],
dataset: {},
innerHTML: '',
textContent: text,
classList: {
add(name) {
classes.add(name);
code.className = [...classes].join(' ');
},
},
};
return code;
};
globalThis.document = { readyState: 'complete', querySelectorAll: () => [] };
globalThis.window = {};
await import('../assets/src/code-highlighter.js');
test('unsupported language labels remain plain without interrupting later blocks', () => {
const unsupported = createCodeBlock('language-not-a-language', '<?php echo 1;');
const supported = createCodeBlock('language-php', '<?php echo 1;');
const warnings = [];
const originalWarn = console.warn;
console.warn = (...args) => warnings.push(args);
try {
window.blogCodeHighlighter.highlight({ querySelectorAll: () => [unsupported, supported] });
} finally {
console.warn = originalWarn;
}
assert.deepEqual(warnings, []);
assert.equal(unsupported.className, 'language-not-a-language');
assert.deepEqual(unsupported.dataset, {});
assert.equal(supported.dataset.highlighted, 'yes');
assert.match(supported.innerHTML, /<span class="hljs-/);
});