Skip to content

CONSUME-AS-DATA-01: Open the read() door for typed frontmatter

contract.read returns a typed Doc whose frontmatter fields you pull with no separate types file.

Builds on: nothing — this is the first rung of Consume as Typed Data: Reading the Document as a Model.

A TypeScript program against the library API; inline comments show the resulting values and behavior.

import { contract, sections, section } from "markdown-contract";
import { z } from "zod";
const c = contract({
frontmatter: z.object({ id: z.string(), status: z.string() }).strict(),
body: sections({ allowUnknown: true }, [section("Summary")]),
});
const doc = c.read(src, { path: "notes/note.md" }); // Doc, or throws ContractError
console.log(doc.frontmatter.id); // typed from the same contract that validated
  • contract()
  • contract.read(src,{path})
  • Doc.frontmatter typed from the frontmatter Zod schema