# Literate Markdown & Ontology Export Forma treats Markdown (`.md`, `.forma.md`, `.markdown`) as a first-class authoring and ontology projection format alongside `.lisp`. You can write prose, tables, diagrams, and executable Forma definitions in the same document, check and format them with exact `.md` line/column diagnostics, and round-trip them cleanly through the content-addressed transactional store. ## Fenced Block Rules Forma extracts executable code strictly from fenced code blocks whose language tag is `forma` or `lisp` (case-insensitive). Both backtick (```` ``` ````) and tilde (`~~~`) fences of three or more characters are supported. | Fence header | Extracted & checked? | Notes | | --- | --- | --- | | ```` ```forma ```` / ```` ```lisp ```` | Yes | Concatenated in document order with exact source-span mapping. | | `~~~forma` / `~~~lisp` | Yes | Equivalent to backtick fences. | | ```` ```forma ignore ```` | No | Opt-out tokens `ignore`, `no-check`, and `skip` exclude the block. | | ```` ```json ````, ```` ```ts ````, ```` ```sh ```` | No | Non-Forma fences are left untouched. | | Untyped ```` ``` ```` or prose | No | Ignored semantically and preserved verbatim. | An ontology document can mix YAML frontmatter, narrative prose, reference payloads, and executable Forma blocks: ````markdown --- id: hr version: 0.1.0 --- # HR Ontology Candidate and offer definitions for recruiting workflows. ```forma (entity Candidate {:name String :email (Option String) :status String}) (entity Offer {:title String :status String :candidate (Option (Id Candidate))}) ``` Example webhook payload (ignored by Forma): ```json {"event": "offer.accepted", "candidate": "candidate:taylor"} ``` Draft scratchpad (opted out of checking): ```forma ignore (define (wip-experiment x) x) ``` ```forma (query active-offers :from Offer :select [title status candidate]) ``` ```` ## CLI, Modules & Exact `.md` Diagnostics All core CLI commands accept `.md` and `.forma.md` files natively: ```sh forma check ontology.md forma run ontology.md forma test ontology.md forma fmt ontology.md ``` - **Exact `.md` coordinates:** Parse, typecheck, and form-hook diagnostics across multiple fenced blocks are remapped back to the exact 1-based `line:col` location inside the `.md` file. The language server remaps diagnostics, hover ranges, and formatting edits the same way. - **Lossless formatting:** `forma fmt ontology.md` formats only the executable `forma`/`lisp` blocks and splices them back into place without modifying surrounding prose, headings, frontmatter, or non-Forma fences. - **Module imports:** Relative imports resolve `.md` modules alongside `.lisp` files, extracting their executable fences and remapping any module diagnostics to the imported `.md` file. ## Transactional Store & Markdown Ontology Export The content-addressed store (`forma store`) supports Markdown as a primary human- and agent-reviewable projection format: ```sh # Initialize a store from .md or .lisp files forma store init ontology.md # Export the current snapshot to reviewable Markdown documents forma store export --out ./exported --format md # Re-import edited Markdown files as a minimal semantic transaction forma store import ./exported --branch main ``` ### Generated Markdown Structure When exporting a fresh snapshot with `--format md` (or `exportSnapshotToMarkdown`), each module is projected with: - **YAML metadata frontmatter** (optional, enabled by default): records `forma_ontology: true`, `branch`, `snapshot`, `policies`, and `definitions`. - **Semantic section headings:** groups definitions in canonical order under `## Entities`, `## Types`, `## Errors`, `## Services`, `## Queries`, `## Workflow Models`, `## Definitions`, and `## Tests` (omitting empty sections). - **Docstrings as prose:** preserves definition docstrings above clean ```` ```forma ```` blocks. ### Prose-Invariant Round-Trips & Surgical Updates Semantic identity in the store (`DefHash`) depends only on the canonical meaning of definitions inside executable fences: 1. **Prose edits produce zero semantic operations.** Rewriting Markdown paragraphs, headings, tables, non-Forma fences, or `ignore` fences leaves every `DefHash` unchanged, so `importMarkdownAsTransaction` returns an empty operation list. 2. **Surgical updates preserve human prose.** Passing `originalMarkdown` to `exportSnapshotToMarkdown` updates only changed, renamed, added, or removed definitions inside their existing fenced blocks. Unchanged modules are returned byte-for-byte verbatim, and surrounding prose, custom frontmatter, and non-Forma fences are preserved intact. 3. **Requirement vs. implementation classification.** Editing a `test`, refinement contract (`refine` / `:where`), or `law` inside a `.md` fence is automatically classified as `requirement-edit` (requiring explicit approval when enforced by branch policy), while normal implementation edits classify as `impl-edit`. ## Programmatic API Both the Markdown extractor and store projection helpers are browser-safe (zero `node:*` imports) and exported from `@forma-language/forma`: - Markdown source extraction & remapping: `isMarkdownPath`, `extractMarkdownBlocks`, `extractMarkdownProgram`, `remapMarkdownSpan`, `remapMarkdownDiagnostics`, `spliceMarkdownBlocks`. - Store Markdown projection: `exportSnapshotToMarkdown`, `importMarkdownAsTransaction`, `moduleIdToMarkdownPath`.