domain/converters/search.index.converter.ts
domain/converters/search.index.converter.ts is a file in Bane's Lab Site. 70 lines of code and 16 definitions.
import type {
PositionSources,
SearchCorpus,
SearchIndex,
SearchPage,
SearchPositions,
SearchSource,
} from "#types/search.types";
import { anchorPath, anchorsOf, evidenceOf, linksOf, placeOf, routeOf } from "#core/analyzers/search.analyzer";
import type { Section } from "#types/document.types";
import { TITLE_SEPARATOR } from "#configuration/strings/page.strings";
import type { TabbedContent } from "#types/page.types";
const tabbedSources = function tabbedSources(page: SearchPage, content: TabbedContent): SearchSource[] {
return content.tabs.flatMap((tab, index) => {
const layout = tab.layout ?? content.layout;
if (layout === "tree") {
return [];
}
const label = page.definition.title + TITLE_SEPARATOR + tab.label;
return [
{ first: index === 0, icon: tab.icon, label, layout, page: page.definition.id, tab, tone: content.tone },
];
});
};
export const sourcesOf = function sourcesOf(page: SearchPage): readonly SearchSource[] {
const { content, id, title } = page.definition;
if (content.kind === "tabbed") {
return tabbedSources(page, content);
}
if (content.kind === "home") {
return [];
}
const tab = { icon: page.icon, id, label: content.meta.title, sections: content.sections };
return [{ first: true, icon: page.icon, label: title, layout: "document", page: id, tab, tone: null }];
};
const firstTabsOf = function firstTabsOf(pages: readonly SearchPage[]): ReadonlyMap<string, string> {
return new Map(
pages.flatMap(({ definition }) => {
const first = definition.content.kind === "tabbed" ? definition.content.tabs[0] : undefined;
return first === undefined ? [] : [[definition.id, first.id] as const];
}),
);
};
export const positionsOf = function positionsOf(input: PositionSources): SearchPositions {
const route = routeOf(input.route);
return {
evidence: Object.fromEntries(evidenceOf(input.evidence, route, firstTabsOf(input.pages))),
links: Object.fromEntries(linksOf(input.pages.flatMap(sourcesOf), route)),
route: Object.fromEntries(route),
};
};
const heldIndexes = new WeakMap<SearchCorpus, SearchIndex>();
export const indexOf = function indexOf(corpus: SearchCorpus): SearchIndex {
const held = heldIndexes.get(corpus);
if (held !== undefined) {
return held;
}
const { positions } = corpus;
const index = {
evidence: new Map(Object.entries(positions.evidence)),
links: new Map(Object.entries(positions.links)),
route: new Map(Object.entries(positions.route)),
sources: corpus.pages.flatMap(sourcesOf),
};
heldIndexes.set(corpus, index);
return index;
};
export const positionOf = function positionOf(index: SearchIndex, source: SearchSource, section: Section): number {
return index.route.get(anchorPath(source, section.id)) ?? placeOf(anchorsOf(section), index.links);
};