# domain/loaders/reference.loader.ts

> 37 lines of code and 9 definitions.

Tree: Site tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/tree#file-domain-loaders-reference-loader-ts
Source text: https://banes-lab.com/assets/sources/source.4ceaae22a4303ca83cea5ec2de9f173e69bedb12cae91c78e3b8726d0e5c645c.generated.txt

## Definitions

- `loadReference` (lexical_declaration, line 30, exported)
- `fragmentOf` (lexical_declaration, line 8)
- `loadContent` (lexical_declaration, line 12)
- `chapter` (lexical_declaration, line 17)
- `{ anatomyReference }` (lexical_declaration, line 21)
- `index` (lexical_declaration, line 25)
- `record` (lexical_declaration, line 26)
- `content` (lexical_declaration, line 34, exported)
- `{ withEvidence }` (lexical_declaration, line 38, exported)

## Uses

- [domain/loaders/evidence.loader.ts](https://banes-lab.com/source/tree/domain/loaders/evidence.loader.ts.md)

## Source

```typescript
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<ReferenceContent | null> {
    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<ReferenceContent | null> {
    const content = await loadContent(target, href);
    if (content === null) {
        return null;
    }
    const { withEvidence } = await import("#domain/loaders/evidence.loader");
    return withEvidence(target, content);
};
```
