跳到主要内容
食谱编号42

排版食谱 · 第1章 · 页面与网格

社区简报:头条与简讯

一份两页的菜园协会简报,采用一栏半版面:头条占满宽栏并转到第2页接续;分栏符把带标签的简讯送进窄栏。

本页内容
输出
Canvas
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.25.0
许可证
更新于2026年10月11日
代码MIT · 文本CC BY 4.0
  • 英文样例:尚无中文版本
  • 成品尺寸210 × 297 mm
  • 一栏半, 栏间距8 mm
  • Work Sans 9.4/13.4
  • Courier Prime
  • Titan One
  • 2页
  • 难度
  • Postext 1.57.1
  • 排版用时55 ms
  • 168行代码

简单来说

一个社区菜园协会的两页简报。头条文章占宽栏,接到第2页继续;短消息排在旁边的窄栏里。

成品一览

The Allotment Gazette的春季号,这是一个虚构菜园协会的简报,印在一张A4纸的正反两面。一条麦秆色色带上用Titan One分两行排着刊名,底边一排幼苗,还有一个番茄色标签写着期号和季节。色带下方,页面分成一个113 mm的栏和一个57 mm的栏。头条讲新建的三个堆肥池,排满宽栏,上方是一幅用代码画的场地平面图,结束于一个“Continued on page 2”标签。窄栏排五条简讯,每条以胶囊形标签开头。第2页在转页标题下接续头条,并把一幅温度图表居中排在栏里。再往下,介绍苗床的段落排在苗床图旁边,图放在栏的右边。窄栏排满八条通知。

这道食谱解答

  • 怎样做一栏半版式,一个宽的正文栏加一个窄的侧栏?
  • Postext能排三栏或更多栏,或者让文字绕图排吗?
  • 怎样在框里分两栏(文字栏旁边是图栏)?
  • 怎样做行内标签:键盘按键、标签、练习用的词库?

简短回答

script.js · 第33–49行在完整代码中
const layout = {
  layoutType: 'oneAndHalf', // the narrow column sits on the right and takes text (the
  // defaults of sideColumnSide and sideColumnRole; 'floats' would keep text out of it)
  sideColumnPercent: 32, // 57 mm of the 178 mm measure
  gutterWidth: mm(8), // the wide column keeps the other 113 mm
  columnRule: { enabled: true, color: col('rule') }, // a 0.5 pt hairline in the gutter
};
// The text runs down the wide column, then the narrow one, then the next page's wide
// column. A :::columnbreak sends the next block on to the following column, so the
// Markdown decides what each column holds:
//   ## Shared compost bays open by the top gate      ← the lead, in the wide column
//   … :chip[Continued on page 2]{style="jump"}
//   :::columnbreak                                    ← the briefs open the narrow column
//   ## In brief {style="rail"}
//   …
//   :::columnbreak                                    ← from the last column: next page
//   :chip[Continued from page 1]{style="jump"}        ← the lead goes on in the wide column

用料

类型
Work Sans, Titan One, Courier Prime(SIL OFL 1.1)
素材
  • The site plan, the temperature chart, the square-metre bed and the row of seedlings under the nameplate, drawn in code in the page's palette (Ignacio Ferro, CC BY 4.0)

做法

#1 · 让Markdown填满每一栏

版面设置见上文的简短回答。在oneAndHalf版面中,窄栏默认排正文,正文先排宽栏,再排窄栏,然后是下一页的宽栏(版面类型)。:::columnbreak在所在位置结束一栏(指令):头条的“Continued on page 2”标签之后,它开启窄栏排简讯;最后一条简讯之后,它位于该页的最后一栏,于是开启第2页。这一期第1页的两栏都调整为结束于同一条网格线,距上切边275 mm,所以删掉任一个分栏符,页面都不变。稿子一改,分栏符就起作用了。删掉头条的第二段,如果没有第一个分栏符,“In brief”会从宽栏往下247 mm处、那个标签下方开始,第一条简讯会延续到窄栏,后半段按窄栏57 mm的行长重新断行。

#2 · 用前置元数据搭出报头

script.js · 第53–75行在完整代码中
const BEARING = 0.6; // mm: Titan One's left side bearing at 60 pt (T 0.42, G 0.63, L/H 0.85)
const DRILL = { id: 'drill', typeId: 'drawing', kind: 'svg', createdAt: 0, updatedAt: 0,
  svg: { fileId: 'drill.svg', width: 2100, height: 80 } }; // drawn by the design, not the text
const masthead = { enabled: true,
  minHeight: pt(10 * LEAD), // ten grid lines: the text starts one line clear of the band
  slot: { elements: [
    // The band is 62 mm deep. The column rule starts under the masthead, where the text does.
    { kind: 'box', id: 'band', style: { backgroundColor: col('straw') },
      placement: { ...at('page', 'top-left'), size: { width: mm(210), height: mm(62) } } },
    { kind: 'image', id: 'drill', resourceId: 'drill', // seedlings along the band's foot
      placement: { ...at('page', 'top-left', 0, 54), size: { width: mm(210), height: mm(8) } } },
    { kind: 'text', id: 'society', content: '{subtitle}', ...caps(8, 'leaf'), align: 'left',
      placement: { ...at('page', 'top-left', SIDE, 12), size: { width: mm(150) } } },
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: DISPLAY, fontSize: pt(60),
      lineHeight: 0.9, // 54 pt from line to line
      color: col('leaf'), align: 'left', overflow: 'wrap', // two lines in 178 mm
      placement: { ...at('page', 'top-left', SIDE - BEARING, 17), size: { width: mm(178) } } },
    // {attr.issue} comes from the H1 line, {publishDate} from the frontmatter.
    { kind: 'text', id: 'tab', content: '{attr.issue} · {publishDate}', ...caps(9, 'paper'),
      align: 'right', box: { backgroundColor: col('tomato'),
        padding: { top: mm(1.8), right: mm(3.2), bottom: mm(1.6), left: mm(3.2) } },
      placement: at('page', 'top-right', -SIDE, 44) },
  ] } };

H1采用一个标题样式,其版面设计画出报头,span: 'page'让这个设计横跨两栏上方。60 pt的{titleText}在178 mm宽的框里折成两行。标签把写在H1行上的{attr.issue}和前置元数据里的{publishDate}拼在一起(文本元素)。没有minHeight的话,报头会预留到色带底部,即距上切边62 mm处,正文会从下一条网格线,也就是62.5 mm处开始,紧贴色带。minHeight设为十条网格线,正文就从67 mm处开始。

#3 · 用行内标签标记简讯

script.js · 第79–89行在完整代码中
const chip = (id, look) => ({ id, fontFamily: LABEL, bold: true,
  fontSize: pt(7.7), // in points: em(0.82) is 7.7 pt in the text, 6.6 pt on the 8 pt jump lines
  borderWidth: pt(0), borderRadius: em(1), // a radius past half the height draws a pill
  paddingX: em(0.5), paddingY: em(0.14), ...look }); // em: of the chip's own size
const chipStyles = [
  chip('tag', { background: col('tomato'), color: col('paper') }), // the first is the default
  chip('free', { background: col('marigold'), color: col('ink') }),
  chip('when', { backgroundEnabled: false, borderWidth: pt(0.8), borderColor: col('leaf'),
    color: col('leaf') }),
  chip('jump', { background: col('straw'), color: col('ink') }),
];

声明chipStyles会替换内置的浅蓝色标签,不带style的标签采用第一个样式(标签样式)。圆角半径大于框高的一半时,画出的就是胶囊形。标签内部永远不断开,所以一行只会在标签前后的空格处断开,“Sat 11 April”这样的日期会留在同一行。标签在Markdown里用大写字母输入,因为标签样式没有大小写设置。垂直内边距画在行外,所以13.4 pt的网格保持不变。

#4 · 让文字排在图旁

script.js · 第93–98行在完整代码中
// wrap sets the drawing at that side of its column, width of it, and the text after the
// ::resource line runs beside it at the measure left over, then under it at full width:
//   #### Sixteen squares by the gate {style="kicker"}
//   ::resource{id="bed"}                     ← 51 mm at the right of the 113 mm column
//   The demonstration bed by the gate is 1.2 metres square …   ← beside it, then under it
const WRAP = { wrap: 'right', width: 0.45 }; // a body line of white between it and the text

wrap: 'right'把苗床图放在113 mm宽栏的右边,宽度为栏宽的45 %(51 mm),::resource行之后的段落在剩下的57 mm里排在图旁,与图及其题注隔开一行正文的距离(文字绕排)。图的顶端与段落第一行的顶端齐平,每一行都在13.4 pt的网格上。段落再长一些,就会在图下方恢复113 mm全宽,仍是同一个段落;如果图还在正文旁边时来了标题或另一幅图,它们会排到图的下方。图上方的四级标题采用一个标题样式,只把颜色改成叶绿色。

#5 · 把窄图表居中放在所在区域

script.js · 第102–114行在完整代码中
const drawing = (id, w, h, placement = {}) => ({ id, typeId: 'drawing', kind: 'svg',
  svg: { fileId: `${id}.svg`, width: w * 10, height: h * 10 }, // mm × 10: fitted to the column
  placement: { position: 'here', ...placement }, // drawn where ::resource{id} stands
  caption: CAPTIONS[id][0], note: CAPTIONS[id][1], altText: CAPTIONS[id][2],
  createdAt: 0, updatedAt: 0 });
// width is a fraction of the column and align where the figure sits in it: the text above
// and below never moves into the white either side.
const HEAT = { width: 0.62, align: 'center' };
const resources = () => [drawing('plan', 114, 56.5), drawing('heat', 68, 40, HEAT),
  drawing('bed', 50, 50, WRAP), DRILL];
// Newsletter drawings carry no "Figure 1": an empty prefix prints no label.
const resourceTypes = [{ id: 'drawing', name: 'Drawing', shortLabel: 'Drawing',
  captionPrefix: '', numberingTemplate: '', resetOn: 'never', counterFormat: 'decimal' }];

width: 0.62让图表在113 mm的栏里宽70.1 mm,align: 'center'在两侧各留出21.5 mm空白(位置)。上下的正文保持完整行长,不会流进这片空白。这些图属于一个captionPrefix为空的资源类型,所以每条题注都以自己的粗体词语开头,前面没有“Figure 1”。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 042 · Community newsletter: lead story and briefs ═════
// https://postext.dev/en/cookbook/community-newsletter
// Code: MIT · Text: original (CC BY 4.0) · Drawings: generated in code (CC BY 4.0)
// Fonts: Work Sans, Titan One, Courier Prime (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage, inlineSvgFonts,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'community-newsletter';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: garden colours, every one linked by id
// ink: text · leaf: nameplate and headlines · tomato, the accent: tags, tab, column heads ·
// straw: the band · marigold: second tag · tint: pale greens · rule: hairlines · muted: notes
const palette = { ink: '#1d211c', leaf: '#2f6b3a', tomato: '#c43f2a', straw: '#f2d492',
  marigold: '#e2b33c', tint: '#eef4e8', rule: '#cfd3c6', muted: '#5f6659', paper: '#fbfaf5' };
// Each colour carries its hex and the palette entry it follows.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [ // defaults link to 'main-color': point it at the leaf, never blue
  ...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
  { id: 'main-color', name: 'leaf (defaults)', value: { hex: palette.leaf, model: 'hex' } },
];
// #endregion
const [TEXT, DISPLAY, LABEL] = ['Work Sans', 'Titan One', 'Courier Prime'];
const SIDE = 16; // mm: the side margins of one A4 sheet, printed on both sides
const LEAD = 13.4; // body leading in pt: the baseline grid
const caps = (size, colour) => ({ fontFamily: LABEL, fontSize: pt(size), fontWeight: 700,
  textTransform: 'uppercase', letterSpacing: pt(size * 0.12), color: col(colour) });
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });

// #region answer: a wide column for the lead, a narrow one for the briefs, one flow
const layout = {
  layoutType: 'oneAndHalf', // the narrow column sits on the right and takes text (the
  // defaults of sideColumnSide and sideColumnRole; 'floats' would keep text out of it)
  sideColumnPercent: 32, // 57 mm of the 178 mm measure
  gutterWidth: mm(8), // the wide column keeps the other 113 mm
  columnRule: { enabled: true, color: col('rule') }, // a 0.5 pt hairline in the gutter
};
// The text runs down the wide column, then the narrow one, then the next page's wide
// column. A :::columnbreak sends the next block on to the following column, so the
// Markdown decides what each column holds:
//   ## Shared compost bays open by the top gate      ← the lead, in the wide column
//   … :chip[Continued on page 2]{style="jump"}
//   :::columnbreak                                    ← the briefs open the narrow column
//   ## In brief {style="rail"}
//   …
//   :::columnbreak                                    ← from the last column: next page

组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)

变化

#简讯放在左边

宽栏仍然先读,所以同一份Markdown会把头条排在右边,简讯排在左边。

   layoutType: 'oneAndHalf',
+  sideColumnSide: 'left',

#简讯横排整页

正文最多排两栏。报纸头版在页面下部用一个带:::columns{count=4}的框横排四条简讯。

致谢

文本
原创文字, CC BY 4.0
图片
  • The site plan, the temperature chart, the square-metre bed and the row of seedlings under the nameplate, drawn in code in the page's palette · Ignacio Ferro · CC BY 4.0
字体
Work Sans (SIL OFL 1.1) · Titan One (SIL OFL 1.1) · Courier Prime (SIL OFL 1.1)
沙盒