# core/formatters/figure.formatter.ts

> 55 lines of code and 11 definitions.

Tree: GovLab Docs
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/docs#file-docs-core-formatters-figure-formatter-ts
Source text: https://banes-lab.com/source/docs/core/formatters/figure.formatter.ts.txt

Listed in [core/formatters](https://banes-lab.com/api/source/docs/core/formatters.md), after [core/formatters/document.formatter.ts](https://banes-lab.com/source/docs/core/formatters/document.formatter.ts.md) and before [core/formatters/finding.formatter.ts](https://banes-lab.com/source/docs/core/formatters/finding.formatter.ts.md).

## Definitions

- `mermaidBlock` (lexical_declaration, line 14)
- `partBlocks` (lexical_declaration, line 18)
- `legendBlock` (lexical_declaration, line 26)
- `renderCharts` (lexical_declaration, line 56, exported)
- `sourcesBlock` (lexical_declaration, line 30)
- `chartBlock` (lexical_declaration, line 44)
- `MERMAID_OPEN` (lexical_declaration, line 11)
- `FENCE_CLOSE` (lexical_declaration, line 12)
- `parts` (lexical_declaration, line 19)
- `rendered` (lexical_declaration, line 45)
- `blocks` (lexical_declaration, line 57, exported)

## Contained in

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

## Uses

- [configuration/strings/figure.strings.ts](https://banes-lab.com/source/docs/configuration/strings/figure.strings.ts.md)

## Used by

- [core/coordinators/figure.coordinator.ts](https://banes-lab.com/source/docs/core/coordinators/figure.coordinator.ts.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/docs/folder-docs-configuration-strings.md)
- [core/coordinators](https://banes-lab.com/anatomy/docs/folder-docs-core-coordinators.md)
- [core/formatters](https://banes-lab.com/anatomy/docs/folder-docs-core-formatters.md)

## Source

````typescript
import type { DiagramContext, DiagramKind, RenderedDiagram } from "#types/figure.types";
import {
    SOURCES_CLOSE,
    SOURCES_SUMMARY,
    SOURCES_TABLE_HEAD,
    chartsHeading,
    legendLine,
} from "#configuration/strings/figure.strings";
import { DIAGRAM_KINDS } from "#core/loaders/diagram.loader";

const MERMAID_OPEN = "```mermaid";
const FENCE_CLOSE = "```";

const mermaidBlock = function mermaidBlock(mermaid: string): string[] {
    return [MERMAID_OPEN, mermaid, FENCE_CLOSE, ""];
};

const partBlocks = function partBlocks(rendered: RenderedDiagram): string[] {
    const parts = rendered.parts ?? [];
    if (parts.length === 0) {
        return mermaidBlock(rendered.mermaid);
    }
    return parts.flatMap((part) => [`### ${part.caption}`, "", ...mermaidBlock(part.mermaid)]);
};

const legendBlock = function legendBlock(rendered: RenderedDiagram): string[] {
    return rendered.legend.length > 0 ? [legendLine(rendered.legend), ""] : [];
};

const sourcesBlock = function sourcesBlock(rendered: RenderedDiagram): string[] {
    if (rendered.sources.length === 0) {
        return [];
    }
    return [
        SOURCES_SUMMARY,
        "",
        ...SOURCES_TABLE_HEAD,
        ...rendered.sources.map((source) => `| ${source.label} | \`${source.file}:${source.line}\` |`),
        "",
        SOURCES_CLOSE,
    ];
};

const chartBlock = function chartBlock(kind: DiagramKind, context: DiagramContext): string[] {
    const rendered = kind.appliesTo(context) ? kind.render(context) : null;
    if (rendered === null) {
        return [];
    }
    return [
        [`## ${kind.title}`, "", ...partBlocks(rendered), ...legendBlock(rendered), ...sourcesBlock(rendered)].join(
            "\n",
        ),
    ];
};

export const renderCharts = function renderCharts(context: DiagramContext): string | null {
    const blocks = DIAGRAM_KINDS.flatMap((kind) => chartBlock(kind, context));
    if (blocks.length === 0) {
        return null;
    }
    return `${chartsHeading(context.moduleName)}\n\n${blocks.join("\n\n")}\n`;
};
````
