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
Copy file name to clipboardExpand all lines: README.md
+43-3Lines changed: 43 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,14 +15,14 @@ Write HTML → get Word, PDFs, spreadsheets, and more — all with one unified T
15
15
16
16
## How It Works
17
17
18
-
Below is a high-level overview of the conversion pipeline. The library processes the HTML input through optional middleware steps, parses it into a structured intermediate representation, and then delegates to an adapter to generate the desired output format.
18
+
Below is a high-level overview of the conversion pipeline. The library processes the HTML input through optional plugin steps, parses it into a structured intermediate representation, and then delegates to an adapter to generate the desired output format.
-**Middleware**: One or more middleware functions can inspect or transform the HTML string before parsing (e.g., sanitization, custom tags).
25
+
-**Plugins**: `beforeParse` hooks can inspect or transform the HTML string before parsing, and `afterParse` hooks can transform parsed `DocumentElement[]`. Deprecated middleware still works through internal plugin adaptation.
26
26
-**Parser**: Converts the (possibly modified) HTML string into an array of `DocumentElement` objects, representing a structured AST.
27
27
-**Adapter**: Takes the parsed `DocumentElement[]` and renders it into the target format (e.g., DOCX, PDF, Markdown) via a registered adapter.
28
28
@@ -38,7 +38,7 @@ The stages are:
38
38
|**Style mapping engine**| Define your own css mappings for the adapters and set per‑format defaults |
39
39
|**Custom tag handlers**| Override or extend how any HTML tag is parsed |
40
40
|**Page sections & headers**| Use `<section class="page">`, `<section class="page-break">`, `<header>` and `<footer>` to control pages in DOCX |
41
-
|**Middleware pipeline**| Transform or sanitise HTML before parsing |
41
+
|**Plugin pipeline**| Transform HTML before parsing or transform `DocumentElement[]` after parsing|
42
42
43
43
---
44
44
@@ -183,6 +183,46 @@ const elements = await converter.parse('<p>Some HTML</p>');
@@ -121,6 +134,7 @@ Register a custom document converter adapter.
121
134
|[`InitOptions`](./types)| Options for initializing the converter via `init`. |
122
135
|[`ConverterOptions`](./types)| Internal options for the `Converter` constructor. |
123
136
|[`Converter`](./types)| Main class for conversion and parsing. |
137
+
|[`Plugin`](./types)| Optional `beforeParse` and `afterParse` hooks for extending the conversion pipeline. |
124
138
|[`Middleware`](./types)| Asynchronous function taking an HTML string and returning a Promise of string. |
125
139
|[`TagHandler`](./types)| Handler that processes an `HTMLElement` with optional `TagHandlerOptions` and returns a `DocumentElement` or an array of `DocumentElement`. |
Copy file name to clipboardExpand all lines: docs/docs/api/init.md
+63-9Lines changed: 63 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ sidebar_position: 2
7
7
8
8
# Initialization
9
9
10
-
The `init` function is your main entry point to configure and initialize the converter engine. It returns a `Converter` instance that can parse HTML and convert it into document formats like DOCX, PDF, or Markdown. Through `init`, you can register custom adapters, tag handlers, middleware, and default styles to control how HTML is interpreted and styled.
10
+
The `init` function is your main entry point to configure and initialize the converter engine. It returns a `Converter` instance that can parse HTML and convert it into document formats like DOCX, PDF, or Markdown. Through `init`, you can register custom adapters, tag handlers, plugins, and default styles to control how HTML is interpreted and styled.
11
11
12
12
## Quick Start
13
13
@@ -42,13 +42,62 @@ declare function init(options?: InitOptions): Converter;
42
42
43
43
The `options` object conforms to the [`InitOptions`](./types) type and supports the following properties:
44
44
45
+
### `plugins?: Plugin[]`
46
+
47
+
Register one or more plugins for the converter pipeline.
48
+
49
+
-**Type:**[`Plugin`](./types)[]
50
+
-**Default:** the built-in `minify` plugin is enabled unless disabled by `enableDefaultPlugins: false`, or implicitly by legacy `clearMiddleware: true`
51
+
-**Hooks:**
52
+
-`beforeParse?(html)` transforms the raw HTML string
53
+
-`afterParse?(elements)` transforms the parsed `DocumentElement[]`
54
+
-**Order:** plugins run in array order; all `beforeParse` hooks run before parsing and all `afterParse` hooks run after parsing
55
+
-**Errors:** plugin failures fail fast and surface their original errors
Copy file name to clipboardExpand all lines: docs/docs/api/middleware.md
+27-7Lines changed: 27 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,16 @@
2
2
id: middleware
3
3
title: Middleware
4
4
sidebar_label: Middleware
5
-
sidebar_position: 4
5
+
sidebar_position: 5
6
6
---
7
7
8
8
# Middleware
9
9
10
-
Middleware functions run on the HTML string _before_ it is parsed into `DocumentElement` nodes. They allow you to transform, sanitize, or minify the HTML content.
10
+
`middleware` is deprecated and kept as a compatibility layer for the newer plugin system.
11
+
12
+
Middleware functions still run on the HTML string _before_ it is parsed into `DocumentElement` nodes, but internally each middleware entry is adapted into a plugin with a `beforeParse` hook.
See the [Types Reference](./types) for the full definition.
21
25
22
-
## Default Middleware
26
+
## Default Middleware Behavior
27
+
28
+
The old built-in whitespace minifier now exists as the default `minify` plugin. `clearMiddleware: true` still disables it by default because it implies `enableDefaultPlugins: false` unless you explicitly override that.
29
+
30
+
The default behavior is still:
23
31
24
-
By default, `init()` applies a built-in whitespace-minifying middleware (unless you set `clearMiddleware: true`). This default middleware:
25
32
- Strips HTML comments (`<!-- ... -->`)
26
33
- Collapses consecutive whitespace into a single space (outside `<pre>`)
27
34
- Removes unnecessary whitespace between tags
28
35
- Trims leading and trailing whitespace
29
36
30
37
## Custom Middleware
31
38
32
-
There are two ways to register your own middleware:
> **Note:** Middleware functions are executed in the order they are passed in or registered. Make sure to arrange them accordingly if one depends on the output of another.
58
65
66
+
When both `plugins` and deprecated `middleware` are provided through `init()` or the `Converter` constructor, plugin `beforeParse` hooks run first and adapted middleware runs after them.
0 commit comments