0de11320bb
Инструмент в tools/covergen: SVG 1200x630, детерминированный от слага (FNV-1a -> mulberry32 -> палитра), 5 стилей (poster, bauhaus, constructivist, aurora, paper), типографика с переносом и подбором кегля, аудит геометрии и WCAG-контраста, CLI для одной обложки и для манифеста. Без зависимостей, node >= 18, 12 тестов (node --test) — проходят в этой раскладке. Документация и объём работ по встраиванию в сайт: docs/covergen.md, задача #1.
55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const { paletteFor, hash } = require('./palette');
|
|
const { STYLES } = require('./styles');
|
|
|
|
const STYLE_NAMES = Object.keys(STYLES);
|
|
|
|
/**
|
|
* @param {{title:string, subtitle?:string, author?:string, tags?:string[], slug?:string,
|
|
* style?:string, width?:number, height?:number, background?:string}} input
|
|
* @returns {string} SVG-документ
|
|
*/
|
|
function render(input) {
|
|
const title = String(input.title ?? '').trim();
|
|
if (!title) throw new Error('title is required');
|
|
|
|
const seed = String(input.slug || title).toLowerCase();
|
|
const styleName = input.style && input.style !== 'auto'
|
|
? input.style
|
|
: STYLE_NAMES[hash(seed) % STYLE_NAMES.length];
|
|
const style = STYLES[styleName];
|
|
if (!style) {
|
|
throw new Error(`unknown style "${styleName}", available: ${STYLE_NAMES.join(', ')}`);
|
|
}
|
|
|
|
const width = Math.max(320, Math.min(4000, Number(input.width) || 1200));
|
|
const height = Math.max(320, Math.min(4000, Number(input.height) || 630));
|
|
const tags = Array.isArray(input.tags)
|
|
? input.tags.map((tag) => String(tag).trim()).filter(Boolean)
|
|
: String(input.tags ?? '').split(',').map((tag) => tag.trim()).filter(Boolean);
|
|
|
|
const body = style({
|
|
width,
|
|
height,
|
|
title,
|
|
subtitle: input.subtitle ?? '',
|
|
author: input.author ?? '',
|
|
tags,
|
|
palette: paletteFor(seed),
|
|
uid: (hash(seed) % 99991).toString(36),
|
|
});
|
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img" aria-label="${escapeAttr(title)}">
|
|
${body}
|
|
</svg>
|
|
`;
|
|
}
|
|
|
|
function escapeAttr(value) {
|
|
return String(value).replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"');
|
|
}
|
|
|
|
module.exports = { render, STYLE_NAMES };
|