@functional-examples/documentation
Documentation generation plugin for functional-examples
npm install @functional-examples/documentation@functional-examples/documentation
Documentation generation plugin for functional-examples.
Installation
npm install @functional-examples/documentation
Overview
This plugin generates documentation from your scanned examples using customizable templates. It supports both Markdown and Markdoc output formats, with Eta-powered template rendering.
Usage
import { createDocumentationPlugin } from '@functional-examples/documentation';
import { createJavaScriptPlugin } from '@functional-examples/javascript';
import type { Config } from 'functional-examples';
/**
* Configuration demonstrating the documentation plugin.
*
* The documentation plugin:
* - Adds the `generate` CLI command
* - Enables template-based doc generation
* - Provides prose helpers (file(), region(), fencedBlock())
*/
const config: Config = {
plugins: [
createJavaScriptPlugin(),
createDocumentationPlugin({
outputDir: 'generated-docs',
format: 'markdown',
}),
],
scan: {
include: ['src/**/*'],
exclude: ['**/node_modules/**', '**/dist/**'],
},
};
export default config;
Features
- Template-based generation: Eta templates for full control over output
- Per-example templates: Override the template for specific examples
- Prose file rendering: README.md files in examples are rendered with helper expansion
- Region references: Use
file()andregion()helpers in prose to embed code - Guide hydration: Expand cross-example references in standalone guide documents
- Check mode: CI-friendly
--checkflag to verify docs are up-to-date - Multiple formats: Markdown and Markdoc output
Template Helpers
Inside prose files (README.md in examples), use:
<%= file('utils.ts') %> <% /* Embed a file as a fenced code block */ %>
<%= region('setup') %> <% /* Embed a code region */ %>
Inside guide documents, reference any example:
<%= example('basic-usage').file('scan.ts') %>
<%= example('basic-usage').region('scan') %>
License
MIT
API EXPORTS
Functions
buildTemplateDatafunctionBuild the template data model from an Example.
createDocumentationPluginfunctionCreate the documentation plugin.
createFileAccessorfunctionCreate a FileAccessor for a specific file.
createGuideRendererfunctionCreate a guide renderer bound to a set of scanned examples. Guide templates can reference any example by ID: ```markdown <%= example('basic-usage').file('scan.ts') %> ```
createMarkdownExtractorfunctionCreate a markdown frontmatter extractor. Scans for .md files with YAML frontmatter containing at least `id` and `title`. Treats each matching file as a single-file example.
createProseHelpersfunctionCreate prose-template helpers bound to a specific set of files and tracker. These helpers are injected into Eta prose templates via `functionHeader` so authors can write `<%= file('utils.ts') %>` instead of `<%= it.file('utils.ts') %>`.
getBuiltinTemplatesDirfunctionResolve the built-in templates directory relative to this file. At runtime, this file lives at `dist/templates/engine.js`. The templates directory is at `packages/documentation/templates/` — two levels up from `dist/templates/`.
loadTemplatesfunctionLoad all `.template` files from a directory (recursively) and parse their paths. Supports nested directory structures like: ``` templates/ index.md.template examples/@example/index.md.template ```
parseTemplateNamefunctionParse a template's relative path into its descriptor fields. The `@var` convention can appear in any path segment — either in the filename itself or in a directory name: - `@slug.md.template` → variable `slug` (flat, filename) - `examples/@example/index.md.template` → variable `example` (nested, directory)
renderIndexTemplatefunctionRender an aggregate template with all examples.
renderItemTemplatefunctionRender a per-item template for a single example.
renderProseFilesfunctionRender prose files through Eta, expanding `file()` and `region()` helpers, and return which files remain unconsumed.
renderTemplatefunctionRender an example using the default or a custom template file.
substituteVarsfunctionReplace `@var` placeholders in a pattern with values from a vars map. Example: `substituteVars('@slug.md', { slug: 'getting-started' })` → `getting-started.md`
Interfaces
DocsMetadatainterfaceDocumentation-specific metadata that examples can declare.
ExampleAccessorinterfaceScoped API returned by `example(id)` inside guide templates.
FileAccessorinterfaceChainable accessor returned by `file()` in both prose and guide contexts. Supports two usage patterns: - `<%= file('path.ts') %>` — renders the whole file (calls `toString()`) - `<%= file('path.ts').region('id') %>` — renders a scoped region
GuideRendererinterfaceA renderer that expands example references in guide markdown.
GuideRendererOptionsinterfaceOptions for createGuideRenderer.
IndexTemplateDatainterfaceData model for aggregate (index) templates.
ProseHelpersinterfaceHelpers injected into prose Eta templates as top-level variables.
ProseRenderResultinterfaceResult of rendering prose files through Eta.
RenderedFileinterfaceRendered output ready to write.
TemplateDatainterfaceTemplate data model passed to Eta per-item templates as `it`.
TemplateDescriptorinterfaceParsed template file descriptor.