@nextgensoftwares/folio-formatter
A smart formatting pass: rules read the document and propose layout hints. It never mutates the document. 2 rules
ts
interface RuleContext { prev: FolioNode | undefined; next: FolioNode | undefined }
interface FormatRule {
name: string;
apply(block: FolioNode, ctx: RuleContext): BlockHint | undefined; // BlockHint from @nextgensoftwares/folio-layout
}A rule may only depend on the block and its immediate neighbours: results are cached by those three nodes' identities (per rules array), so a keystroke re-evaluates three blocks, not the whole book. Hosts ship their own rule packs.
| Export | Signature | Description |
|---|---|---|
format | (doc: FolioDocument, rules: readonly FormatRule[]) => Map<number, BlockHint> | hints by top-level block index; later rules' fields override earlier ones for the same block. Pass the same rules array each time to keep the cache |
h1StartsPage | FormatRule | every top-level H1 after the first gets pageBreakBefore |
introKeepsWithNext | FormatRule | a paragraph ending in : (or :) keeps with a following list, table, code block, media or math block |
defaultRules | FormatRule[] | [introKeepsWithNext] |
ts
const hints = format(doc, defaultRules);
const layout = layoutDocument(doc, { measurer, hints });@nextgensoftwares/folio-editor re-declares the rule shape structurally as FormatRuleLike, so plugins can contribute rules without depending on this package.