Filters

Apply text transformations by adding filter attributes to HTML elements.

Usage

Add a filter attribute to any HTML element to transform its text content. The attribute name is the filter, and its value is the argument.

html
<div uppercase>hello world</div>

Result:

html
<div>HELLO WORLD</div>

You may use multiple filters on one element — they run in attribute order:

html
<div append="!" prepend="Hey, ">world</div>

Result:

html
<div>Hey, world!</div>

More examples:

html
<div truncate="10">This is a longer sentence</div>
<!-- Result: This is a ... -->

<div replace="old|new">This is old text</div>
<!-- Result: This is new text -->

Nested filter elements are processed children-first.

Customization

Disabling filters

You may disable all filters by setting filters to false:

maizzle.config.ts
export default defineConfig({
  filters: false,
})

Custom filters

Define your own filters in the filters config. They are merged with the built-in defaults.

maizzle.config.ts
export default defineConfig({
  filters: {
    'big-text': (str) => `<span style="font-size: 24px">${str}</span>`,
  },
})

Usage:

html
<div big-text>Hello</div>

Result:

html
<div><span style="font-size: 24px">Hello</span></div>

Built-in filters

FilterDescriptionExample
uppercaseConvert to uppercase<div uppercase>
lowercaseConvert to lowercase<div lowercase>
capitalizeCapitalize first letter<div capitalize>
trimTrim whitespace<div trim>
lstripTrim leading whitespace<div lstrip>
rstripTrim trailing whitespace<div rstrip>
escapeHTML-escape characters<div escape>
escape-onceDecode then escape<div escape-once>
appendAppend string<div append="!">
prependPrepend string<div prepend="hi ">
plusAdd number<div plus="5">10</div>15
minusSubtract<div minus="3">10</div>7
multiply / timesMultiply<div multiply="2">5</div>10
divide-by / divideDivide<div divide="2">10</div>5
moduloModulo<div modulo="3">10</div>1
ceilRound up<div ceil>3.2</div>4
floorRound down<div floor>3.8</div>3
roundRound<div round>3.5</div>4
sizeString length<div size>hello</div>5
sliceSlice string<div slice="0,3">hello</div>hel
truncateTruncate with ellipsis<div truncate="5">hello world</div>hello...
truncate-wordsTruncate by words<div truncate-words="2">a b c</div>a b...
removeRemove all occurrences<div remove="l">hello</div>heo
remove-firstRemove first match<div remove-first="l">hello</div>helo
replaceReplace all (pipe-sep)<div replace="l|r">hello</div>herro
replace-firstReplace first<div replace-first="l|r">hello</div>herlo
newline-to-br\n<br><div newline-to-br>
strip-newlinesRemove newlines<div strip-newlines>
url-decodeURL decode<div url-decode>
url-encodeURL encode<div url-encode>

API

ts
import { filters } from '@maizzle/framework'

// Built-in filters only
const out = filters('<p uppercase>foo</p>')
// → '<p>FOO</p>'

// Add your own filter (merged with the defaults)
const custom = filters('<p suffix=" world">hello</p>', {
  suffix: (str, value) => str + value,
})

// Disable every filter (including the defaults)
const raw = filters('<p uppercase>foo</p>', false)

The first argument is an HTML string. The second is an optional FiltersConfig — an object whose keys are attribute names and values are (content, attrValue) => string functions, or false to disable all filters. Returns the transformed HTML string.