feat(site-builder): add dependency-free formatHtml prettifier

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-08 18:48:41 -07:00
co-authored by Claude Opus 5
parent 4ac57e1c4c
commit 321a193b83
2 changed files with 252 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
import { describe, test, expect } from 'vitest';
import { formatHtml } from './format-html';
describe('formatHtml', () => {
test('indents nested block elements two spaces per level', () => {
expect(formatHtml('<div><section><p>hi</p></section></div>')).toBe(
'<div>\n <section>\n <p>hi</p>\n </section>\n</div>',
);
});
test('leaves inline tags on the same line as their text', () => {
expect(formatHtml('<p>hello <strong>world</strong> now</p>')).toBe(
'<p>hello <strong>world</strong> now</p>',
);
});
test('void elements do not open an indent level', () => {
expect(formatHtml('<div><img src="a.png"><br><p>x</p></div>')).toBe(
'<div>\n <img src="a.png">\n <br>\n <p>x</p>\n</div>',
);
});
test('preserves <pre> contents verbatim', () => {
const src = '<div><pre> keep\n this</pre></div>';
expect(formatHtml(src)).toBe('<div>\n <pre> keep\n this</pre>\n</div>');
});
test('is idempotent', () => {
const once = formatHtml('<div><section><p>hi</p></section></div>');
expect(formatHtml(once)).toBe(once);
});
test('empty and whitespace-only input round-trip to an empty string', () => {
expect(formatHtml('')).toBe('');
expect(formatHtml(' \n ')).toBe('');
});
test('unbalanced markup never produces negative indent', () => {
expect(formatHtml('</div><p>x</p>')).toBe('</div>\n<p>x</p>');
});
});