/** * Which tools a page's Related tools block links to, worked out from data * passed in rather than imported, so the page (through tool-groups.ts), the * unit test and scripts/link-report.mjs all run this one function. * * Families first. A tool's families (tool-families.json) are the pages a * reader on it most likely wants next, across categories. Its category is * twenty tools wide, and taking the first six of it - what this used to do - * meant the same handful of pages linked from everywhere and the tools at the * end of a long category linked from nowhere. * * A tool in two families takes one sibling from each in turn, so the second * family is not pushed off the end by the first. When the families give fewer * than MIN_LINKS, the list is topped up from the tool's category only if that * category is one theme all through (topUpFrom in tool-families.json: PDF, * video, email). A mixed category would add strangers - the Font Inspector * ending with the DWG, 3D and SketchUp viewers - so there a page shows the * three to five links its families give and no more. * * Everything is in a fixed order, never shuffled: a link block that changes on * every render is a different page each time a crawler fetches it, and the * server-rendered HTML would not match the browser's. */ /** Most links one page shows. */ export const MAX_LINKS = 8; /** What a topped-up page is filled to, from a single-theme category. */ export const MIN_LINKS = 6; export interface RelatedData { groups: { slug: string; tools: string[] }[]; families: { tools: string[] }[]; /** Category slugs whose tools are all close enough to top a short list up from. */ topUpFrom: string[]; /** True if the page is indexable or parked: links go only to, and only from, pages that are. */ index: (path: string) => boolean; } export function relatedFor(key: string, d: RelatedData): string[] { if (!d.index(key)) return []; const picked: string[] = []; const add = (path: string) => { if (path !== key && !picked.includes(path) && d.index(path)) picked.push(path); }; const lists = d.families.filter((f) => f.tools.includes(key)).map((f) => f.tools); const longest = Math.max(0, ...lists.map((l) => l.length)); for (let i = 0; i < longest && picked.length < MAX_LINKS; i++) { for (const list of lists) if (i < list.length && picked.length < MAX_LINKS) add(list[i]); } const group = d.groups.find((g) => g.tools.includes(key)); if (group && d.topUpFrom.includes(group.slug)) { for (const path of group.tools) { if (picked.length >= MIN_LINKS) break; add(path); } } return picked; }