You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rewriter: Expose rewrite/rewriteString functions and refine types (#786)
This pull request exposes new `rewrite` and `rewriteString` functions in
the `@herb-tools/rewriter` package. This allows for more ergonomic use
of rewriters.
Examples:
**`rewrite`**:
```ts
import { Herb } from "@herb-tools/node-wasm"
import { rewrite } from "@herb-tools/rewriter"
import { tailwindClassSorter } from "@herb-tools/rewriter/loader"
await Herb.load()
const template = `<div class="text-red-500 p-4 mt-2"></div>`
const parseResult = Herb.parse(template)
const { node, output } = await rewrite(template, [tailwindClassSorter()])
output
// Result: "<div class="mt-2 p-4 text-red-500"></div>"
```
**`rewriteString`**:
```ts
import { Herb } from "@herb-tools/node-wasm"
import { rewrite } from "@herb-tools/rewriter"
import { tailwindClassSorter } from "@herb-tools/rewriter/loader"
const template = `<div class="text-red-500 p-4 mt-2"></div>`
const output = await rewriteString(Herb, template, [tailwindClassSorter()])
// Result: "<div class="mt-2 p-4 text-red-500"></div>"
```
Additionally, it cleans up the interface of the rewriters to accept and
return `Node` instead of `ParseResult`. This allows for more flexibility
and also allows for partial rewrites of a tree/document.
Rewriter system for transforming HTML+ERB AST nodes and formatted strings. Provides base classes and utilities for creating custom rewriters that can modify templates.
8
8
9
-
## Overview
10
-
11
-
The rewriter package provides a plugin architecture for transforming HTML+ERB templates. Rewriters can be used to transform templates before formatting, implement linter autofixes, or perform any custom AST or string transformations.
The rewriter package provides a plugin architecture for transforming HTML+ERB templates. Rewriters can be used to transform templates before formatting, implement linter autofixes, or perform any custom AST or string transformations.
**Note:**`rewrite()` returns both the rewritten string (`output`) and the transformed AST (`node`), which allows for partial rewrites and further processing. `rewriteString()` is a convenience wrapper that returns just the string.
85
+
40
86
## Built-in Rewriters
41
87
42
88
### Tailwind Class Sorter
@@ -45,18 +91,22 @@ Automatically sorts Tailwind CSS classes in `class` attributes according to Tail
0 commit comments