Skip to main content
Recipe number 66

Cookbook · Chapter 3 · Headings & openers

Storybook chapters with vignettes and summaries

One heading style opens every chapter; each heading names its vignette and its italic summary in attributes, which the opener paints.

pp. 6–7 · 1–2 of 5

  • Trim 190 × 240 mm
  • 1 column
  • Averia Serif Libre 13/19
  • Fredericka the Great
  • Quicksand
  • 5 pages
  • Level
  • Postext 1.57.1
  • Laid out in 59 ms
  • 162 lines of code

In short

The first pages of an Italian children's edition of Pinocchio. Each chapter opens with its own soft painted picture and a short summary of what happens.

What you'll build

The opening pages of an Italian children's edition of Collodi's Le avventure di Pinocchio, set in Averia Serif Libre at 13 on 19 pt on a 190 × 240 mm page. A title page on the verso faces chapter I: a watercolour of the Tuscan hills, where a donkey cart brings firewood to a carpenter's workshop, above the title in Fredericka the Great. Each chapter opens under a watercolour vignette that fades into the paper (Maestro Ciliegia with his axe raised over the talking log for chapter I, Geppetto in his yellow wig taking the log for chapter II), then the chapter line and Collodi's summary in centred italic. All four pictures were generated with diffusion models from one style prompt and the palette of the page. Running heads print only on pages 8 and 9, the two without an opener. The folios sit in wood-coloured discs, and two cherries close chapter I with no caption or number.

This recipe answers

  • How do I give every chapter its own photo, colour or opener variant?
  • How do I set unnumbered artwork: ornaments, vignettes, logos?
  • How do I add an author line, a standfirst or a lead with a drop cap to an opener?
  • How do I hide running heads on openers and blank pages, or paint a blank verso in the part colour?

The short answer

script.js · lines 44–64in full code
// # Capitolo I {style="capitolo" vignetta="ceppo" summary="Come andò che Maestro Ciliegia…"}
// resourceId takes placeholders like a design text: '{attr.vignetta}' draws the resource the
// heading names, so every chapter shares one style.
const VIGNETTE = { width: 84, height: 56 }; // mm, centred at the head of the text block
const SUMMARY = 124; // mm: the summary's measure, narrower than the text's
const opener = { enabled: true,
  // A floor for the height the opener reserves, in whole lines, so the text under chapter I's
  // two-line summary starts on the same grid line as the text under chapter II's three lines.
  minHeight: pt(14 * LEAD),
  slot: { elements: [
    { kind: 'image', id: 'vignette', resourceId: '{attr.vignetta}',
      placement: at('container', 'top', 0, 0,
        { width: mm(VIGNETTE.width), height: mm(VIGNETTE.height) }) },
    // The chapter line 6 mm under the picture's foot, the summary 5 mm under the chapter line.
    text('chapter', '{titleText}', DISPLAY, 24, at('container', 'top-left', 0,
      VIGNETTE.height + 6, { width: mm(MEASURE) }), { ...caps(2.4), lineHeight: 1 }),
    text('summary', '{attr.summary}', TEXT, 12.5, at('#chapter', 'below',
      (MEASURE - SUMMARY) / 2, 5, { width: mm(SUMMARY) }), { italic: true, lineHeight: 1.38 }),
  ] },
};
const chapter = { id: 'capitolo', advancedDesign: opener };

One opener for every chapter: the vignette the heading names, then its summary

Ingredients

Type
Averia Serif Libre, Fredericka the Great, Quicksand (SIL OFL 1.1)
Assets
  • ceppo-1200.jpg
  • ciliegie-600.jpg
  • frontespizio-1400.jpg
  • polenta-1200.jpg
  • The frontispiece: Tuscan hills, a hill village and a donkey cart of firewood on its way to the carpenter’s workshop (Generated With Diffusion Models, original)
  • Chapter I’s vignette: Maestro Ciliegia, axe raised over the talking log (Generated With Diffusion Models, original)
  • Chapter II’s vignette: Geppetto in his yellow wig receives the log, beside a pot of polenta (Generated With Diffusion Models, original)
  • The cherry tailpiece (Generated With Diffusion Models, original)

Method

#1 · One heading style, a vignette named on each heading

The code for this step is the short answer. An image element's resourceId takes the same placeholders as a design text, so '{attr.vignetta}' draws the resource the heading names, the way {attr.summary} prints its summary. Both chapters share the capitolo style and name their picture on the heading line: # Capitolo II {style="capitolo" vignetta="polenta" summary="…"}. A third chapter needs one more resource and one more heading, not another style (image elements). The chapter line is placed from the top of the container, 6 mm below the vignette's box, and the summary hangs 5 mm under it; the opener reserves room down to the summary's last line. With minHeight as a floor of 14 lines, chapter I's text starts on the same line as chapter II's, whose summary runs one line longer; without it, chapter I's text starts 6.7 mm higher (span and advanced design).

#2 · Load the paintings as bitmap resources

script.js · lines 122–133in full code
const PICTURES = { // id: [file, width, height, alt text in the book's language]
  frontespizio: ['frontespizio-1400.jpg', 1400, 1105, 'Colline toscane con cipressi e un borgo; '
    + 'un contadino guida un asinello con un carretto di legna verso la bottega di un falegname.'],
  ceppo: ['ceppo-1200.jpg', 1200, 800, 'Maestro Ciliegia, con la scure alzata sopra un pezzo di '
    + 'legno sul banco, si guarda intorno spaventato.'],
  polenta: ['polenta-1200.jpg', 1200, 800, 'Geppetto, con la parrucca gialla, riceve il pezzo di '
    + 'legno da Maestro Ciliegia; accanto, un paiolo di polenta fumante.'],
  ciliegie: ['ciliegie-600.jpg', 600, 626, 'Due ciliegie rosse su un picciolo, con una foglia.'],
};
const resources = Object.entries(PICTURES).map(([id, [fileId, w, h, altText]]) => ({
  id, typeId: 'fregio', kind: 'bitmap', createdAt: 0, updatedAt: 0, altText,
  bitmap: { fileId, format: 'jpeg', width: w, height: h } }));

Each picture is a JPEG in the recipe's assets/ folder, declared at its pixel size: the layout reads only those sizes, and the canvas paints whatever loadImage() has registered under each file id when it draws the page. The vignettes and the cherries were generated on white and then multiplied by the paper colour #fffaf0, so their margins print as the page and the pictures have no visible edge. The frontispiece is cut to 190:150, the shape of its field, so the fitted image fills the field without a band of paper. The landscape and the vignettes are painted by image elements in the designs, the cherries by ::resource{id="ciliegie"} in the Markdown.

#3 · Make the title page a heading style

script.js · lines 68–85in full code
const SKY = 150; // mm from the top edge: the foot of the painted field
// # Pinocchio {style="frontespizio" kicker="Le avventure di"}; subtitle and author come from
// the frontmatter. Every element hangs from the page, so the field runs off three edges.
const frontispiece = {
  id: 'frontespizio',
  footer: { elements: [] }, // no folio; the running heads already skip openers
  advancedDesign: { enabled: true, slot: { elements: [ // array order is paint order
    { kind: 'image', id: 'landscape', resourceId: 'frontespizio', // cut to the field's 190:150
      placement: at('page', 'top-left', 0, 0, { width: 'fill', height: mm(SKY) }) },
    text('kicker', '{attr.kicker}', LABEL, 11, at('page', 'top', 0, SKY + 11),
      { ...caps(2.6), fontWeight: 700 }),
    text('title', '{titleText}', DISPLAY, 70, at('page', 'top', 0, SKY + 17),
      { lineHeight: 1, color: col('bark') }),
    text('subtitle', '{subtitle}', TEXT, 15, at('page', 'top', 0, SKY + 44), { italic: true }),
    text('author', '{author}', LABEL, 9, at('page', 'top', 0, SKY + 58),
      { ...caps(2), fontWeight: 600 }),
  ] } },
};

The title page is the first heading of the Markdown, # Pinocchio {style="frontespizio" kicker="Le avventure di"}, and its style paints everything on the page: the landscape, 150 mm deep from the top edge, then the kicker from the attribute, the title, and the subtitle and author from the frontmatter. Every element hangs from the page, not from the text block, so the painting reaches the top edge and both sides of the sheet while the heading itself stays in the book's one column. The empty footer removes the folio disc the chapter openers print, and pages: 'body' already keeps the running heads off the page (heading styles).

#4 · Running heads on pages of text, folios in discs

script.js · lines 89–110in full code
// The excerpt opens on book page 6, a verso, so the title page faces chapter I.
const continuation = { pageIndexOffset: 5, pageNumbering: { startAt: 6 } };
const SHIFT = (INNER - OUTER) / 2; // mm: the text block's centre sits off the page's centre
const head = (id, content, parity, x) => text(id, content, LABEL, 8,
  at('page', 'top', x, 12), { ...caps(1.8), fontWeight: 600, parity, pages: 'body' });
const DISC = 7.4; // mm: the folio's disc
// A fixed square with a radius of half its side is a circle; the figures sit in its middle.
// lineHeight 1.17: the baseline falls at 0.8 of the line, 0.94 em down, so Quicksand's
// figures (0.70 em tall) leave 0.23 em above and below them, and 'middle' centres that line.
const disc = (id, parity, pages, edge, x) => text(id, '{pageNumber}', LABEL, 8.5,
  at('page', edge, x, -(BOTTOM - DISC) / 2, { width: mm(DISC), height: mm(DISC) }),
  { fontWeight: 700, color: col('paper'), lineHeight: 1.17, verticalAlign: 'middle', parity,
    pages, box: { backgroundColor: col('wood'), borderRadius: mm(DISC / 2) } });
const header = { elements: [
  head('verso-title', '{title}', 'even', -SHIFT), // the book on the left-hand page
  head('recto-chapter', '{chapterTitle}', 'odd', SHIFT), // the chapter on the right
] };
const footer = { elements: [
  disc('verso-folio', 'even', 'body', 'bottom-left', OUTER),
  disc('recto-folio', 'odd', 'body', 'bottom-right', -OUTER),
  disc('opener-folio', 'all', 'opener', 'bottom', 0), // a chapter opens on either side
] };

continuation numbers the excerpt from book page 6, a verso, so the title page faces chapter I and the first folio printed is 7. pages: 'body' keeps the running heads off the title page and both openers, so they print only on pages 8 and 9, the book's title on the verso and the chapter's on the recto (text elements). The folio is a design text in a fixed 7.4 mm square whose corner radius is half its side, which makes a circle; verticalAlign: 'middle' and the line height worked out in the comment centre the figures in it. The paper-coloured figures have a contrast ratio of 4.98:1 against the wood. The tracked labels take no offset: the tracking after a line's last letter is left out when the line is centred.

#5 · Close the chapter with an unnumbered tailpiece

script.js · lines 114–118in full code
// No caption prefix and no caption, so the cherries that close chapter I print with no
// label or number. The pictures the design slots paint are of this type too.
const resourceTypes = [{ id: 'fregio', name: 'Fregio', namePlural: 'Fregi', shortLabel: 'Fregio',
  captionPrefix: '', numberingTemplate: '{n}', resetOn: 'never', counterFormat: 'decimal',
  defaultPlacement: { position: 'here', width: 0.16, align: 'center' } }];

A resource type prints its captionPrefix and number before the caption. With an empty prefix and no caption, nothing prints under the cherries; with captionPrefix: 'Fregio', the same resource prints Fregio 1. under the drawing and grows 10 mm taller (resource types). The type's defaultPlacement sets each resource where its ::resource line stands, a sixth of the measure wide and centred, so the Markdown needs only ::resource{id="ciliegie"} after the chapter's last paragraph. The other pictures share the type but no :ref or ::resource names them, so they are never placed or counted.

The whole recipe

Sandbox
// ═══ Postext Cookbook · Nº 066 · Storybook chapters with vignettes and summaries ═══
// https://postext.dev/en/cookbook/storybook-chapter-vignettes
// Code: MIT · Text: C. Collodi, 1883 (PD, Gutenberg #52484) · Pictures: diffusion models
// Fonts: Averia Serif Libre, Fredericka the Great, Quicksand (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document (the sample is Italian in both)
const RECIPE = 'storybook-chapter-vignettes';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { // every colour in the config comes from here; the pictures were painted to it
  ink: '#2b2118', // text: a brown near-black
  paper: '#fffaf0', // a warm book paper, which the vignettes' margins were tinted to
  bark: '#6b4224', // chapter lines, running heads, the title
  wood: '#9a5f30', // the folio discs
};
// col(id): a colour linked to its palette entry, with the entry's hex beside the id.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
  ...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
  // The defaults linked to main-color, bold among them, print in bark instead of blue.
  { id: 'main-color', name: 'bark (defaults)', value: { hex: palette.bark, model: 'hex' } },
];
// Text and summaries; the book's title and the chapter lines; running heads and folios.
const [TEXT, DISPLAY, LABEL] = ['Averia Serif Libre', 'Fredericka the Great', 'Quicksand'];
const TRIM = { width: 190, height: 240 }; // mm
const [TOP, INNER, OUTER] = [22, 22, 20]; // mm; mirrored, so the spine margin swaps sides
const MEASURE = TRIM.width - INNER - OUTER; // 148 mm
const [BODY, LEAD, LINES] = [13, 19, 29]; // pt, pt, and whole lines in the text block
const BOTTOM = TRIM.height - TOP - (LINES * LEAD * 25.4) / 72; // mm: 23.6
// Design-slot shorthands: a placement from an anchor's edge (mm), and a design text.
const at = (to, edge, x = 0, y = 0, size) => ({ anchor: { to, edge },
  offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const text = (id, content, fontFamily, size, placement, extra = {}) => ({ kind: 'text', id,
  content, fontFamily, fontSize: pt(size), color: col('ink'), align: 'center',
  overflow: 'wrap', // a long line wraps in every slot, the running heads too
  placement, ...extra });
const caps = (tracking) => ({ textTransform: 'uppercase', letterSpacing: pt(tracking),
  color: col('bark') });

// #region answer: one opener for every chapter: the vignette the heading names, then its summary
// # Capitolo I {style="capitolo" vignetta="ceppo" summary="Come andò che Maestro Ciliegia…"}
// resourceId takes placeholders like a design text: '{attr.vignetta}' draws the resource the
// heading names, so every chapter shares one style.
const VIGNETTE = { width: 84, height: 56 }; // mm, centred at the head of the text block
const SUMMARY = 124; // mm: the summary's measure, narrower than the text's

The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)

Variations

#Open every chapter on a recto

With 'odd', chapter II moves to page 11 and page 10 is left blank; the title page turns its own break off, because it inherits the level's and would otherwise move to page 7 after a blank page 6.

@@ the frontispiece style @@
   id: 'frontespizio',
+  breakBefore: { enabled: false },
@@ headings, in config() @@
-    levels: [{ level: 1, breakBefore: { enabled: true, parity: 'any' } }],
+    levels: [{ level: 1, breakBefore: { enabled: true, parity: 'odd' } }],

Pitfalls

Pitfall

Attribute values: no { or }; single-quote a value with "

An attribute value ends at the closing brace, so it cannot hold { or }. A value that contains a double quote goes in single quotes; a dollar sign is fine. Heading attributes →

  • In the Markdown, a no-break space (U+00A0) ties each of Collodi's dialogue dashes to its word, so a closing dash never opens a line; two more keep di legno and in piedi together at the end of their paragraphs. The ties hold in a paragraph with bold or italic too, such as the one with ohi on page 9, which has four.

Credits

Text
  • Le avventure di Pinocchio (1883): chapter I and the opening of chapter II with Collodi’s chapter summaries, from the 1902 Bemporad edition, with typographic apostrophes, dashes and ellipses; chapter I’s summary has the comma after “falegname” that the edition’s table of contents prints and its chapter head leaves out · Carlo Collodi · public domain
Images
  • The frontispiece: Tuscan hills, a hill village and a donkey cart of firewood on its way to the carpenter’s workshop · Generated With Diffusion Models · original
  • Chapter I’s vignette: Maestro Ciliegia, axe raised over the talking log · Generated With Diffusion Models · original
  • Chapter II’s vignette: Geppetto in his yellow wig receives the log, beside a pot of polenta · Generated With Diffusion Models · original
  • The cherry tailpiece · Generated With Diffusion Models · original
Fonts
Averia Serif Libre (SIL OFL 1.1) · Fredericka the Great (SIL OFL 1.1) · Quicksand (SIL OFL 1.1)
Sandbox