@markmap/cli 0.0.0-stage → 1.0.0-next.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 +4 -2
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +279 -0
- package/dist/inline-images.d.ts +8 -0
- package/dist/main.d.ts +11 -0
- package/package.json +41 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Gerald
|
|
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
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @markmap/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Turn Markdown into a map from the command line.
|
|
4
|
+
|
|
5
|
+
See the [Markmap README](https://github.com/markmap/markmap#readme) for how the packages fit together.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { mkdir, readFile, realpath, writeFile } from "node:fs/promises";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { basename, dirname, extname, isAbsolute, join, parse, relative, resolve, sep } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { parseArgs } from "node:util";
|
|
8
|
+
import { toPlainText } from "@markmap/core";
|
|
9
|
+
import { createPage, remarkReplaceImages, walk } from "@markmap/page";
|
|
10
|
+
import { parse as parse$1 } from "@markmap/parse";
|
|
11
|
+
import { mathContent, remarkMath } from "@markmap/plugin-math";
|
|
12
|
+
import { approximateMeasurer, createImageContent, defaultContentPlugins, renderTreeAsync, resolveRenderOptions, toSVGString } from "@markmap/render";
|
|
13
|
+
// src/inline-images.ts
|
|
14
|
+
/** The types `sanitizeImageUrl` lets through as data URLs. */
|
|
15
|
+
var IMAGE_TYPES = {
|
|
16
|
+
".png": "image/png",
|
|
17
|
+
".jpg": "image/jpeg",
|
|
18
|
+
".jpeg": "image/jpeg",
|
|
19
|
+
".gif": "image/gif",
|
|
20
|
+
".webp": "image/webp",
|
|
21
|
+
".avif": "image/avif"
|
|
22
|
+
};
|
|
23
|
+
/** Has a scheme, or is `//host/…`: not a file next to the Markdown. */
|
|
24
|
+
var NOT_A_PATH = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
25
|
+
/** The file a URL in the Markdown points at, if it can point at one. */
|
|
26
|
+
function localPath(url, baseDir) {
|
|
27
|
+
if (!url || NOT_A_PATH.test(url)) return void 0;
|
|
28
|
+
try {
|
|
29
|
+
return resolve(baseDir, decodeURI(url));
|
|
30
|
+
} catch {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function toDataUrl(url, baseDir) {
|
|
35
|
+
const type = IMAGE_TYPES[extname(url).toLowerCase()];
|
|
36
|
+
if (!type) throw new Error("not a supported image type");
|
|
37
|
+
const base = await realpath(baseDir);
|
|
38
|
+
const file = await realpath(resolve(base, decodeURI(url)));
|
|
39
|
+
const path = relative(base, file);
|
|
40
|
+
if (path === ".." || path.startsWith(`..${sep}`) || isAbsolute(path)) throw new Error("outside the Markdown file's folder");
|
|
41
|
+
return `data:${type};base64,${(await readFile(file)).toString("base64")}`;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Data URLs for local images, by the URL in the Markdown. Only files in `baseDir` are
|
|
45
|
+
* read: Markdown from someone else must not be able to pull other files into a page
|
|
46
|
+
* that gets shared. Anything skipped is reported and keeps its URL.
|
|
47
|
+
*/
|
|
48
|
+
async function inlineImages(urls, baseDir, warn) {
|
|
49
|
+
const images = /* @__PURE__ */ new Map();
|
|
50
|
+
const local = [...new Set(urls)].filter((url) => localPath(url, baseDir));
|
|
51
|
+
await Promise.all(local.map(async (url) => {
|
|
52
|
+
try {
|
|
53
|
+
images.set(url, await toDataUrl(url, baseDir));
|
|
54
|
+
} catch (error) {
|
|
55
|
+
warn(`Image ${url} not included: ${error.code ?? error.message}`);
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
return images;
|
|
59
|
+
}
|
|
60
|
+
// src/main.ts
|
|
61
|
+
var USAGE = `Usage: markmap [options] <file.md>
|
|
62
|
+
|
|
63
|
+
Writes a map of a Markdown file as one HTML file that works offline. Use - to read
|
|
64
|
+
from stdin.
|
|
65
|
+
|
|
66
|
+
Options:
|
|
67
|
+
-o, --output <file> Where to write; - for stdout. Defaults to the input's name.
|
|
68
|
+
--svg Write an SVG image instead
|
|
69
|
+
--png Write a PNG image instead
|
|
70
|
+
--no-open Do not open the page in a browser
|
|
71
|
+
--no-toolbar Leave the toolbar off the page
|
|
72
|
+
-h, --help Show this help
|
|
73
|
+
-v, --version Show the version
|
|
74
|
+
`;
|
|
75
|
+
var require = createRequire(import.meta.url);
|
|
76
|
+
/** Open a file with whatever the system uses for it. Failing is not worth stopping for. */
|
|
77
|
+
function openFile(file) {
|
|
78
|
+
const [command, ...args] = process.platform === "darwin" ? ["open"] : process.platform === "win32" ? ["rundll32", "url.dll,FileProtocolHandler"] : ["xdg-open"];
|
|
79
|
+
const child = spawn(command, [...args, file], {
|
|
80
|
+
detached: true,
|
|
81
|
+
stdio: "ignore"
|
|
82
|
+
});
|
|
83
|
+
child.on("error", () => {});
|
|
84
|
+
child.unref();
|
|
85
|
+
}
|
|
86
|
+
var defaultIo = {
|
|
87
|
+
async stdin() {
|
|
88
|
+
let text = "";
|
|
89
|
+
for await (const chunk of process.stdin) text += String(chunk);
|
|
90
|
+
return text;
|
|
91
|
+
},
|
|
92
|
+
stdout: (data) => void process.stdout.write(data),
|
|
93
|
+
stderr: (text) => void process.stderr.write(text),
|
|
94
|
+
client: (math) => readFile(require.resolve(`@markmap/page/${math ? "client-math" : "client"}.js`), "utf8"),
|
|
95
|
+
open: openFile
|
|
96
|
+
};
|
|
97
|
+
/** Image URLs in a document, and whether it has math. */
|
|
98
|
+
function inspect(markdown) {
|
|
99
|
+
const urls = [];
|
|
100
|
+
let math = false;
|
|
101
|
+
const visit = (node) => {
|
|
102
|
+
if (node.type === "image") urls.push(node.url);
|
|
103
|
+
const { type } = node;
|
|
104
|
+
if (type === "math" || type === "inlineMath") math = true;
|
|
105
|
+
};
|
|
106
|
+
parse$1(markdown, { plugins: [remarkMath, () => (tree) => walk(tree, visit)] });
|
|
107
|
+
return {
|
|
108
|
+
urls,
|
|
109
|
+
math
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** Folds as a view would start with them; exports have no reader to unfold anything. */
|
|
113
|
+
function startFolded(root, foldLevel) {
|
|
114
|
+
const folded = /* @__PURE__ */ new Set();
|
|
115
|
+
const visit = (node, depth, foldAll) => {
|
|
116
|
+
const all = foldAll || !!node.data.foldAll;
|
|
117
|
+
const byLevel = typeof foldLevel === "number" && depth >= foldLevel;
|
|
118
|
+
if (node.children.length && (all || node.data.fold || byLevel)) folded.add(node.id);
|
|
119
|
+
for (const child of node.children) visit(child, depth + 1, all);
|
|
120
|
+
};
|
|
121
|
+
visit(root, 0, false);
|
|
122
|
+
return folded;
|
|
123
|
+
}
|
|
124
|
+
async function writeHtml(source, toolbar, io) {
|
|
125
|
+
const { urls, math } = inspect(source.markdown);
|
|
126
|
+
const { file, markdown } = source;
|
|
127
|
+
const images = file ? await inlineImages(urls, dirname(file), warnWith(io)) : /* @__PURE__ */ new Map();
|
|
128
|
+
const { root, frontmatter } = parse$1(markdown);
|
|
129
|
+
const title = typeof frontmatter.title === "string" ? frontmatter.title : void 0;
|
|
130
|
+
return createPage({
|
|
131
|
+
title: title || toPlainText(root.content) || (file ? basename(file) : "Markmap"),
|
|
132
|
+
data: {
|
|
133
|
+
markdown,
|
|
134
|
+
images: Object.fromEntries(images),
|
|
135
|
+
toolbar
|
|
136
|
+
},
|
|
137
|
+
script: await io.client(math)
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
var warnWith = (io) => (message) => io.stderr(`${message}\n`);
|
|
141
|
+
/** Frontmatter options, with what was ignored reported. */
|
|
142
|
+
function documentOptions({ frontmatter }, io) {
|
|
143
|
+
const { options, json, warnings } = resolveRenderOptions({ plugins: [...defaultContentPlugins, mathContent] }, frontmatter.markmap);
|
|
144
|
+
warnings.forEach(warnWith(io));
|
|
145
|
+
return {
|
|
146
|
+
options,
|
|
147
|
+
colorScheme: json.colorScheme === "dark" ? "dark" : "light",
|
|
148
|
+
foldLevel: json.foldLevel
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
async function loadNodeCanvas() {
|
|
152
|
+
try {
|
|
153
|
+
return await import("@markmap/canvas/node");
|
|
154
|
+
} catch {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async function writeSvg({ markdown, file }, io) {
|
|
159
|
+
const images = file ? await inlineImages(inspect(markdown).urls, dirname(file), warnWith(io)) : /* @__PURE__ */ new Map();
|
|
160
|
+
const parsed = parse$1(markdown, { plugins: [remarkMath, remarkReplaceImages(images)] });
|
|
161
|
+
const { options, colorScheme, foldLevel } = documentOptions(parsed, io);
|
|
162
|
+
const canvas = await loadNodeCanvas();
|
|
163
|
+
if (!canvas) io.stderr("Without @napi-rs/canvas, text and image sizes are approximate\n");
|
|
164
|
+
const nodeImages = createImageContent({ loadSize: canvas && ((src) => canvas.loadNodeImage(src)) });
|
|
165
|
+
const result = await renderTreeAsync(parsed.root, {
|
|
166
|
+
...options,
|
|
167
|
+
plugins: options.plugins?.map((p) => p.name === nodeImages.name ? nodeImages : p),
|
|
168
|
+
measurer: canvas?.createNodeMeasurer() ?? approximateMeasurer,
|
|
169
|
+
folded: startFolded(parsed.root, foldLevel)
|
|
170
|
+
});
|
|
171
|
+
return toSVGString(result, { colorScheme });
|
|
172
|
+
}
|
|
173
|
+
async function writePng({ markdown, file }, io) {
|
|
174
|
+
const canvas = await loadNodeCanvas();
|
|
175
|
+
if (!canvas) throw new Error("--png needs @napi-rs/canvas, which is not installed");
|
|
176
|
+
const parsed = parse$1(markdown, { plugins: [remarkMath] });
|
|
177
|
+
const { options, colorScheme, foldLevel } = documentOptions(parsed, io);
|
|
178
|
+
return canvas.renderPNG(parsed.root, {
|
|
179
|
+
...options,
|
|
180
|
+
colorScheme,
|
|
181
|
+
baseDir: file && dirname(file),
|
|
182
|
+
folded: startFolded(parsed.root, foldLevel)
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/** A file's real path, so links and other spellings of a name count as the same file. */
|
|
186
|
+
var real = (path) => realpath(path).catch(() => void 0);
|
|
187
|
+
/** What `output` would destroy, if it is the input or an image the input uses. */
|
|
188
|
+
async function conflict({ markdown, file }, output) {
|
|
189
|
+
if (!file) return void 0;
|
|
190
|
+
const target = await real(output);
|
|
191
|
+
if (!target) return void 0;
|
|
192
|
+
if (target === await real(file)) return "the input";
|
|
193
|
+
const paths = inspect(markdown).urls.map((url) => localPath(url, dirname(file)));
|
|
194
|
+
for (const path of paths) if (path && target === await real(path)) return "an image the input uses";
|
|
195
|
+
}
|
|
196
|
+
async function version() {
|
|
197
|
+
const file = join(dirname(fileURLToPath(import.meta.url)), "../package.json");
|
|
198
|
+
return JSON.parse(await readFile(file, "utf8")).version;
|
|
199
|
+
}
|
|
200
|
+
/** Run the command. Returns the exit code. */
|
|
201
|
+
async function main(argv, overrides = {}) {
|
|
202
|
+
const io = {
|
|
203
|
+
...defaultIo,
|
|
204
|
+
...overrides
|
|
205
|
+
};
|
|
206
|
+
let args;
|
|
207
|
+
try {
|
|
208
|
+
args = parseArgs({
|
|
209
|
+
args: [...argv],
|
|
210
|
+
allowPositionals: true,
|
|
211
|
+
options: {
|
|
212
|
+
output: {
|
|
213
|
+
type: "string",
|
|
214
|
+
short: "o"
|
|
215
|
+
},
|
|
216
|
+
svg: { type: "boolean" },
|
|
217
|
+
png: { type: "boolean" },
|
|
218
|
+
"no-open": { type: "boolean" },
|
|
219
|
+
"no-toolbar": { type: "boolean" },
|
|
220
|
+
help: {
|
|
221
|
+
type: "boolean",
|
|
222
|
+
short: "h"
|
|
223
|
+
},
|
|
224
|
+
version: {
|
|
225
|
+
type: "boolean",
|
|
226
|
+
short: "v"
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
} catch (error) {
|
|
231
|
+
io.stderr(`${error.message}\n\n${USAGE}`);
|
|
232
|
+
return 2;
|
|
233
|
+
}
|
|
234
|
+
const { values, positionals } = args;
|
|
235
|
+
if (values.help) {
|
|
236
|
+
io.stdout(USAGE);
|
|
237
|
+
return 0;
|
|
238
|
+
}
|
|
239
|
+
if (values.version) {
|
|
240
|
+
io.stdout(`${await version()}\n`);
|
|
241
|
+
return 0;
|
|
242
|
+
}
|
|
243
|
+
if (positionals.length !== 1 || values.svg && values.png) {
|
|
244
|
+
io.stderr(USAGE);
|
|
245
|
+
return 2;
|
|
246
|
+
}
|
|
247
|
+
const [input] = positionals;
|
|
248
|
+
const stdin = input === "-";
|
|
249
|
+
const extension = values.svg ? ".svg" : values.png ? ".png" : ".html";
|
|
250
|
+
const name = stdin ? "markmap" : parse(input).name;
|
|
251
|
+
const output = values.output ?? (stdin ? `${name}${extension}` : join(dirname(input), `${name}${extension}`));
|
|
252
|
+
try {
|
|
253
|
+
const source = stdin ? { markdown: await io.stdin() } : {
|
|
254
|
+
markdown: await readFile(input, "utf8"),
|
|
255
|
+
file: resolve(input)
|
|
256
|
+
};
|
|
257
|
+
const used = output === "-" ? void 0 : await conflict(source, output);
|
|
258
|
+
if (used) {
|
|
259
|
+
io.stderr(`Not overwriting ${output}: it is ${used}. Choose another name with -o.\n`);
|
|
260
|
+
return 1;
|
|
261
|
+
}
|
|
262
|
+
const data = values.svg ? await writeSvg(source, io) : values.png ? await writePng(source, io) : await writeHtml(source, !values["no-toolbar"], io);
|
|
263
|
+
if (output === "-") {
|
|
264
|
+
io.stdout(data);
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
await mkdir(dirname(resolve(output)), { recursive: true });
|
|
268
|
+
await writeFile(output, data);
|
|
269
|
+
io.stderr(`Wrote ${output}\n`);
|
|
270
|
+
if (extname(output) === ".html" && !values.svg && !values.png && !values["no-open"]) io.open(resolve(output));
|
|
271
|
+
return 0;
|
|
272
|
+
} catch (error) {
|
|
273
|
+
const { code, path, message } = error;
|
|
274
|
+
io.stderr(`${code === "ENOENT" ? `No such file: ${path}` : message}\n`);
|
|
275
|
+
return 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// src/cli.ts
|
|
279
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The file a URL in the Markdown points at, if it can point at one. */
|
|
2
|
+
export declare function localPath(url: string, baseDir: string): string | undefined;
|
|
3
|
+
/**
|
|
4
|
+
* Data URLs for local images, by the URL in the Markdown. Only files in `baseDir` are
|
|
5
|
+
* read: Markdown from someone else must not be able to pull other files into a page
|
|
6
|
+
* that gets shared. Anything skipped is reported and keeps its URL.
|
|
7
|
+
*/
|
|
8
|
+
export declare function inlineImages(urls: Iterable<string>, baseDir: string, warn: (message: string) => void): Promise<Map<string, string>>;
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The outside world, replaceable in tests. */
|
|
2
|
+
export interface Io {
|
|
3
|
+
stdin(): Promise<string>;
|
|
4
|
+
stdout(data: string | Uint8Array): void;
|
|
5
|
+
stderr(text: string): void;
|
|
6
|
+
/** The bundled client script for a page. */
|
|
7
|
+
client(math: boolean): Promise<string>;
|
|
8
|
+
open(file: string): void;
|
|
9
|
+
}
|
|
10
|
+
/** Run the command. Returns the exit code. */
|
|
11
|
+
export declare function main(argv: readonly string[], overrides?: Partial<Io>): Promise<number>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markmap/cli",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-next.0",
|
|
4
|
+
"description": "Turn Markdown into a map from the command line",
|
|
5
|
+
"homepage": "https://github.com/markmap/markmap#readme",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/markmap/markmap.git",
|
|
10
|
+
"directory": "packages/cli"
|
|
11
|
+
},
|
|
12
|
+
"bin": {
|
|
13
|
+
"markmap": "./dist/cli.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@markmap/canvas": "1.0.0-next.0",
|
|
24
|
+
"@markmap/core": "1.0.0-next.0",
|
|
25
|
+
"@markmap/page": "1.0.0-next.0",
|
|
26
|
+
"@markmap/parse": "1.0.0-next.0",
|
|
27
|
+
"@markmap/plugin-math": "1.0.0-next.0",
|
|
28
|
+
"@markmap/render": "1.0.0-next.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/mdast": "^4.0.4"
|
|
32
|
+
},
|
|
33
|
+
"optionalDependencies": {
|
|
34
|
+
"@napi-rs/canvas": "^1.1.0"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
41
|
+
"build:types": "tsc -b",
|
|
42
|
+
"build:js": "vite build"
|
|
43
|
+
}
|
|
6
44
|
}
|