# core/selectors/document.selector.ts

> 19 lines of code and 7 definitions.

Tree: GovLab Context
Language: typescript
Layer: application
Canonical: https://banes-lab.com/anatomy/context#file-context-core-selectors-document-selector-ts
Source text: https://banes-lab.com/source/context/core/selectors/document.selector.ts.txt

Listed in [core/selectors](https://banes-lab.com/api/source/context/core/selectors.md), after [core/selectors/check.selector.ts](https://banes-lab.com/source/context/core/selectors/check.selector.ts.md) and before [core/selectors/field.selector.ts](https://banes-lab.com/source/context/core/selectors/field.selector.ts.md).

## Definitions

- `pagBlockOf` (lexical_declaration, line 9, exported)
- `pagTextOf` (lexical_declaration, line 20, exported)
- `isPagFenceOpen` (lexical_declaration, line 5)
- `lines` (lexical_declaration, line 10, exported)
- `open` (lexical_declaration, line 11, exported)
- `close` (lexical_declaration, line 15, exported)
- `body` (lexical_declaration, line 16, exported)

## Contained in

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

## Linked from

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

## Source

```typescript
import type { DocumentSource, PagText } from "#types/grammar.document.types";
import { FENCE, FIRST_LINE, LINE_BREAK, PAG_FENCE_INFO } from "#configuration/constants/document.constants";
import { splitLines } from "#core/converters/text.converter";

const isPagFenceOpen = function isPagFenceOpen(trimmed: string): boolean {
    return trimmed.startsWith(FENCE) && trimmed.includes(PAG_FENCE_INFO);
};

export const pagBlockOf = function pagBlockOf(text: string): PagText | null {
    const lines = splitLines(text);
    const open = lines.findIndex((line) => isPagFenceOpen(line.trim()));
    if (open === -1) {
        return null;
    }
    const close = lines.findIndex((line, index) => index > open && line.trim().startsWith(FENCE));
    const body = lines.slice(open + 1, close === -1 ? lines.length : close);
    return { firstLine: open + 2, text: body.join(LINE_BREAK) };
};

export const pagTextOf = function pagTextOf(source: DocumentSource, text: string): PagText | null {
    return source.kind === "template" ? pagBlockOf(text) : { firstLine: FIRST_LINE, text };
};
```
