core/factories/schema.factory.ts

core/factories/schema.factory.ts is a file in Bane's Lab Site. 48 lines of code and 7 definitions.

import type { Crumb, SchemaEntity } from "#types/schema.types";
import { ORGANIZATION_ID, PERSON_ID, SCHEMA_LANGUAGE, WEBSITE_ID } from "#configuration/constants/schema.constants";
import type { PageDates, SchemaSubject } from "#types/page.types";
import { JSON_FORMAT } from "#configuration/constants/code.constants";
import { SITE_URL } from "#core/assets/link.assets";

const MARKDOWN_FORMAT = "text/markdown";
const DAY_START = "T00:00:00+00:00";

export const breadcrumb = function breadcrumb(crumbs: readonly Crumb[]): SchemaEntity {
    return {
        "@type": "BreadcrumbList",
        itemListElement: crumbs.map((crumb, index) => ({
            "@type": "ListItem",
            item: SITE_URL + crumb.path,
            name: crumb.name,
            position: index + 1,
        })),
    };
};

export const encoding = function encoding(json: string, markdown: string): readonly SchemaEntity[] {
    return [
        { "@type": "MediaObject", contentUrl: SITE_URL + json, encodingFormat: JSON_FORMAT },
        { "@type": "MediaObject", contentUrl: SITE_URL + markdown, encodingFormat: MARKDOWN_FORMAT },
    ];
};

export const instantOf = function instantOf(day: string): string {
    return day + DAY_START;
};

export const datesOf = function datesOf(dates: PageDates | undefined): SchemaEntity {
    return dates === undefined
        ? {}
        : { dateModified: instantOf(dates.modified), datePublished: instantOf(dates.published) };
};

export const basePage = function basePage(
    subject: Pick<SchemaSubject, "dates" | "description" | "path" | "title">,
): SchemaEntity {
    return {
        "@id": SITE_URL + subject.path,
        "@type": "WebPage",
        author: { "@id": PERSON_ID },
        ...datesOf(subject.dates),
        description: subject.description,
        inLanguage: SCHEMA_LANGUAGE,
        isPartOf: { "@id": WEBSITE_ID },
        name: subject.title,
        publisher: { "@id": ORGANIZATION_ID },
        url: SITE_URL + subject.path,
    };
};