md2x 0.5.2 → 0.7.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/README.md +196 -14
- package/dist/chunks/{chunk-5DRL26RK.js → chunk-EFKQH5UR.js} +23 -23
- package/dist/chunks/chunk-XTD75532.js +1023 -0
- package/dist/chunks/{docx-exporter-FGXU5MTK.js → docx-exporter-ZZ76JXFU.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/md2x.js +11 -9
- package/dist/renderer/puppeteer-render-worker.js +97 -90
- package/dist/templates/html/example.html +35 -0
- package/dist/templates/vue/PrinterComponent.vue +875 -0
- package/dist/templates/vue/example.vue +17 -0
- package/dist/types/node/src/host/browser-renderer.d.ts +120 -0
- package/dist/types/node/src/host/index.d.ts +16 -2
- package/dist/types/node/src/host/node-exporter.d.ts +54 -23
- package/dist/types/node/src/index.d.ts +4 -4
- package/dist/types/src/plugins/md2x-plugin.d.ts +9 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-KEWRLSTT.js +0 -723
package/README.md
CHANGED
|
@@ -1,9 +1,173 @@
|
|
|
1
1
|
# md2x
|
|
2
2
|
|
|
3
|
-
Markdown → PDF/DOCX/HTML converter (local, no server). Supports Mermaid/Graphviz/Infographic/Vega/HTML/SVG rendering, math, and code highlighting.
|
|
3
|
+
Markdown → PDF/DOCX/HTML/Image converter (local, no server). Supports Mermaid/Graphviz/Infographic/Vega/HTML/SVG rendering, math, and code highlighting.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/md2x)
|
|
6
6
|
|
|
7
|
+
## CLI Options
|
|
8
|
+
|
|
9
|
+
| Option | Alias | Description | Default | Values |
|
|
10
|
+
|--------|-------|-------------|---------|--------|
|
|
11
|
+
| `--help` | `-h` | Show help message | - | - |
|
|
12
|
+
| `--version` | `-v` | Show version number | - | - |
|
|
13
|
+
| `--output` | `-o` | Output file path | Input name with format extension | File path |
|
|
14
|
+
| `--format` | `-f` | Output format | `pdf` | `pdf`, `docx`, `html`, `png`, `jpg/jpeg`, `webp` |
|
|
15
|
+
| `--theme` | `-t` | Theme name | `default` | See `--list-themes` |
|
|
16
|
+
| `--diagram-mode` | - | HTML/Image diagram rendering mode | `live` | `img`, `live`, `none` |
|
|
17
|
+
| `--hr-page-break` | - | Convert horizontal rules to page breaks | `true` for PDF/DOCX, `false` for HTML/Image | `true`, `false` |
|
|
18
|
+
| `--templates-dir` | - | Extra template dir for md2x blocks (repeatable; resolved against input dir when relative) | - | Directory path |
|
|
19
|
+
| `--list-themes` | - | List all available themes | - | - |
|
|
20
|
+
|
|
21
|
+
### Diagram Modes (HTML/Image)
|
|
22
|
+
|
|
23
|
+
- **`live`** (default): Render diagrams in the browser on load using online CDN scripts (Mermaid, @viz-js/viz, Vega-Lite, Infographic)
|
|
24
|
+
- **`img`**: Pre-render diagrams as embedded images (offline, stable; no CDN)
|
|
25
|
+
- **`none`**: Keep diagram source blocks only (no rendering)
|
|
26
|
+
|
|
27
|
+
## Front Matter Options
|
|
28
|
+
|
|
29
|
+
When converting a markdown **file**, you can put options in YAML front matter (the CLI merges front matter with CLI flags; explicit CLI flags win).
|
|
30
|
+
|
|
31
|
+
### Common (All Formats)
|
|
32
|
+
|
|
33
|
+
```yaml
|
|
34
|
+
---
|
|
35
|
+
format: pdf # pdf | docx | html | png | jpg | jpeg | webp
|
|
36
|
+
theme: default
|
|
37
|
+
hrAsPageBreak: true
|
|
38
|
+
---
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### PDF
|
|
42
|
+
|
|
43
|
+
```yaml
|
|
44
|
+
---
|
|
45
|
+
format: pdf
|
|
46
|
+
title: "My Doc" # used for PDF metadata/header templates
|
|
47
|
+
pdf:
|
|
48
|
+
format: A4 # A4 | Letter | Legal | A3 | A5
|
|
49
|
+
landscape: false
|
|
50
|
+
margin:
|
|
51
|
+
top: 1cm
|
|
52
|
+
bottom: 1cm
|
|
53
|
+
left: 1cm
|
|
54
|
+
right: 1cm
|
|
55
|
+
printBackground: true
|
|
56
|
+
scale: 1
|
|
57
|
+
displayHeaderFooter: false
|
|
58
|
+
headerTemplate: "<div style='font-size:10px;width:100%;text-align:center;'><span class='title'></span></div>"
|
|
59
|
+
footerTemplate: "<div style='font-size:10px;width:100%;text-align:center;'>Page <span class='pageNumber'></span> / <span class='totalPages'></span></div>"
|
|
60
|
+
---
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### DOCX
|
|
64
|
+
|
|
65
|
+
```yaml
|
|
66
|
+
---
|
|
67
|
+
format: docx
|
|
68
|
+
theme: default
|
|
69
|
+
hrAsPageBreak: true
|
|
70
|
+
---
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### HTML
|
|
74
|
+
|
|
75
|
+
```yaml
|
|
76
|
+
---
|
|
77
|
+
format: html
|
|
78
|
+
title: "My Doc"
|
|
79
|
+
standalone: true # full HTML document (default)
|
|
80
|
+
baseTag: true # emit <base href="file://.../"> for resolving relative paths (default)
|
|
81
|
+
diagramMode: live # img | live | none
|
|
82
|
+
cdn: # optional: override CDN URLs (used when diagramMode: live)
|
|
83
|
+
mermaid: "https://cdn.jsdelivr.net/npm/mermaid@11.12.2/dist/mermaid.min.js"
|
|
84
|
+
---
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Image (PNG/JPEG/WebP)
|
|
88
|
+
|
|
89
|
+
```yaml
|
|
90
|
+
---
|
|
91
|
+
format: png
|
|
92
|
+
diagramMode: live # or "img" for offline (no CDN)
|
|
93
|
+
image:
|
|
94
|
+
# selector can be a string or an array of selectors (CSS selector list).
|
|
95
|
+
selector:
|
|
96
|
+
- 'div.md2x-diagram[data-md2x-diagram-kind="mermaid"]'
|
|
97
|
+
- 'div.md2x-diagram[data-md2x-diagram-kind="infographic"]'
|
|
98
|
+
# selectorMode: first | each | union | stitch (default: stitch)
|
|
99
|
+
# - union: capture the union bounding box (includes in-between page content)
|
|
100
|
+
# - stitch: stack matched elements and capture only them (no in-between content)
|
|
101
|
+
selectorMode: stitch
|
|
102
|
+
selectorGap: 16 # optional: vertical gap (px) between stitched elements
|
|
103
|
+
selectorPadding: 8 # optional: padding (px) around the stitched region
|
|
104
|
+
split: auto # optional: split very tall output into multiple images
|
|
105
|
+
---
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
When `image.split` produces multiple parts, outputs are written as `output.part-001.png`, `output.part-002.png`, ...
|
|
109
|
+
|
|
110
|
+
Diagram blocks are tagged with `data-md2x-diagram-kind` so you can target specific types via selectors:
|
|
111
|
+
|
|
112
|
+
```yaml
|
|
113
|
+
image:
|
|
114
|
+
selector: 'div.md2x-diagram[data-md2x-diagram-kind="mermaid"]'
|
|
115
|
+
selectorMode: stitch
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## md2x Template Blocks
|
|
119
|
+
|
|
120
|
+
Besides diagram blocks (mermaid/dot/vega-lite/infographic), `md2x` also supports template blocks via:
|
|
121
|
+
|
|
122
|
+
````md
|
|
123
|
+
```md2x
|
|
124
|
+
{
|
|
125
|
+
type: 'vue', // "vue" | "html"
|
|
126
|
+
template: 'example.vue',
|
|
127
|
+
data: [{ title: 't', message: 'm' }]
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
````
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
//example.vue
|
|
134
|
+
<script setup>
|
|
135
|
+
const data = templateData;
|
|
136
|
+
</script>
|
|
137
|
+
|
|
138
|
+
<template>
|
|
139
|
+
<div class="my-component">Hello md2x! This is vue template</div>
|
|
140
|
+
<div v-for="(item, index) in data" :key="index">
|
|
141
|
+
<h2>{{ item.title }}</h2>
|
|
142
|
+
<p>{{ item.message }}</p>
|
|
143
|
+
</div>
|
|
144
|
+
</template>
|
|
145
|
+
|
|
146
|
+
<style scoped>
|
|
147
|
+
.my-component {
|
|
148
|
+
color: red;
|
|
149
|
+
}
|
|
150
|
+
</style>
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Config Fields
|
|
154
|
+
|
|
155
|
+
- `type`: `"vue"` or `"html"`
|
|
156
|
+
- `template`: template file name/path
|
|
157
|
+
- if you only pass a filename (e.g. `example.vue`), it is treated as `${type}/${template}` (e.g. `vue/example.vue`)
|
|
158
|
+
- `data`: arbitrary JSON-serializable data (injected by replacing the `templateData` placeholder)
|
|
159
|
+
- `allowScripts` (optional, **unsafe**, html only): when exporting **images** in `diagramMode: "img"`, set `allowScripts: true` to execute inline `<script>` blocks before rendering to PNG.
|
|
160
|
+
- not supported: `<script type="module">`
|
|
161
|
+
- external `<script src="...">` is not supported for image rendering (use inline scripts)
|
|
162
|
+
|
|
163
|
+
### Template Resolution (External Templates)
|
|
164
|
+
|
|
165
|
+
To load templates from outside the built-in `dist/templates`, use either:
|
|
166
|
+
|
|
167
|
+
- CLI: `--templates-dir /path/to/templates` (repeatable)
|
|
168
|
+
- Front matter: `templatesDir: /path/to/templates` (string or list)
|
|
169
|
+
|
|
170
|
+
|
|
7
171
|
## Usage
|
|
8
172
|
|
|
9
173
|
Export to PDF:
|
|
@@ -23,21 +187,10 @@ Export to HTML:
|
|
|
23
187
|
npx md2x input.md -f html
|
|
24
188
|
```
|
|
25
189
|
|
|
26
|
-
|
|
190
|
+
Export to PNG:
|
|
27
191
|
|
|
28
192
|
```bash
|
|
29
|
-
|
|
30
|
-
# - uses online CDN scripts (Mermaid/@viz-js/viz/Vega-Lite/Infographic)
|
|
31
|
-
npx md2x input.md -f html --diagram-mode live
|
|
32
|
-
|
|
33
|
-
# Pre-render diagrams as embedded images (offline, stable)
|
|
34
|
-
npx md2x input.md -f html --diagram-mode img
|
|
35
|
-
|
|
36
|
-
# Render diagrams in the browser on load (keeps source blocks)
|
|
37
|
-
# Tip: Vega-Lite CDN major version is auto-selected from the spec $schema (v5 or v6).
|
|
38
|
-
|
|
39
|
-
# Keep diagram source blocks only (no rendering)
|
|
40
|
-
npx md2x input.md -f html --diagram-mode none
|
|
193
|
+
npx md2x input.md -f png -o output.png
|
|
41
194
|
```
|
|
42
195
|
|
|
43
196
|
List themes:
|
|
@@ -58,6 +211,35 @@ Help:
|
|
|
58
211
|
npx md2x -h
|
|
59
212
|
```
|
|
60
213
|
|
|
214
|
+
## MCP server (Model Context Protocol)
|
|
215
|
+
|
|
216
|
+
This repo includes an Express-based MCP server that exposes `md2x` as MCP tools over HTTP, so MCP clients can convert Markdown and download the generated HTML/PDF/DOCX from `/resources`.
|
|
217
|
+
|
|
218
|
+
Run:
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
pnpm -C mcp install
|
|
222
|
+
pnpm -C mcp start
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Endpoints:
|
|
226
|
+
|
|
227
|
+
- Streamable HTTP (recommended): `POST/GET/DELETE /mcp`
|
|
228
|
+
- Legacy HTTP+SSE: `GET /sse` and `POST /messages?sessionId=...`
|
|
229
|
+
- Resources (static files): `GET /resources/*`
|
|
230
|
+
|
|
231
|
+
Tools:
|
|
232
|
+
|
|
233
|
+
- `markdown_to_html` / `markdown_to_pdf` / `markdown_to_docx` - Convert Markdown to HTML/PDF/DOCX
|
|
234
|
+
- `markdown_to_image` - Convert Markdown to an image (`png`/`jpg`/`jpeg`/`webp`), may return multiple parts for very tall pages
|
|
235
|
+
- `markdown_convert` - Auto convert via `md2x.convert()` (front matter supported; includes image formats)
|
|
236
|
+
- `resources_upload` - Upload a file to `/resources` (e.g. images referenced by Markdown)
|
|
237
|
+
|
|
238
|
+
Notes:
|
|
239
|
+
|
|
240
|
+
- The conversion tools return an MCP `resource_link` pointing to the generated file URL.
|
|
241
|
+
- Config: `PORT` (default `3000`) and `MD2X_BASE_URL` (used to build the public `/resources` URL). See `mcp/README.md`.
|
|
242
|
+
|
|
61
243
|
## Puppeteer / Chrome install
|
|
62
244
|
|
|
63
245
|
This package depends on `puppeteer`. On first install, Puppeteer downloads a compatible "Chrome for Testing" build (cached under your user directory). Set `PUPPETEER_SKIP_DOWNLOAD=1` to skip download and use a system Chrome via `PUPPETEER_EXECUTABLE_PATH`.
|