# core/loaders/section.loader.ts

> 38 lines of code and 8 definitions.

Tree: Bane's Lab Content
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/content#file-content-core-loaders-section-loader-ts
Source text: https://banes-lab.com/source/content/core/loaders/section.loader.ts.txt

Listed in [core/loaders](https://banes-lab.com/api/source/content/core/loaders.md), after [core/loaders/readme.loader.ts](https://banes-lab.com/source/content/core/loaders/readme.loader.ts.md) and before [core/loaders/seed.loader.ts](https://banes-lab.com/source/content/core/loaders/seed.loader.ts.md).

## Definitions

- `packageSections` (lexical_declaration, line 32, exported)
- `PAGE_IDS_MODULE` (lexical_declaration, line 12)
- `sitePages` (lexical_declaration, line 14, exported)
- `ids` (lexical_declaration, line 15, exported)
- `readSections` (lexical_declaration, line 21, exported)
- `found` (lexical_declaration, line 25, exported)
- `modules` (lexical_declaration, line 33, exported)
- `readme` (lexical_declaration, line 40, exported)

## Contained in

- [core/loaders](https://banes-lab.com/anatomy/content/folder-content-core-loaders.md)

## Uses

- [core/converters/section.converter.ts](https://banes-lab.com/source/content/core/converters/section.converter.ts.md)

## Linked from

- [core/converters](https://banes-lab.com/anatomy/content/folder-content-core-converters.md)

## Source

```typescript
import { COORDINATION_PAGE, README_FILE } from "#configuration/constants/writing.constants";
import { markdownSectionsOf, moduleSectionOf, sectionsOf } from "#core/converters/section.converter";
import { packageFiles, packagePath } from "#core/loaders/coordination.loader";
import type { PageSection } from "#types/writing.types";
import { STRINGS_SUFFIX } from "#configuration/constants/tone.constants";
import { UTF8 } from "#configuration/constants/source.constants";
import { join } from "node:path";
import { literalsOf } from "#core/loaders/strings.loader";
import { readFileSync } from "node:fs";
import { readPageContent } from "#core/loaders/payload.loader";

const PAGE_IDS_MODULE = "@banes-lab/web/core/ids/page.ids.ts";

export const sitePages = async function sitePages(): Promise<readonly string[]> {
    const ids: unknown = await import(PAGE_IDS_MODULE);
    return typeof ids === "object" && ids !== null
        ? Object.values(ids).filter((id: unknown): id is string => typeof id === "string")
        : [];
};

export const readSections = function readSections(pages: readonly string[]): {
    readonly missing: readonly string[];
    readonly sections: readonly PageSection[];
} {
    const found = pages.map((page) => ({ content: readPageContent(page), page }));
    return {
        missing: found.filter((entry) => entry.content === null).map((entry) => entry.page),
        sections: found.flatMap((entry) => (entry.content === null ? [] : sectionsOf(entry.page, entry.content))),
    };
};

export const packageSections = async function packageSections(root: string): Promise<readonly PageSection[]> {
    const modules = await Promise.all(
        packageFiles(root)
            .filter((file) => file.endsWith(STRINGS_SUFFIX))
            .map(async (file) =>
                moduleSectionOf(COORDINATION_PAGE, packagePath(root, file), await literalsOf(readFileSync(file, UTF8))),
            ),
    );
    const readme = readFileSync(join(root, README_FILE), UTF8);
    return [...markdownSectionsOf(COORDINATION_PAGE, README_FILE, readme), ...modules];
};
```
