domain/converters/source.converter.ts

domain/converters/source.converter.ts is a file in Bane's Lab Site. 49 lines of code and 17 definitions.

import { EXTENSION_SEPARATOR, LANGUAGE_BY_EXTENSION, LINE_INFIX } from "#configuration/constants/anatomy.constants";
import { FILE_ANCHOR, FOLDER_ANCHOR, TREE_TAB } from "#core/ids/anatomy.ids";
import { SITE_NAME, TITLE_SEPARATOR } from "#configuration/strings/page.strings";
import { localPath, treeOf } from "#core/converters/folder.converter";
import { nodePath, tabLink } from "#core/assets/link.assets";
import { ANATOMY_PAGE } from "#core/ids/page.ids";
import { ROOT_LABEL } from "#configuration/strings/folder.strings";
import { TEXT_LANGUAGE } from "#configuration/constants/code.constants";
import { slugOf } from "#domain/converters/ontology.converter";
import { treeDeclarationOf } from "#core/registries/anatomy.tree.registry";
import { undeclaredTree } from "#configuration/strings/report.strings";

export const fileId = function fileId(path: string): string {
    return FILE_ANCHOR + slugOf(path);
};

export const nodeHref = function nodeHref(file: string, line: number | null): string {
    const suffix = line === null ? "" : LINE_INFIX + String(line);
    return tabLink(ANATOMY_PAGE, treeOf(file), fileId(file) + suffix);
};

export const folderId = function folderId(path: string): string {
    return FOLDER_ANCHOR + slugOf(path === "" ? ROOT_LABEL : path);
};

export const folderHref = function folderHref(path: string): string {
    return tabLink(ANATOMY_PAGE, treeOf(path), folderId(path));
};

export const treeLabelOf = function treeLabelOf(tab: string): string {
    const declaration = treeDeclarationOf(tab);
    if (declaration === undefined) {
        throw new Error(undeclaredTree(tab));
    }
    return declaration.label;
};

export const pathLabel = function pathLabel(path: string): string {
    const tree = treeOf(path);
    return tree === TREE_TAB ? path : treeLabelOf(tree) + TITLE_SEPARATOR + localPath(path);
};

export const nodeRoute = function nodeRoute(path: string, folder: boolean): string {
    return nodePath(ANATOMY_PAGE, treeOf(path), folder ? folderId(path) : fileId(path));
};

export const sourceTitle = function sourceTitle(path: string): string {
    const local = localPath(path);
    const named = local.length === 0 ? ROOT_LABEL : local;
    const label = treeLabelOf(treeOf(path));
    const title = label + TITLE_SEPARATOR + named;
    return label.includes(SITE_NAME) ? title : title + TITLE_SEPARATOR + SITE_NAME;
};

export const languageOf = function languageOf(name: string): string {
    const extension = name.slice(name.lastIndexOf(EXTENSION_SEPARATOR) + 1);
    return LANGUAGE_BY_EXTENSION.get(extension) ?? TEXT_LANGUAGE;
};