Documentation

Modes

The four ways polytypo reads a document, what each one treats as prose, and how to choose between them.

Updated · spec 1.12.0

A mode tells polytypo what the string you pass in actually is. The rules (quotes, dashes, spaces, no-break spaces and the rest) are the same in every mode; the mode decides which characters the rules are allowed to see. Everything a mode does not hand to the rules comes back byte for byte.

There are four modes, selected with the mode option:

ModeThe input isWhat the rules seeNeeds
textplain prosethe whole stringnothing
htmlan HTML document or fragmenttext nodes only, never tags, attributes or codenothing
markdownCommonMark or MDXprose, never code, link destinations, HTML or (by default) frontmatterdialect
yamla YAML filethe values of the keys you namekeys

Why there are modes at all #

Typography rules read the characters around what they change. In plain prose that is safe. In a document it is not: a rule that turns " into a curly quote must not touch the quote that closes an HTML attribute, and a rule that puts a space before a colon in French must not touch title: in frontmatter.

html and markdown are prose formats with islands of code in them, so the modes skip a fixed, closed list of islands and treat everything else as prose. yaml is the opposite, a data format with islands of prose in it, so it processes only what you name. That difference is why markdown needs a dialect and yaml needs keys: in both cases the one fact the library cannot work out for itself is required.

What every mode guarantees #

  • Unprocessed text is returned unchanged. The output is the input with a set of replacements applied at known positions; the source is located, never re-emitted. Markup is not re-serialised, attribute quoting and whitespace stay as written. See Round-trip guarantee.
  • Prose is processed as one text. A quotation that opens before an inline element and closes after it is still one quotation. See Span model.
  • The result is a fixed point. Running a mode again on its own output changes nothing. See Idempotency.

Choosing a mode #

  • A string of prose, such as a headline, a CMS field or a notification: text.
  • A string that contains tags, or can contain & or  : html, even when it is a fragment. A string that might contain a character reference is HTML.
  • A .md or .mdx file: markdown, with the dialect the file is written in.
  • A configuration or API description file with some prose fields: yaml, with the keys of those fields.

Choosing a mode goes through the pitfalls of getting this wrong.

Normative text: spec/rules/modes.md.

Already using polytypo? Add our badge

Two lines: a script tag and a span. No per-embed network call. Pick a language and a theme, then copy the code into your footer or credits. Embedding notes cover Content Security Policy. Sites with the badge are listed on Sites using polytypo.

<script async src="https://polytypo.dev/badge.js"></script>
<span data-polytypo-lang="en" data-polytypo-theme="light"></span>

Search runs in your browser. Nothing is sent anywhere.