Bend the compiler:
pandoc Lua filters in Folio Writer

Folio Writer's compile pipeline covers the 99% — citations, cross-references, templates, formats. Lua filters are the escape hatch for the last 1%: the journal's odd house rule, your own shorthand, the thing no checkbox anticipates. A filter is a few lines of Lua that rewrite the document during compilation, and pandoc runs them natively — nothing to install.

What a filter actually is

When pandoc compiles your manuscript, it first parses the Markdown into a document tree — paragraphs, headers, spans, citations. A Lua filter is a set of functions named after those elements; each one gets called with every matching element and may return a replacement. Return nothing to leave it alone, return {} to delete it, return new elements to transform it. That's the whole model.

Attaching one in Folio Writer

Since 1.17 a filter can belong to the project — it runs on every compile and preview, on every device, and on the web — or to a compile profile on one Mac. Project filters are the ones to reach for.

  1. Open File ▸ Project Settings… ▸ Compile ▸ Lua filters and press Add. Three sources: From Templates — a .lua document kept in the binder under Templates ▸ Lua Filters, editable in the editor and synced with the project; Built-in — the filters that ship with Folio Writer (below); or File… — a .lua anywhere on this Mac (it stays on this Mac).
  2. Order matters: filters run top to bottom, after pandoc-crossref and before citeproc. Drag to reorder.
  3. Per-Mac filters live where they always did — the compile sheet's Style tab, Lua filters ▸ Add Lua Filter…, saved with the compile profile — and run after the project's.
Project Settings ▸ Compile: the Lua filters list with the built-in abstract-section filter, Defaults files and extra pandoc arguments
Project Settings ▸ Compile in the Example Project: the built-in abstract-section filter, listed as builtin:abstract-section.lua.
The preview plays along: the live preview runs the project's filters in the same order as compile, so you see the effect while writing, not as a surprise in the PDF. A filter kept in Templates ▸ Lua Filters never shows up in New From Template — it is compile tooling, not a document template.

Tutorial · The five built-in filters

These ship inside the app and are referenced in the project as builtin:<name>.lua, so a project that lists one works on every Mac, on iPhone and iPad, and on the Folio Writer server. The academic templates come with the ones they need already switched on.

FilterWhat it does
abstract-sectionA section headed Abstract — the binder title of an Abstract document, or a # Abstract you typed — is lifted out of the body into the abstract: metadata, so it lands where each format keeps abstracts: the LaTeX title block, elsarticle's abstract box, IEEEtran, Typst's and Word's abstract slot. A Keywords section right after it becomes keywords:. An abstract: already in the metadata wins.
scholarly-authorsThe structured author list the templates use — name, email, orcid, corresponding, affiliations — comes out right through plain pandoc: superscript affiliation numbers, the affiliation lines and the corresponding author's e-mail under the names, date: today as today's date; in PDF, Word, HTML and Typst.
elsevier-authorsElsevier's elsarticle front matter — \author, \ead, \corref, \affiliation, abstract, keywords, and the journal in the preprint footer — from the same metadata, plus the preamble elsarticle needs under pandoc.
ieee-authorsIEEEtran's author blocks and \begin{IEEEkeywords} from the same metadata.
yaml-in-latexA raw LaTeX block marked % template may use pandoc template syntax — $title$, $for(author)$…$endfor$ — with the project's metadata, for a title page or a footer of your own.
The Metadata document's YAML beside the title page it renders
The metadata the filters read, and the title page it becomes.

Try it: an abstract that is a document

  1. New Project ▸ Journal Article. Its Project Settings already list abstract-section and scholarly-authors.
  2. Open Front Matter ▸ Abstract and write the abstract as ordinary text. Open Metadata and fill in the authors:
author:
  - name: First Author
    email: [email protected]
    corresponding: true
    affiliations:
      - name: University
        department: Department of Earth & Planetary Sciences
  - name: Second Author
    affiliations:
      - name: Institute
date: today

Compile to PDF: the title block shows First Author1,*, Second Author2, the numbered affiliations, the corresponding author's address and today's date, and the abstract sits under them — no LaTeX typed anywhere. Switch the template to Elsevier or IEEE and the same metadata becomes that class's front matter instead.

Try it: a filter of your own in Templates ▸ Lua Filters

  1. Select the Templates folder, choose File ▸ New Folder and name it Lua Filters (the Example Project under Help already has one, with a twelve-line filter inside).
  2. With that folder selected, File ▸ New File…, name it small-caps-units.lua, and paste:
-- Units like "150 GPa" or "2300 K." in small capitals.
local units = { GPa = true, MPa = true, K = true, kbar = true }

function Str(el)
  local unit, tail = el.text:match("^(%a+)(%p*)$")
  if not (unit and units[unit]) then return nil end
  local out = { pandoc.SmallCaps({ pandoc.Str(unit) }) }
  if tail ~= "" then out[#out + 1] = pandoc.Str(tail) end
  return out
end

Then Project Settings ▸ Compile ▸ Lua filters ▸ Add ▸ From Templates. The preview updates at once, the filter syncs with the project to your other Macs, and because it is a document you can edit it in place — change the unit list and the next preview uses it.

On the web and iPad: built-in filters run on the Folio Writer server as they are; a filter from Templates ▸ Lua Filters runs there only when it is a byte-for-byte copy of a built-in — the server does not execute arbitrary Lua. A filter from a file on your Mac runs on that Mac only.

Example 1 · An always-current date

Type @@today anywhere; the compiled document carries the compile date — useful in cover letters and draft watermarks:

-- today.lua
function Str(el)
  if el.text == "@@today" then
    return pandoc.Str(os.date("%B %e, %Y"))
  end
end

Example 2 · Draft notes that never ship

Write notes-to-self in a fenced div, visible while drafting, guaranteed absent from output:

::: draft
Ask Wei whether the 2019 calibration applies here.
:::
-- strip-draft.lua
function Div(el)
  if el.classes:includes("draft") then
    return {}          -- deleted from every compiled format
  end
end

Attach it only to your submission profiles and the notes still appear in your advisor-draft profile — per-profile filters are exactly for this.

Example 3 · Your own inline markup

Give [key result]{.highlight} a real meaning per format — a yellow mark in HTML, a highlight in LaTeX PDFs:

-- highlight.lua
function Span(el)
  if not el.classes:includes("highlight") then return nil end
  if FORMAT:match("latex") then
    local out = pandoc.Inlines(pandoc.RawInline("latex", "\\hl{"))
    out:extend(el.content)
    out:insert(pandoc.RawInline("latex", "}"))
    return out
  elseif FORMAT:match("html") then
    local out = pandoc.Inlines(pandoc.RawInline("html", ""))
    out:extend(el.content)
    out:insert(pandoc.RawInline("html", ""))
    return out
  end
end

For the LaTeX branch, add the highlighting package once in the Format metadata: header-includes: \usepackage{soul}.

Debugging without guesswork

Two habits make filter-writing painless. To see the tree you're matching against, run pandoc -t native on a snippet in Terminal — it prints exactly the elements your functions will receive. And when a compile misbehaves, Compile & Preview Log shows the full pandoc command line and its output, filter errors included. One Folio-specific note: a filter whose file has moved is skipped rather than failing the compile — check the log if a filter seems silently inactive.

Where to go deeper

The pandoc Lua filter documentation lists every element and helper, and the community's filter collection is full of ready-made ones — abbreviation handling, table tweaks, diagram rendering. Anything there drops straight into the compile sheet.

Download Folio Writer free →  ·  More guides