@helping-ai-workflow/md2doc 1.0.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 +64 -0
- package/bin/md2html.js +134 -0
- package/bin/md2pdf.js +133 -0
- package/lib/md2doc.js +1351 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 helping-ai-workflow
|
|
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,64 @@
|
|
|
1
|
+
# @helping-ai-workflow/md2doc
|
|
2
|
+
|
|
3
|
+
> Markdown → HTML / PDF renderer with WaveDrom, Mermaid, and Graphviz support.
|
|
4
|
+
|
|
5
|
+
Two global CLIs (`md2html`, `md2pdf`) you can call from any directory.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g @helping-ai-workflow/md2doc
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Node.js 18 or higher. The first install pulls puppeteer (≈ 170 MB Chromium download); subsequent installs reuse it.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
md2html foo.md # → foo.html (next to source)
|
|
19
|
+
md2html foo.md bar.md # batch render
|
|
20
|
+
md2html foo.md --out custom.html # explicit output (single-file mode)
|
|
21
|
+
md2html foo.md --open # render then launch viewer
|
|
22
|
+
md2html foo.md --quiet # suppress progress output
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`md2pdf` accepts the same four flags with PDF-output semantics.
|
|
26
|
+
|
|
27
|
+
### Flags
|
|
28
|
+
|
|
29
|
+
| Flag | Meaning |
|
|
30
|
+
|---|---|
|
|
31
|
+
| `--out <path>` | Explicit output path. Only valid with exactly one input. |
|
|
32
|
+
| `--open` | Launch the platform viewer (`xdg-open` / `open` / `start`) after render. |
|
|
33
|
+
| `--quiet` | Suppress per-file progress messages. |
|
|
34
|
+
| `--version`, `-v` | Print version. |
|
|
35
|
+
| `--help`, `-h` | Print help. |
|
|
36
|
+
|
|
37
|
+
## Supported diagram types
|
|
38
|
+
|
|
39
|
+
Embedded in fenced code blocks inside your Markdown:
|
|
40
|
+
|
|
41
|
+
````markdown
|
|
42
|
+
```mermaid
|
|
43
|
+
graph LR
|
|
44
|
+
A --> B
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```wavedrom
|
|
48
|
+
{ "signal": [...] }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```dot
|
|
52
|
+
digraph G { A -> B }
|
|
53
|
+
```
|
|
54
|
+
````
|
|
55
|
+
|
|
56
|
+
Diagrams render directly in the output (HTML or PDF).
|
|
57
|
+
|
|
58
|
+
## Why a global CLI
|
|
59
|
+
|
|
60
|
+
Multiple repos used to ship copies of this script. They drifted. This package centralises the renderer so every repo references the same version. See [`docs/why.md`](https://github.com/helping-ai-workflow/md2doc) for background.
|
|
61
|
+
|
|
62
|
+
## Licence
|
|
63
|
+
|
|
64
|
+
MIT.
|
package/bin/md2html.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const { spawnSync } = require('child_process');
|
|
7
|
+
|
|
8
|
+
const FORMAT = 'html';
|
|
9
|
+
const LIB = path.resolve(__dirname, '..', 'lib', 'md2doc.js');
|
|
10
|
+
const VERSION = require('../package.json').version;
|
|
11
|
+
|
|
12
|
+
function printHelp() {
|
|
13
|
+
process.stdout.write([
|
|
14
|
+
`md2${FORMAT} — render Markdown to ${FORMAT.toUpperCase()} (WaveDrom / Mermaid / Graphviz supported)`,
|
|
15
|
+
'',
|
|
16
|
+
'Usage:',
|
|
17
|
+
` md2${FORMAT} <input.md> [<input2.md> ...] # render each to <stem>.${FORMAT} next to source`,
|
|
18
|
+
` md2${FORMAT} <input.md> --out <path.${FORMAT}> # explicit output (single-file mode)`,
|
|
19
|
+
` md2${FORMAT} <input.md> --open # render then open viewer`,
|
|
20
|
+
` md2${FORMAT} <input.md> --quiet # suppress progress output`,
|
|
21
|
+
'',
|
|
22
|
+
'Flags:',
|
|
23
|
+
' --out <path> Explicit output path. Only valid with exactly one input.',
|
|
24
|
+
' --open Launch the platform viewer after render.',
|
|
25
|
+
' --quiet Suppress per-file progress messages.',
|
|
26
|
+
' --version, -v Print version.',
|
|
27
|
+
' --help, -h Print this help.',
|
|
28
|
+
''
|
|
29
|
+
].join('\n'));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function parseArgs(argv) {
|
|
33
|
+
const inputs = [];
|
|
34
|
+
let out = null;
|
|
35
|
+
let open = false;
|
|
36
|
+
let quiet = false;
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < argv.length; i++) {
|
|
39
|
+
const a = argv[i];
|
|
40
|
+
if (a === '--version' || a === '-v') {
|
|
41
|
+
process.stdout.write(VERSION + '\n');
|
|
42
|
+
process.exit(0);
|
|
43
|
+
}
|
|
44
|
+
if (a === '--help' || a === '-h') {
|
|
45
|
+
printHelp();
|
|
46
|
+
process.exit(0);
|
|
47
|
+
}
|
|
48
|
+
if (a === '--out') {
|
|
49
|
+
if (out !== null) {
|
|
50
|
+
process.stderr.write('error: --out specified more than once\n');
|
|
51
|
+
process.exit(2);
|
|
52
|
+
}
|
|
53
|
+
i++;
|
|
54
|
+
if (i >= argv.length) {
|
|
55
|
+
process.stderr.write('error: --out requires a value\n');
|
|
56
|
+
process.exit(2);
|
|
57
|
+
}
|
|
58
|
+
out = argv[i];
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (a === '--open') { open = true; continue; }
|
|
62
|
+
if (a === '--quiet') { quiet = true; continue; }
|
|
63
|
+
if (a.startsWith('-')) {
|
|
64
|
+
process.stderr.write(`error: unknown flag ${a}\n`);
|
|
65
|
+
process.exit(2);
|
|
66
|
+
}
|
|
67
|
+
inputs.push(a);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (inputs.length === 0) {
|
|
71
|
+
printHelp();
|
|
72
|
+
process.exit(2);
|
|
73
|
+
}
|
|
74
|
+
if (out !== null && inputs.length !== 1) {
|
|
75
|
+
process.stderr.write('error: --out is only valid with exactly one input file\n');
|
|
76
|
+
process.exit(2);
|
|
77
|
+
}
|
|
78
|
+
return { inputs, out, open, quiet };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function deriveOutput(input, format) {
|
|
82
|
+
const stem = input.replace(/\.md$/i, '');
|
|
83
|
+
if (stem === input) {
|
|
84
|
+
// No .md extension — append .<format>.
|
|
85
|
+
return input + '.' + format;
|
|
86
|
+
}
|
|
87
|
+
return stem + '.' + format;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function openViewer(filePath) {
|
|
91
|
+
const platform = process.platform;
|
|
92
|
+
let cmd, args;
|
|
93
|
+
if (platform === 'darwin') {
|
|
94
|
+
cmd = 'open'; args = [filePath];
|
|
95
|
+
} else if (platform === 'win32') {
|
|
96
|
+
cmd = 'cmd'; args = ['/c', 'start', '""', filePath];
|
|
97
|
+
} else {
|
|
98
|
+
cmd = 'xdg-open'; args = [filePath];
|
|
99
|
+
}
|
|
100
|
+
const r = spawnSync(cmd, args, { stdio: 'ignore' });
|
|
101
|
+
if (r.error || r.status !== 0) {
|
|
102
|
+
process.stderr.write(`warning: could not launch viewer for ${filePath}\n`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function main() {
|
|
107
|
+
const args = parseArgs(process.argv.slice(2));
|
|
108
|
+
const outputs = [];
|
|
109
|
+
|
|
110
|
+
for (const input of args.inputs) {
|
|
111
|
+
if (!fs.existsSync(input)) {
|
|
112
|
+
process.stderr.write(`error: input not found: ${input}\n`);
|
|
113
|
+
process.exit(1);
|
|
114
|
+
}
|
|
115
|
+
const output = (args.out !== null) ? args.out : deriveOutput(input, FORMAT);
|
|
116
|
+
if (!args.quiet) {
|
|
117
|
+
process.stdout.write(`[${FORMAT.toUpperCase()}] ${input} → ${output}\n`);
|
|
118
|
+
}
|
|
119
|
+
const r = spawnSync(process.execPath, [LIB, input, output], { stdio: 'inherit' });
|
|
120
|
+
if (r.status !== 0) {
|
|
121
|
+
process.stderr.write(`error: render failed for ${input} (exit ${r.status})\n`);
|
|
122
|
+
process.exit(r.status || 1);
|
|
123
|
+
}
|
|
124
|
+
outputs.push(output);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (args.open) {
|
|
128
|
+
for (const o of outputs) {
|
|
129
|
+
openViewer(o);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
main();
|
package/bin/md2pdf.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const { spawnSync } = require('child_process');
|
|
7
|
+
|
|
8
|
+
const FORMAT = 'pdf';
|
|
9
|
+
const LIB = path.resolve(__dirname, '..', 'lib', 'md2doc.js');
|
|
10
|
+
const VERSION = require('../package.json').version;
|
|
11
|
+
|
|
12
|
+
function printHelp() {
|
|
13
|
+
process.stdout.write([
|
|
14
|
+
`md2${FORMAT} — render Markdown to ${FORMAT.toUpperCase()} (WaveDrom / Mermaid / Graphviz supported)`,
|
|
15
|
+
'',
|
|
16
|
+
'Usage:',
|
|
17
|
+
` md2${FORMAT} <input.md> [<input2.md> ...] # render each to <stem>.${FORMAT} next to source`,
|
|
18
|
+
` md2${FORMAT} <input.md> --out <path.${FORMAT}> # explicit output (single-file mode)`,
|
|
19
|
+
` md2${FORMAT} <input.md> --open # render then open viewer`,
|
|
20
|
+
` md2${FORMAT} <input.md> --quiet # suppress progress output`,
|
|
21
|
+
'',
|
|
22
|
+
'Flags:',
|
|
23
|
+
' --out <path> Explicit output path. Only valid with exactly one input.',
|
|
24
|
+
' --open Launch the platform viewer after render.',
|
|
25
|
+
' --quiet Suppress per-file progress messages.',
|
|
26
|
+
' --version, -v Print version.',
|
|
27
|
+
' --help, -h Print this help.',
|
|
28
|
+
''
|
|
29
|
+
].join('\n'));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function parseArgs(argv) {
|
|
33
|
+
const inputs = [];
|
|
34
|
+
let out = null;
|
|
35
|
+
let open = false;
|
|
36
|
+
let quiet = false;
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < argv.length; i++) {
|
|
39
|
+
const a = argv[i];
|
|
40
|
+
if (a === '--version' || a === '-v') {
|
|
41
|
+
process.stdout.write(VERSION + '\n');
|
|
42
|
+
process.exit(0);
|
|
43
|
+
}
|
|
44
|
+
if (a === '--help' || a === '-h') {
|
|
45
|
+
printHelp();
|
|
46
|
+
process.exit(0);
|
|
47
|
+
}
|
|
48
|
+
if (a === '--out') {
|
|
49
|
+
if (out !== null) {
|
|
50
|
+
process.stderr.write('error: --out specified more than once\n');
|
|
51
|
+
process.exit(2);
|
|
52
|
+
}
|
|
53
|
+
i++;
|
|
54
|
+
if (i >= argv.length) {
|
|
55
|
+
process.stderr.write('error: --out requires a value\n');
|
|
56
|
+
process.exit(2);
|
|
57
|
+
}
|
|
58
|
+
out = argv[i];
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (a === '--open') { open = true; continue; }
|
|
62
|
+
if (a === '--quiet') { quiet = true; continue; }
|
|
63
|
+
if (a.startsWith('-')) {
|
|
64
|
+
process.stderr.write(`error: unknown flag ${a}\n`);
|
|
65
|
+
process.exit(2);
|
|
66
|
+
}
|
|
67
|
+
inputs.push(a);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (inputs.length === 0) {
|
|
71
|
+
printHelp();
|
|
72
|
+
process.exit(2);
|
|
73
|
+
}
|
|
74
|
+
if (out !== null && inputs.length !== 1) {
|
|
75
|
+
process.stderr.write('error: --out is only valid with exactly one input file\n');
|
|
76
|
+
process.exit(2);
|
|
77
|
+
}
|
|
78
|
+
return { inputs, out, open, quiet };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function deriveOutput(input, format) {
|
|
82
|
+
const stem = input.replace(/\.md$/i, '');
|
|
83
|
+
if (stem === input) {
|
|
84
|
+
return input + '.' + format;
|
|
85
|
+
}
|
|
86
|
+
return stem + '.' + format;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function openViewer(filePath) {
|
|
90
|
+
const platform = process.platform;
|
|
91
|
+
let cmd, args;
|
|
92
|
+
if (platform === 'darwin') {
|
|
93
|
+
cmd = 'open'; args = [filePath];
|
|
94
|
+
} else if (platform === 'win32') {
|
|
95
|
+
cmd = 'cmd'; args = ['/c', 'start', '""', filePath];
|
|
96
|
+
} else {
|
|
97
|
+
cmd = 'xdg-open'; args = [filePath];
|
|
98
|
+
}
|
|
99
|
+
const r = spawnSync(cmd, args, { stdio: 'ignore' });
|
|
100
|
+
if (r.error || r.status !== 0) {
|
|
101
|
+
process.stderr.write(`warning: could not launch viewer for ${filePath}\n`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function main() {
|
|
106
|
+
const args = parseArgs(process.argv.slice(2));
|
|
107
|
+
const outputs = [];
|
|
108
|
+
|
|
109
|
+
for (const input of args.inputs) {
|
|
110
|
+
if (!fs.existsSync(input)) {
|
|
111
|
+
process.stderr.write(`error: input not found: ${input}\n`);
|
|
112
|
+
process.exit(1);
|
|
113
|
+
}
|
|
114
|
+
const output = (args.out !== null) ? args.out : deriveOutput(input, FORMAT);
|
|
115
|
+
if (!args.quiet) {
|
|
116
|
+
process.stdout.write(`[${FORMAT.toUpperCase()}] ${input} → ${output}\n`);
|
|
117
|
+
}
|
|
118
|
+
const r = spawnSync(process.execPath, [LIB, input, output], { stdio: 'inherit' });
|
|
119
|
+
if (r.status !== 0) {
|
|
120
|
+
process.stderr.write(`error: render failed for ${input} (exit ${r.status})\n`);
|
|
121
|
+
process.exit(r.status || 1);
|
|
122
|
+
}
|
|
123
|
+
outputs.push(output);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (args.open) {
|
|
127
|
+
for (const o of outputs) {
|
|
128
|
+
openViewer(o);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
main();
|