core/converters/section.page.converter.ts

core/converters/section.page.converter.ts is a file in Bane's Lab Build Scripts. 40 lines of code and 9 definitions.

import { SECTION_KIND, partLabel, partTitle, sectionDescription } from "#configuration/strings/page.strings";
import { SITE_NAME, TITLE_SEPARATOR } from "@banes-lab/web/strings/page.strings";
import type { SectionSeed, SectionSubject } from "@banes-lab/web/types/page.types.js";
import { nodePath, sectionLeafPath, sectionMarkdownPath } from "@banes-lab/web/core/assets/link.assets.ts";
import { partPathOf } from "#core/converters/source.segment.converter";

export const sectionSubjectOf = function sectionSubjectOf(seed: SectionSeed, qualified = false): SectionSubject {
    const base = nodePath(seed.page, seed.tab, seed.section);
    const heading = qualified ? [seed.name, seed.kind ?? SECTION_KIND] : [seed.name];
    const title = [...heading, seed.tabTitle, SITE_NAME].join(TITLE_SEPARATOR);
    const description = sectionDescription(seed.name, seed.records, seed.tabTitle);
    const first = seed.part === 1;
    return {
        alternates: {
            json: sectionLeafPath(seed.page, seed.tab, seed.section),
            markdown: sectionMarkdownPath(seed.page, seed.tab, seed.section),
        },
        description: first ? description : partTitle(description, seed.part, seed.parts),
        name: seed.name,
        parts: Array.from({ length: seed.parts }, (_, index) => ({
            label: partLabel(index + 1, seed.parts),
            path: partPathOf(base, index),
        })),
        path: partPathOf(base, seed.part - 1),
        seed,
        title: first ? title : partTitle(title, seed.part, seed.parts),
    };
};

export const distinctSections = function distinctSections(
    seeds: readonly SectionSeed[],
    taken: ReadonlySet<string>,
): readonly SectionSubject[] {
    const plain = seeds.map((seed) => ({ seed, subject: sectionSubjectOf(seed) }));
    const counts = plain.reduce(
        (held, { subject }) => held.set(subject.title, (held.get(subject.title) ?? 0) + 1),
        new Map<string, number>(),
    );
    return plain.map(({ seed, subject }) =>
        taken.has(subject.title) || (counts.get(subject.title) ?? 0) > 1 ? sectionSubjectOf(seed, true) : subject,
    );
};