4cb6323e4c
- tools/covergen из ветки covergen-tooling + флаг --palette auto|dedinit-dark (цвета theme-vars/hero.svg) - scripts/gen-covers.mjs: SVG в static/covers (в git), PNG — артефакт сборки (gitignore), --png/--force/--only - rss.xml: <enclosure> с covers/<slug>.png при fileExists, <language>ru</language>, описание канала по-русски - hugo.toml: limit 5 → 20, enableRobotsTXT - deploy.yml: nodejs + librsvg2-bin, шаг генерации обложек перед hugo - docs: DZEN_RSS (итог), covergen (палитра, SVG/PNG, питфолл snap-chromium), вики reference-covers/reference-rss-dzen - openspec change dzen-rss-compliance
55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const { paletteFor, paletteByName, 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: paletteByName(input.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 };
|