UNPKG

@rr0/cms

Version:

RR0 Content Management System (CMS)

92 lines (91 loc) 3.99 kB
import path from "path"; import { Gender } from "@rr0/common"; import { AbstractDataFactory } from "@rr0/data"; export class PeopleHtmlRenderer { renderLink(context, people, pseudoPeopleList, allCountries, occupations, filterOccupations = [], content) { var _a, _b, _c; const dirName = people.dirName; const events = people.events; const titles = []; const classList = ["data-resolved", "people-resolved"]; if (pseudoPeopleList.indexOf(people) >= 0 || people.pseudonyms.includes(content)) { classList.push("pseudonym"); titles.push(`(pseudonyme de ${people.firstAndLastName})`); } if (people.hoax) { classList.push("canular"); } const birthTimeStr = (_a = people.birthTime) === null || _a === void 0 ? void 0 : _a.year.toString(); const deathTimeStr = (_b = people.deathTime) === null || _b === void 0 ? void 0 : _b.year.toString(); if (people.isDeceased()) { classList.push("deceased"); } if (birthTimeStr || deathTimeStr) { const timeStr = birthTimeStr ? deathTimeStr ? `${birthTimeStr}-${deathTimeStr}` : `${birthTimeStr}-` : `-${deathTimeStr}`; titles.push(timeStr); } const age = people.getAge(); if (age) { titles.push(`${age} ans`); } const countries = people.countries; if (countries) { for (const country of countries) { allCountries.add(country); const countryLabel = (_c = context.messages.country[country]) === null || _c === void 0 ? void 0 : _c.title; if (!countryLabel) { throw new Error(`No title for country "${country}"`); } titles.push(countryLabel); classList.push(`country-${country}`); } } const gender = people.gender || Gender.male; for (const occupation of people.occupations) { if (filterOccupations.length > 1 || !filterOccupations.includes(occupation)) { occupations.add(occupation); const occupationMsg = context.messages.people.occupation[occupation]; if (!occupationMsg) { throw Error(`No message to translate occupation "${occupation}" in ${context.locale}, as specified in ${people.dirName}/people*.json`); } classList.push(`occupation-${occupation}`); titles.push(occupationMsg(gender)); } } const text = content || people.lastAndFirstName || people.title; const doc = context.file.document; const link = doc.createElement("a"); link.innerHTML = text; link.href = `/${dirName}/`; if (people.discredited) { link.append(" 🤥"); titles.push("discrédité"); } const elem = doc.createElement("span"); if (titles.length > 0) { elem.title = titles.join(", "); } if (classList.length > 0) { elem.classList.add(...classList); } let portraitUrl = people.image; const imageEvents = events.filter(event => event.eventType === "image"); if (!portraitUrl) { const portraitEvent = imageEvents.find(event => AbstractDataFactory.defaultPreviewFileNames.includes(event.url)); if (portraitEvent) { portraitUrl = path.join("/", people.dirName, portraitEvent.url); } } if (portraitUrl) { const portraitElem = doc.createElement("img"); portraitElem.src = path.join("/", portraitUrl); portraitElem.alt = text; portraitElem.className = "portrait"; portraitElem.loading = "lazy"; portraitElem.width = 75; link.append(portraitElem); } elem.append(link); return elem; } }