mintdoc 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +240 -0
- package/dist/index.d.mts +64 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +488 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +447 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yourica
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="logo_mintdoc.svg" width="120" alt="Mintdoc" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">Mintdoc</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center"><strong>Mint perfect documents from templates.</strong></p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
A modern, lightweight document templating engine for <code>.docx</code> files.<br/>
|
|
11
|
+
Generate Word documents from templates and data — in Node.js or the browser.
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://www.npmjs.com/package/mintdoc"><img src="https://img.shields.io/npm/v/mintdoc" alt="npm version"/></a>
|
|
16
|
+
<a href="./LICENSE"><img src="https://img.shields.io/npm/l/mintdoc" alt="license"/></a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- **Variables** — `{firstName}`, `{company.name}` (dot-notation)
|
|
24
|
+
- **Loops** — `{#items}...{/items}` with automatic table row duplication
|
|
25
|
+
- **Conditions** — `{#if premium}...{:else}...{/if}`
|
|
26
|
+
- **Formatters** — `{name | uppercase}` with built-in and custom formatters
|
|
27
|
+
- **Loop metadata** — `{@index}`, `{@first}`, `{@last}`, `{.}`
|
|
28
|
+
- **Headers & footers** — template tags work everywhere in the document
|
|
29
|
+
- **TypeScript** — full typings included
|
|
30
|
+
- **Zero server dependency** — works in Node.js and the browser
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install mintdoc
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { Mintdoc } from "mintdoc";
|
|
46
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
47
|
+
|
|
48
|
+
const doc = new Mintdoc();
|
|
49
|
+
const template = readFileSync("template.docx");
|
|
50
|
+
|
|
51
|
+
const output = doc.render(template, {
|
|
52
|
+
firstName: "Alice",
|
|
53
|
+
company: { name: "Acme Corp" },
|
|
54
|
+
premium: true,
|
|
55
|
+
items: [
|
|
56
|
+
{ name: "Widget", price: "10 €" },
|
|
57
|
+
{ name: "Gadget", price: "20 €" },
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
writeFileSync("output.docx", output);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Or use the shorthand function for simple cases:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { render } from "mintdoc";
|
|
68
|
+
|
|
69
|
+
const output = render(template, { name: "Alice" });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Template Syntax
|
|
75
|
+
|
|
76
|
+
Create your template in Word, then use these tags in your document:
|
|
77
|
+
|
|
78
|
+
| Syntax | Description |
|
|
79
|
+
|---|---|
|
|
80
|
+
| `{variable}` | Simple variable |
|
|
81
|
+
| `{object.property}` | Dot-notation path |
|
|
82
|
+
| `{variable \| formatter}` | Apply a formatter |
|
|
83
|
+
| `{#items}...{/items}` | Loop over an array |
|
|
84
|
+
| `{#if condition}...{/if}` | Conditional block |
|
|
85
|
+
| `{#if condition}...{:else}...{/if}` | Conditional with else |
|
|
86
|
+
|
|
87
|
+
### Loop Metadata
|
|
88
|
+
|
|
89
|
+
Inside a loop, these special variables are available:
|
|
90
|
+
|
|
91
|
+
| Variable | Description |
|
|
92
|
+
|---|---|
|
|
93
|
+
| `{@index}` | Current iteration index (0-based) |
|
|
94
|
+
| `{@first}` | `true` on the first iteration |
|
|
95
|
+
| `{@last}` | `true` on the last iteration |
|
|
96
|
+
| `{.}` | Current item (for primitive arrays) |
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
{#items}
|
|
100
|
+
{name} — item {#if @first}(first){/if}{#if @last}(last){/if}
|
|
101
|
+
{/items}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Table Row Duplication
|
|
105
|
+
|
|
106
|
+
Place a loop around a table row to duplicate it for each item:
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
| {#items}{name} | {price}{/items} |
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Mintdoc automatically detects loop tags inside table cells and wraps the entire row — no manual configuration needed.
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## Built-in Formatters
|
|
117
|
+
|
|
118
|
+
| Formatter | Example | Output |
|
|
119
|
+
|---|---|---|
|
|
120
|
+
| `uppercase` | `{name \| uppercase}` | `ALICE` |
|
|
121
|
+
| `lowercase` | `{name \| lowercase}` | `alice` |
|
|
122
|
+
| `capitalize` | `{name \| capitalize}` | `Alice` |
|
|
123
|
+
|
|
124
|
+
Formatters can be chained: `{name | lowercase | capitalize}`
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Custom Formatters
|
|
129
|
+
|
|
130
|
+
Register your own formatters with `addFormatters()`:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
const doc = new Mintdoc().addFormatters({
|
|
134
|
+
date: (value) => new Date(String(value)).toLocaleDateString("en-US"),
|
|
135
|
+
euros: (value) => `${value} €`,
|
|
136
|
+
truncate: (value) => String(value).slice(0, 50),
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Then use them in your template:
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
Invoice date: {createdAt | date}
|
|
144
|
+
Total: {amount | euros}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Plugin System
|
|
150
|
+
|
|
151
|
+
Mintdoc has an open plugin API. Register plugins using `use()`:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { Mintdoc } from "mintdoc";
|
|
155
|
+
import { imagePlugin } from "@mintdoc/pro"; // Pro module (coming soon)
|
|
156
|
+
|
|
157
|
+
const doc = new Mintdoc().use(imagePlugin);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Plugins can add formatters and new tag types. Third-party plugins can use the same API.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Error Handling
|
|
165
|
+
|
|
166
|
+
Mintdoc throws typed errors for clear debugging:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
import { Mintdoc, MintdocParseError, MintdocRenderError } from "mintdoc";
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
const output = doc.render(template, data);
|
|
173
|
+
} catch (e) {
|
|
174
|
+
if (e instanceof MintdocParseError) {
|
|
175
|
+
// Malformed template (unclosed tag, mismatched tags…)
|
|
176
|
+
console.error("Template error:", e.message);
|
|
177
|
+
} else if (e instanceof MintdocRenderError) {
|
|
178
|
+
// Data mismatch (unknown formatter, loop on non-array…)
|
|
179
|
+
console.error("Render error:", e.message);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## API Reference
|
|
187
|
+
|
|
188
|
+
### `new Mintdoc()`
|
|
189
|
+
|
|
190
|
+
Creates a reusable instance. Configure once, render multiple times.
|
|
191
|
+
|
|
192
|
+
| Method | Description |
|
|
193
|
+
|---|---|
|
|
194
|
+
| `.use(plugin)` | Register a plugin |
|
|
195
|
+
| `.addFormatters(map)` | Register custom formatters |
|
|
196
|
+
| `.render(template, data)` | Render a `.docx` template |
|
|
197
|
+
|
|
198
|
+
All methods are chainable:
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
const doc = new Mintdoc()
|
|
202
|
+
.use(myPlugin)
|
|
203
|
+
.addFormatters({ date: (v) => new Date(String(v)).toLocaleDateString() });
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### `render(template, data, options?)`
|
|
207
|
+
|
|
208
|
+
Shorthand function — no instance required.
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { render } from "mintdoc";
|
|
212
|
+
|
|
213
|
+
const output = render(template, data, {
|
|
214
|
+
formatters: { date: (v) => new Date(String(v)).toLocaleDateString() },
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## Browser Usage
|
|
221
|
+
|
|
222
|
+
Mintdoc works in the browser. Pass an `ArrayBuffer` instead of a `Buffer`:
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
import { render } from "mintdoc";
|
|
226
|
+
|
|
227
|
+
const response = await fetch("/templates/invoice.docx");
|
|
228
|
+
const template = await response.arrayBuffer();
|
|
229
|
+
const output = render(template, { name: "Alice" });
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
|
|
236
|
+
MIT — see [LICENSE](./LICENSE).
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
> **Pro modules** — images, charts, dynamic tables, Excel, PowerPoint — coming soon at [mintdoc.dev](https://mintdoc.dev).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type Formatter = (value: unknown) => unknown;
|
|
2
|
+
type FormatterMap = Record<string, Formatter>;
|
|
3
|
+
interface MintdocPlugin {
|
|
4
|
+
name: string;
|
|
5
|
+
formatters?: FormatterMap;
|
|
6
|
+
}
|
|
7
|
+
interface RenderOptions {
|
|
8
|
+
formatters?: FormatterMap;
|
|
9
|
+
plugins?: MintdocPlugin[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare class MintdocParseError extends Error {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare class MintdocRenderError extends Error {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare const VERSION = "0.1.0";
|
|
21
|
+
/**
|
|
22
|
+
* Mintdoc — modern document templating engine.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { Mintdoc } from "mintdoc";
|
|
27
|
+
*
|
|
28
|
+
* const doc = new Mintdoc();
|
|
29
|
+
* const output = doc.render(templateBuffer, { name: "Alice" });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare class Mintdoc {
|
|
33
|
+
private plugins;
|
|
34
|
+
private extraFormatters;
|
|
35
|
+
/**
|
|
36
|
+
* Register a plugin (adds formatters, tag handlers, etc.).
|
|
37
|
+
*/
|
|
38
|
+
use(plugin: MintdocPlugin): this;
|
|
39
|
+
/**
|
|
40
|
+
* Register one or more custom formatters.
|
|
41
|
+
*/
|
|
42
|
+
addFormatters(formatters: FormatterMap): this;
|
|
43
|
+
/**
|
|
44
|
+
* Render a .docx template with the given data.
|
|
45
|
+
*
|
|
46
|
+
* @param template - The .docx file as a Buffer, ArrayBuffer or Uint8Array.
|
|
47
|
+
* @param data - The data object to inject into the template.
|
|
48
|
+
* @returns A Buffer containing the rendered .docx file.
|
|
49
|
+
*/
|
|
50
|
+
render(template: Buffer | ArrayBuffer | Uint8Array, data: Record<string, unknown>): Buffer;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Shorthand: render a template without creating an instance.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { render } from "mintdoc";
|
|
58
|
+
*
|
|
59
|
+
* const output = render(templateBuffer, { name: "Alice" });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare function render(template: Buffer | ArrayBuffer | Uint8Array, data: Record<string, unknown>, options?: RenderOptions): Buffer;
|
|
63
|
+
|
|
64
|
+
export { type FormatterMap, Mintdoc, MintdocParseError, type MintdocPlugin, MintdocRenderError, type RenderOptions, VERSION, render };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type Formatter = (value: unknown) => unknown;
|
|
2
|
+
type FormatterMap = Record<string, Formatter>;
|
|
3
|
+
interface MintdocPlugin {
|
|
4
|
+
name: string;
|
|
5
|
+
formatters?: FormatterMap;
|
|
6
|
+
}
|
|
7
|
+
interface RenderOptions {
|
|
8
|
+
formatters?: FormatterMap;
|
|
9
|
+
plugins?: MintdocPlugin[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare class MintdocParseError extends Error {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare class MintdocRenderError extends Error {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare const VERSION = "0.1.0";
|
|
21
|
+
/**
|
|
22
|
+
* Mintdoc — modern document templating engine.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { Mintdoc } from "mintdoc";
|
|
27
|
+
*
|
|
28
|
+
* const doc = new Mintdoc();
|
|
29
|
+
* const output = doc.render(templateBuffer, { name: "Alice" });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare class Mintdoc {
|
|
33
|
+
private plugins;
|
|
34
|
+
private extraFormatters;
|
|
35
|
+
/**
|
|
36
|
+
* Register a plugin (adds formatters, tag handlers, etc.).
|
|
37
|
+
*/
|
|
38
|
+
use(plugin: MintdocPlugin): this;
|
|
39
|
+
/**
|
|
40
|
+
* Register one or more custom formatters.
|
|
41
|
+
*/
|
|
42
|
+
addFormatters(formatters: FormatterMap): this;
|
|
43
|
+
/**
|
|
44
|
+
* Render a .docx template with the given data.
|
|
45
|
+
*
|
|
46
|
+
* @param template - The .docx file as a Buffer, ArrayBuffer or Uint8Array.
|
|
47
|
+
* @param data - The data object to inject into the template.
|
|
48
|
+
* @returns A Buffer containing the rendered .docx file.
|
|
49
|
+
*/
|
|
50
|
+
render(template: Buffer | ArrayBuffer | Uint8Array, data: Record<string, unknown>): Buffer;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Shorthand: render a template without creating an instance.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { render } from "mintdoc";
|
|
58
|
+
*
|
|
59
|
+
* const output = render(templateBuffer, { name: "Alice" });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare function render(template: Buffer | ArrayBuffer | Uint8Array, data: Record<string, unknown>, options?: RenderOptions): Buffer;
|
|
63
|
+
|
|
64
|
+
export { type FormatterMap, Mintdoc, MintdocParseError, type MintdocPlugin, MintdocRenderError, type RenderOptions, VERSION, render };
|