Skip to content

CONSUME-AS-DATA-07: Look blocks up by anchor

doc.byAnchor(id) and section-scoped byAnchor resolve any ^anchor to a kind-discriminated BlockView, declared or not.

Builds on: CONSUME-AS-DATA-06: Name two tables in one section

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

const doc = c.read(src, { path: "README.md" });
const extra = doc.byAnchor("extra"); // doc-wide; an UNdeclared ^extra table
if (extra?.kind === "table") extra.rows[1].Option; // narrow on .kind, then read
doc.byAnchor("components")?.kind; // "table" — declared anchors resolve too
doc.body.decision.byAnchor("missing"); // undefined — scoped to one section
  • doc.byAnchor(id) doc-wide
  • SectionView.byAnchor(id) scoped
  • BlockView .kind discriminant + narrowing