dika/public/card-sources.js
2026-11-27 12:16:12 +00:00

26 lines
1.2 KiB
JavaScript

// Подписи «Описание: …» и «Фото: …» на карточке (DIK-71). Чистая функция: общий для браузера и тестов.
// Возвращает две строки одного вида: { cls, text, name?, href? } — name выводится ссылкой на href.
const OWNER = "владелец";
function parse(source) {
const m = /^(.*?)\s*(https?:\/\/\S+)$/.exec(source);
const name = (m ? m[1] : source).replace(/[\s—-]+$/, "").trim();
return { name: name || (m && m[2]) || "", href: m ? m[2] : null };
}
function line(kind, has, source, t) {
const cls = kind === "desc" ? "src-desc" : "src-photo";
const k = kind === "desc" ? "srcDesc" : "srcPhoto";
if (!has) return { cls, text: t[k + "None"] };
if (!source || source.trim() === OWNER) return source ? { cls, text: t[k + "Owner"] } : { cls, text: t[k + "Unknown"] };
const { name, href } = parse(source);
return { cls, text: t[k + "By"] + " ", name, href };
}
export function cardSources(v, t, { photoFailed = false } = {}) {
return [
line("desc", !!v.description, v.descriptionSource, t),
line("photo", !!v.photo && !photoFailed, v.photoSource, t),
];
}