markmap-cli 0.15.6 → 0.15.8-alpha.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/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
1
  import { readFile, stat, mkdir, writeFile } from "fs/promises";
2
- import { resolve, extname, dirname } from "path";
2
+ import { resolve, join, dirname } from "path";
3
3
  import { fileURLToPath } from "url";
4
4
  import { Command } from "commander";
5
5
  import open from "open";
6
6
  import updateNotifier from "update-notifier";
7
- import { readPackageUp } from "read-pkg-up";
7
+ import { readPackageUp } from "read-package-up";
8
8
  import { buildCSSItem, buildJSItem, defer } from "markmap-common";
9
9
  import { Transformer, baseJsPaths } from "markmap-lib";
10
10
  export * from "markmap-lib";
11
11
  import { packageDirectory } from "pkg-dir";
12
12
  import { createReadStream, createWriteStream } from "fs";
13
13
  import { EventEmitter } from "events";
14
- import http from "http";
15
- import Koa from "koa";
14
+ import { serve } from "@hono/node-server";
15
+ import { Hono } from "hono";
16
16
  import chokidar from "chokidar";
17
17
  import { Readable } from "stream";
18
18
  import { finished } from "stream/promises";
@@ -60,6 +60,114 @@ const assetsDirPromise = rootDirPromise.then((rootDir) => {
60
60
  throw new Error("Could not find root dir");
61
61
  return resolve(rootDir, `.${ASSETS_PREFIX}`);
62
62
  });
63
+ function createStreamBody(stream) {
64
+ const body = new ReadableStream({
65
+ start(controller) {
66
+ stream.on("data", (chunk) => {
67
+ controller.enqueue(chunk);
68
+ });
69
+ stream.on("end", () => {
70
+ controller.close();
71
+ });
72
+ },
73
+ cancel() {
74
+ stream.destroy();
75
+ }
76
+ });
77
+ return body;
78
+ }
79
+ var getMimeType = (filename) => {
80
+ const regexp = /\.([a-zA-Z0-9]+?)$/;
81
+ const match = filename.match(regexp);
82
+ if (!match)
83
+ return;
84
+ let mimeType = mimes[match[1]];
85
+ if (mimeType && mimeType.startsWith("text") || mimeType === "application/json") {
86
+ mimeType += "; charset=utf-8";
87
+ }
88
+ return mimeType;
89
+ };
90
+ var mimes = {
91
+ aac: "audio/aac",
92
+ abw: "application/x-abiword",
93
+ arc: "application/x-freearc",
94
+ avi: "video/x-msvideo",
95
+ avif: "image/avif",
96
+ av1: "video/av1",
97
+ azw: "application/vnd.amazon.ebook",
98
+ bin: "application/octet-stream",
99
+ bmp: "image/bmp",
100
+ bz: "application/x-bzip",
101
+ bz2: "application/x-bzip2",
102
+ csh: "application/x-csh",
103
+ css: "text/css",
104
+ csv: "text/csv",
105
+ doc: "application/msword",
106
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
107
+ eot: "application/vnd.ms-fontobject",
108
+ epub: "application/epub+zip",
109
+ gif: "image/gif",
110
+ gz: "application/gzip",
111
+ htm: "text/html",
112
+ html: "text/html",
113
+ ico: "image/x-icon",
114
+ ics: "text/calendar",
115
+ jar: "application/java-archive",
116
+ jpeg: "image/jpeg",
117
+ jpg: "image/jpeg",
118
+ js: "text/javascript",
119
+ json: "application/json",
120
+ jsonld: "application/ld+json",
121
+ map: "application/json",
122
+ mid: "audio/x-midi",
123
+ midi: "audio/x-midi",
124
+ mjs: "text/javascript",
125
+ mp3: "audio/mpeg",
126
+ mp4: "video/mp4",
127
+ mpeg: "video/mpeg",
128
+ mpkg: "application/vnd.apple.installer+xml",
129
+ odp: "application/vnd.oasis.opendocument.presentation",
130
+ ods: "application/vnd.oasis.opendocument.spreadsheet",
131
+ odt: "application/vnd.oasis.opendocument.text",
132
+ oga: "audio/ogg",
133
+ ogv: "video/ogg",
134
+ ogx: "application/ogg",
135
+ opus: "audio/opus",
136
+ otf: "font/otf",
137
+ pdf: "application/pdf",
138
+ php: "application/php",
139
+ png: "image/png",
140
+ ppt: "application/vnd.ms-powerpoint",
141
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
142
+ rtf: "application/rtf",
143
+ sh: "application/x-sh",
144
+ svg: "image/svg+xml",
145
+ swf: "application/x-shockwave-flash",
146
+ tar: "application/x-tar",
147
+ tif: "image/tiff",
148
+ tiff: "image/tiff",
149
+ ts: "video/mp2t",
150
+ ttf: "font/ttf",
151
+ txt: "text/plain",
152
+ vsd: "application/vnd.visio",
153
+ wasm: "application/wasm",
154
+ webm: "video/webm",
155
+ weba: "audio/webm",
156
+ webp: "image/webp",
157
+ woff: "font/woff",
158
+ woff2: "font/woff2",
159
+ xhtml: "application/xhtml+xml",
160
+ xls: "application/vnd.ms-excel",
161
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
162
+ xml: "application/xml",
163
+ xul: "application/vnd.mozilla.xul+xml",
164
+ zip: "application/zip",
165
+ "3gp": "video/3gpp",
166
+ "3g2": "video/3gpp2",
167
+ "7z": "application/x-7z-compressed",
168
+ gltf: "model/gltf+json",
169
+ glb: "model/gltf-binary"
170
+ };
63
171
  function sequence(fn) {
64
172
  let promise;
65
173
  return () => {
@@ -148,7 +256,7 @@ function startServer(paddingBottom) {
148
256
  const { mm, markmap } = window;
149
257
  refresh();
150
258
  function refresh() {
151
- fetch(`/data?ts=${ts}`).then((res) => res.json()).then((res) => {
259
+ fetch(`/~data?ts=${ts}`).then((res) => res.json()).then((res) => {
152
260
  if (res.ts && res.ts > ts && res.result) {
153
261
  let frontmatter;
154
262
  ({ root, frontmatter, contentLineOffset: offset } = res.result);
@@ -183,7 +291,7 @@ function startServer(paddingBottom) {
183
291
  }
184
292
  }
185
293
  }
186
- function setUpServer(transformer, provider, options) {
294
+ async function setUpServer(transformer, provider, options) {
187
295
  let assets = transformer.getAssets();
188
296
  if (options.toolbar)
189
297
  assets = addToolbar(transformer.urlBuilder, assets);
@@ -191,38 +299,48 @@ function setUpServer(transformer, provider, options) {
191
299
  null,
192
300
  assets
193
301
  )}<script>(${startServer.toString()})(${options.toolbar ? 60 : 0})<\/script>`;
194
- const app = new Koa();
195
- app.use(async (ctx, next) => {
196
- if (ctx.path === "/") {
197
- ctx.body = html;
198
- } else if (ctx.path === "/data") {
199
- const update = await provider.getUpdate(ctx.query.ts);
200
- const result = update.content == null ? null : transformer.transform(update.content || "");
201
- ctx.body = { ts: update.ts, result, line: update.line };
202
- } else {
203
- if (ctx.path.startsWith(ASSETS_PREFIX)) {
204
- const relpath = ctx.path.slice(ASSETS_PREFIX.length);
205
- const assetsDir = await assetsDirPromise;
206
- try {
207
- const realpath = resolve(assetsDir, relpath);
208
- ctx.type = extname(relpath);
209
- ctx.body = createReadStream(realpath);
210
- return;
211
- } catch (err) {
212
- console.error(err);
213
- }
214
- }
215
- await next();
216
- }
302
+ const app = new Hono();
303
+ app.get("/", (c) => c.html(html));
304
+ app.get("/~data", async (c) => {
305
+ const update = await provider.getUpdate(+(c.req.query("ts") || 0));
306
+ const result = update.content == null ? null : transformer.transform(update.content || "");
307
+ return c.json({ ts: update.ts, result, line: update.line });
308
+ });
309
+ app.post("/~api", async (c) => {
310
+ var _a;
311
+ const { cmd, args } = await c.req.json();
312
+ await ((_a = provider[cmd]) == null ? void 0 : _a.call(provider, ...args));
313
+ return c.status(204);
217
314
  });
218
- const handle = app.callback();
219
- const server = http.createServer(handle);
220
- server.listen(() => {
221
- const { port } = server.address();
222
- console.info(`Listening at http://localhost:${port}`);
223
- if (options.open)
224
- open(`http://localhost:${port}`);
315
+ const assetsDir = await assetsDirPromise;
316
+ app.get(`${ASSETS_PREFIX}*`, async (c) => {
317
+ const relpath = c.req.path.slice(ASSETS_PREFIX.length);
318
+ const realpath = join(assetsDir, relpath);
319
+ try {
320
+ const result = await stat(realpath);
321
+ if (!result.isFile())
322
+ throw new Error("File not found");
323
+ } catch {
324
+ return c.status(404);
325
+ }
326
+ const stream = createReadStream(realpath);
327
+ const type = getMimeType(relpath);
328
+ if (type)
329
+ c.header("content-type", type);
330
+ return c.body(createStreamBody(stream));
225
331
  });
332
+ const server = serve(
333
+ {
334
+ fetch: app.fetch,
335
+ port: options.port
336
+ },
337
+ (info) => {
338
+ const { port } = info;
339
+ console.info(`Listening at http://localhost:${port}`);
340
+ if (options.open)
341
+ open(`http://localhost:${port}`);
342
+ }
343
+ );
226
344
  let closing;
227
345
  return {
228
346
  provider,
@@ -367,8 +485,11 @@ async function createMarkmap(options) {
367
485
  });
368
486
  const output = options.output || "markmap.html";
369
487
  await writeFile(output, html, "utf8");
370
- if (options.open)
371
- open(output);
488
+ if (options.open) {
489
+ console.log("open", output);
490
+ const p = await open(output);
491
+ console.log(p.exitCode);
492
+ }
372
493
  }
373
494
  async function main() {
374
495
  var _a;
package/dist/util.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /// <reference types="node" />
2
+ import type { ReadStream } from 'fs';
1
3
  import { UrlBuilder } from 'markmap-common';
2
4
  import { IAssets } from 'markmap-lib';
3
5
  export declare const ASSETS_PREFIX = "/assets/";
@@ -15,8 +17,11 @@ export interface IDevelopOptions {
15
17
  * Ignored in watching mode.
16
18
  */
17
19
  offline: boolean;
20
+ /** A port for the devServer to listen. */
21
+ port?: number;
18
22
  }
19
23
  export declare function addToolbar(urlBuilder: UrlBuilder, assets: IAssets): IAssets;
20
24
  export declare function localProvider(path: string): string;
21
25
  export declare const rootDirPromise: Promise<string | undefined>;
22
26
  export declare const assetsDirPromise: Promise<string>;
27
+ export declare function createStreamBody(stream: ReadStream): ReadableStream<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markmap-cli",
3
- "version": "0.15.6",
3
+ "version": "0.15.8-alpha.0+a192279",
4
4
  "description": "Create markmaps from CLI",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
@@ -42,17 +42,18 @@
42
42
  ],
43
43
  "typings": "dist/index.d.ts",
44
44
  "dependencies": {
45
- "@babel/runtime": "^7.22.6",
45
+ "@babel/runtime": "^7.23.6",
46
+ "@hono/node-server": "^1.3.3",
46
47
  "chokidar": "^3.5.3",
47
- "commander": "^11.0.0",
48
- "koa": "^2.14.2",
48
+ "commander": "^11.1.0",
49
+ "hono": "^3.11.8",
49
50
  "markmap-common": "0.15.6",
50
- "markmap-lib": "0.15.6",
51
+ "markmap-lib": "0.15.7",
51
52
  "markmap-toolbar": "0.15.6",
52
- "open": "^9.1.0",
53
- "pkg-dir": "^7.0.0",
54
- "read-pkg-up": "^10.0.0",
55
- "update-notifier": "^6.0.2"
53
+ "open": "^10.0.0",
54
+ "pkg-dir": "^8.0.0",
55
+ "read-package-up": "^11.0.0",
56
+ "update-notifier": "^7.0.0"
56
57
  },
57
- "gitHead": "7309ee74a3fed2c40a2fc37f1b5df208f25f3c3a"
58
+ "gitHead": "a1922793282449a9ebe2991a6e376e83de778a55"
58
59
  }