import { ANATOMY_FACE, CHAPTER_FACE } from "#configuration/constants/vocabulary.constants"; import type { ReferenceContent, ReferenceTarget } from "#types/reference.types"; import { chapterOf, payloadOfHref } from "#domain/converters/reference.converter"; import { ANCHOR_PREFIX } from "#configuration/constants/document.constants"; import { loadPayload } from "#core/loaders/payload.loader"; import { loadReferences } from "#assets/reference.generated"; const fragmentOf = function fragmentOf(href: string): string { return href.slice(href.indexOf(ANCHOR_PREFIX) + ANCHOR_PREFIX.length); }; const loadContent = async function loadContent( target: ReferenceTarget, href: string, ): Promise { if (target.face === CHAPTER_FACE) { const chapter = chapterOf(await loadPayload(payloadOfHref(href)), fragmentOf(href)); return chapter === null ? null : { chapter, kind: "chapter" }; } if (target.face === ANATOMY_FACE) { const { anatomyReference } = await import("#domain/loaders/anatomy.loader"); const record = anatomyReference(fragmentOf(href)); return record === null ? null : { kind: "record", record }; } const index = await loadReferences(target.face); const record = index?.[target.ref]; return record === undefined ? null : { kind: "record", record }; }; export const loadReference = async function loadReference( target: ReferenceTarget, href: string, ): Promise { const content = await loadContent(target, href); if (content === null) { return null; } const { withEvidence } = await import("#domain/loaders/evidence.loader"); return withEvidence(target, content); };