Language guide · 03

Filters

Filters transform values before Knap writes them. Browse the complete library, then open any filter for tested input, template, and output examples.

Use filters

Add a filter after a pipe. Parameters follow the filter name after a colon, and chains run from left to right.

template.mdKNAP
1{{ title | trim | upper }}2{{ published | date:"YYYY-MM-DD" }}3{{ tags | unique | join:", " }}

Filter directory

Open search from the header or press ⌘K to find a filter by name, alias, category, or behavior. Every page includes at least one copyable example.

Dates and time

Parse, adjust, and format dates or durations.

Text

Normalize case, spacing, file names, and encoded text.

Markdown

Create links, callouts, lists, tables, and other Markdown structures.

Numbers

Calculate, round, and format numeric values.

Collections

Select, reshape, combine, and render arrays and objects.

HTML cleanup

Clean markup while preserving the pieces a Markdown workflow needs.

HTML preset

DOM-dependent filters exported separately from knap/html.

Opt into the HTML preset

html_to_json and remove_html require browser-compatible DOM globals, so they are exported separately from knap/html.

engine.tsTS
1import { createEngine, standardFilters } from 'knap';2import { htmlFilters } from 'knap/html';34const engine = createEngine({5  filters: { ...standardFilters, ...htmlFilters },6});

Register a custom filter

Custom filters may be synchronous or asynchronous. Attach metadata when the editor should validate parameters before rendering.

filters.tsTS
1import { createEngine, standardFilters, type TemplateFilter } from 'knap';23const surround: TemplateFilter = (value, param = '') => {4  const marker = param.replace(/^(['"])(.*)\1$/s, '$2');5  return marker + value + marker;6};78surround.metadata = { example: 'surround:"**"' };910const engine = createEngine({11  filters: { ...standardFilters, surround },12});

A filter can call context.reportWarning() when it preserves a fallback value but wants the host to surface a non-fatal diagnostic.