markmap-cli 0.14.5-alpha.21 → 0.14.5-alpha.22

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/bin/cli.js CHANGED
@@ -1,9 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const updateNotifier = require('update-notifier');
4
- const { main } = require('..');
5
- const pkg = require('../package.json');
3
+ import { main } from '../dist/index.js';
6
4
 
7
- const notifier = updateNotifier({ pkg });
8
- notifier.notify();
9
- main(pkg.version);
5
+ main().catch(err => {
6
+ console.error(err);
7
+ process.exitCode = 1;
8
+ });
package/dist/index.js CHANGED
@@ -1,16 +1,21 @@
1
- /*! markmap-cli v0.14.5-alpha.21+fb81f2c | MIT License */
2
- 'use strict';
3
-
4
- var fs = require('fs');
5
- var commander = require('commander');
6
- var open = require('open');
7
- var markmapCommon = require('markmap-common');
8
- var markmapLib = require('markmap-lib');
9
- var path = require('path');
10
- var events = require('events');
11
- var http = require('http');
12
- var Koa = require('koa');
13
- var chokidar = require('chokidar');
1
+ /*! markmap-cli v0.14.5-alpha.22+04afb38 | MIT License */
2
+ import { readFile, writeFile } from 'fs/promises';
3
+ import { fileURLToPath } from 'url';
4
+ import { Command } from 'commander';
5
+ import open from 'open';
6
+ import updateNotifier from 'update-notifier';
7
+ import { readPackageUp } from 'read-pkg-up';
8
+ import { buildCSSItem, buildJSItem, defer } from 'markmap-common';
9
+ import { Transformer, baseJsPaths } from 'markmap-lib';
10
+ export * from 'markmap-lib';
11
+ import { join, extname } from 'path';
12
+ import { createRequire } from 'module';
13
+ import { packageDirectory } from 'pkg-dir';
14
+ import { createReadStream } from 'fs';
15
+ import { EventEmitter } from 'events';
16
+ import http from 'http';
17
+ import Koa from 'koa';
18
+ import chokidar from 'chokidar';
14
19
 
15
20
  function _extends() {
16
21
  _extends = Object.assign ? Object.assign.bind() : function (target) {
@@ -27,7 +32,7 @@ function _extends() {
27
32
  return _extends.apply(this, arguments);
28
33
  }
29
34
 
30
- const TOOLBAR_VERSION = "0.14.5-alpha.21+fb81f2c";
35
+ const TOOLBAR_VERSION = "0.14.5-alpha.22";
31
36
  const TOOLBAR_CSS = `markmap-toolbar@${TOOLBAR_VERSION}/dist/style.css`;
32
37
  const TOOLBAR_JS = `markmap-toolbar@${TOOLBAR_VERSION}/dist/index.js`;
33
38
  const renderToolbar = () => {
@@ -41,10 +46,10 @@ const renderToolbar = () => {
41
46
  el.setAttribute('style', 'position:absolute;bottom:20px;right:20px');
42
47
  document.body.append(el);
43
48
  };
44
- function addToolbar(assets) {
49
+ function addToolbar(transformer, assets) {
45
50
  return {
46
- styles: [...(assets.styles || []), ...[TOOLBAR_CSS].map(path => markmapCommon.buildCSSItem(path))],
47
- scripts: [...(assets.scripts || []), ...[TOOLBAR_JS].map(path => markmapCommon.buildJSItem(path)), {
51
+ styles: [...(assets.styles || []), ...[TOOLBAR_CSS].map(path => transformer.urlBuilder.getFullUrl(path)).map(path => buildCSSItem(path))],
52
+ scripts: [...(assets.scripts || []), ...[TOOLBAR_JS].map(path => transformer.urlBuilder.getFullUrl(path)).map(path => buildJSItem(path)), {
48
53
  type: 'iife',
49
54
  data: {
50
55
  fn: r => {
@@ -70,6 +75,25 @@ function localProvider(path) {
70
75
  path = parts.join('/');
71
76
  return `/node_modules/${path}`;
72
77
  }
78
+ const require = createRequire(import.meta.url);
79
+ async function doResolveFile(relpath) {
80
+ const parts = relpath.split('/');
81
+ const nameOffset = parts[0].startsWith('@') ? 2 : 1;
82
+ const name = parts.slice(0, nameOffset).join('/');
83
+ const filepath = parts.slice(nameOffset).join('/');
84
+ const mainPath = require.resolve(name);
85
+ const pkgDir = await packageDirectory({
86
+ cwd: mainPath
87
+ });
88
+ if (!pkgDir) throw new Error(`File not found: ${relpath}`);
89
+ const realpath = join(pkgDir, filepath);
90
+ return realpath;
91
+ }
92
+ const cache = {};
93
+ function resolveFile(relpath) {
94
+ cache[relpath] || (cache[relpath] = doResolveFile(relpath));
95
+ return cache[relpath];
96
+ }
73
97
 
74
98
  function sequence(fn) {
75
99
  let promise;
@@ -83,7 +107,7 @@ function sequence(fn) {
83
107
  class BufferContentProvider {
84
108
  constructor() {
85
109
  this.deferredSet = new Set();
86
- this.events = new events.EventEmitter();
110
+ this.events = new EventEmitter();
87
111
  this.ts = 0;
88
112
  this.content = '';
89
113
  this.line = -1;
@@ -100,7 +124,7 @@ class BufferContentProvider {
100
124
  });
101
125
  }
102
126
  async getUpdate(ts, timeout = 10000) {
103
- const deferred = markmapCommon.defer();
127
+ const deferred = defer();
104
128
  this.deferredSet.add(deferred);
105
129
  setTimeout(() => {
106
130
  this.feed({}, deferred);
@@ -144,7 +168,7 @@ class FileSystemProvider extends BufferContentProvider {
144
168
  this.watcher = chokidar.watch(fileName).on('all', sequence(() => this.update()));
145
169
  }
146
170
  async update() {
147
- const content = await fs.promises.readFile(this.fileName, 'utf8');
171
+ const content = await readFile(this.fileName, 'utf8');
148
172
  this.setContent(content);
149
173
  }
150
174
  dispose() {
@@ -205,8 +229,8 @@ function startServer(paddingBottom) {
205
229
  }
206
230
  function setUpServer(transformer, provider, options) {
207
231
  let assets = transformer.getAssets();
208
- if (options.toolbar) assets = addToolbar(assets);
209
- const html = `${markmapLib.fillTemplate(null, assets)}<script>(${startServer.toString()})(${options.toolbar ? 60 : 0})</script>`;
232
+ if (options.toolbar) assets = addToolbar(transformer, assets);
233
+ const html = `${transformer.fillTemplate(null, assets)}<script>(${startServer.toString()})(${options.toolbar ? 60 : 0})</script>`;
210
234
  const app = new Koa();
211
235
  app.use(async (ctx, next) => {
212
236
  if (ctx.path === '/') {
@@ -223,12 +247,12 @@ function setUpServer(transformer, provider, options) {
223
247
  if (ctx.path.startsWith('/node_modules/')) {
224
248
  const relpath = ctx.path.slice(14);
225
249
  try {
226
- const realpath = require.resolve(relpath);
227
- ctx.type = path.extname(relpath);
228
- ctx.body = fs.createReadStream(realpath);
250
+ const realpath = await resolveFile(relpath);
251
+ ctx.type = extname(relpath);
252
+ ctx.body = createReadStream(realpath);
229
253
  return;
230
- } catch (_unused) {
231
- // ignore
254
+ } catch (err) {
255
+ console.error(err);
232
256
  }
233
257
  }
234
258
  await next();
@@ -257,8 +281,9 @@ function setUpServer(transformer, provider, options) {
257
281
  };
258
282
  }
259
283
  async function develop(fileName, options) {
260
- markmapCommon.setProvider('local', localProvider);
261
- const transformer = new markmapLib.Transformer();
284
+ const transformer = new Transformer();
285
+ transformer.urlBuilder.setProvider('local', localProvider);
286
+ transformer.urlBuilder.provider = 'local';
262
287
  const provider = fileName ? new FileSystemProvider(fileName) : new BufferContentProvider();
263
288
  return setUpServer(transformer, provider, options);
264
289
  }
@@ -266,7 +291,7 @@ async function develop(fileName, options) {
266
291
  async function loadFile(path) {
267
292
  if (path.startsWith('/node_modules/')) {
268
293
  const relpath = path.slice(14);
269
- return fs.promises.readFile(require.resolve(relpath), 'utf8');
294
+ return readFile(await resolveFile(relpath), 'utf8');
270
295
  }
271
296
  const res = await fetch(path);
272
297
  if (!res.ok) throw res;
@@ -288,12 +313,13 @@ async function inlineAssets(assets) {
288
313
  };
289
314
  }
290
315
  async function createMarkmap(options) {
316
+ const transformer = new Transformer();
291
317
  if (options.offline) {
292
- markmapCommon.setProvider('local', localProvider);
318
+ transformer.urlBuilder.setProvider('local', localProvider);
319
+ transformer.urlBuilder.provider = 'local';
293
320
  } else {
294
- await markmapCommon.findFastestProvider();
321
+ await transformer.urlBuilder.findFastestProvider();
295
322
  }
296
- const transformer = new markmapLib.Transformer();
297
323
  const {
298
324
  root,
299
325
  features,
@@ -301,26 +327,35 @@ async function createMarkmap(options) {
301
327
  } = transformer.transform(options.content || '');
302
328
  let assets = transformer.getUsedAssets(features);
303
329
  assets = _extends({}, assets, {
304
- scripts: [...markmapLib.baseJsPaths.map(path => markmapCommon.buildJSItem(path)), ...(assets.scripts || [])]
330
+ scripts: [...baseJsPaths.map(path => transformer.urlBuilder.getFullUrl(path)).map(path => buildJSItem(path)), ...(assets.scripts || [])]
305
331
  });
306
332
  if (options.toolbar) {
307
- assets = addToolbar(assets);
333
+ assets = addToolbar(transformer, assets);
308
334
  }
309
335
  if (options.offline) {
310
336
  assets = await inlineAssets(assets);
311
337
  }
312
- const html = markmapLib.fillTemplate(root, assets, {
338
+ const html = transformer.fillTemplate(root, assets, {
313
339
  baseJs: [],
314
340
  jsonOptions: frontmatter == null ? void 0 : frontmatter.markmap
315
341
  });
316
342
  const output = options.output || 'markmap.html';
317
- await fs.promises.writeFile(output, html, 'utf8');
343
+ await writeFile(output, html, 'utf8');
318
344
  if (options.open) open(output);
319
345
  }
320
- function main(version) {
321
- const program = new commander.Command();
322
- program.version(version).description('Create a markmap from a Markdown input file').arguments('<input>').option('--no-open', 'do not open the output file after generation').option('--no-toolbar', 'do not show toolbar').option('-o, --output <output>', 'specify filename of the output HTML').option('--offline', 'Inline all assets to allow the generated HTML to work offline').option('-w, --watch', 'watch the input file and update output on the fly, note that this feature is for development only').action(async (input, cmd) => {
323
- const content = await fs.promises.readFile(input, 'utf8');
346
+ async function main() {
347
+ var _await$readPackageUp;
348
+ const pkg = (_await$readPackageUp = await readPackageUp({
349
+ cwd: fileURLToPath(import.meta.url)
350
+ })) == null ? void 0 : _await$readPackageUp.packageJson;
351
+ if (!pkg) throw new Error('package.json not found');
352
+ const notifier = updateNotifier({
353
+ pkg
354
+ });
355
+ notifier.notify();
356
+ const program = new Command();
357
+ program.version(pkg.version).description('Create a markmap from a Markdown input file').arguments('<input>').option('--no-open', 'do not open the output file after generation').option('--no-toolbar', 'do not show toolbar').option('-o, --output <output>', 'specify filename of the output HTML').option('--offline', 'Inline all assets to allow the generated HTML to work offline').option('-w, --watch', 'watch the input file and update output on the fly, note that this feature is for development only').action(async (input, cmd) => {
358
+ const content = await readFile(input, 'utf8');
324
359
  const output = cmd.output || `${input.replace(/\.\w*$/, '')}.html`;
325
360
  if (cmd.watch) {
326
361
  await develop(input, {
@@ -341,9 +376,4 @@ function main(version) {
341
376
  program.parse(process.argv);
342
377
  }
343
378
 
344
- exports.createMarkmap = createMarkmap;
345
- exports.develop = develop;
346
- exports.main = main;
347
- Object.keys(markmapLib).forEach(function (k) {
348
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = markmapLib[k];
349
- });
379
+ export { createMarkmap, develop, main };
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "markmap-cli",
3
- "version": "0.14.5-alpha.21+fb81f2c",
3
+ "version": "0.14.5-alpha.22+04afb38",
4
4
  "description": "Create markmaps from CLI",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
7
7
  "bin": {
8
8
  "markmap": "bin/cli.js"
9
9
  },
10
+ "engines": {
11
+ "node": ">=16"
12
+ },
10
13
  "keywords": [
11
14
  "markmap",
12
15
  "markdown",
@@ -22,15 +25,17 @@
22
25
  "url": "git+https://github.com/markmap/markmap.git"
23
26
  },
24
27
  "scripts": {
25
- "dev": "rollup -wc rollup.conf.js",
28
+ "dev": "rollup -wc",
26
29
  "clean": "del-cli dist types",
27
30
  "build:types": "tsc",
28
- "build:js": "rollup -c rollup.conf.js"
31
+ "build:js": "rollup -c"
29
32
  },
30
33
  "bugs": {
31
34
  "url": "https://github.com/markmap/markmap/issues"
32
35
  },
36
+ "type": "module",
33
37
  "main": "dist/index.js",
38
+ "module": "dist/index.js",
34
39
  "files": [
35
40
  "bin",
36
41
  "dist",
@@ -40,19 +45,21 @@
40
45
  "dependencies": {
41
46
  "@babel/runtime": "^7.22.6",
42
47
  "chokidar": "^3.5.3",
43
- "commander": "^7.2.0",
48
+ "commander": "^11.0.0",
44
49
  "d3": "^7.8.5",
45
50
  "highlight.js": "^11.8.0",
46
51
  "katex": "^0.16.8",
47
52
  "koa": "^2.14.2",
48
- "markmap-common": "0.14.5-alpha.21+fb81f2c",
49
- "markmap-lib": "0.14.5-alpha.21+fb81f2c",
50
- "markmap-toolbar": "0.14.5-alpha.21+fb81f2c",
51
- "markmap-view": "0.14.5-alpha.21+fb81f2c",
52
- "open": "^8.4.2",
53
+ "markmap-common": "0.14.5-alpha.22+04afb38",
54
+ "markmap-lib": "0.14.5-alpha.22+04afb38",
55
+ "markmap-toolbar": "0.14.5-alpha.22+04afb38",
56
+ "markmap-view": "0.14.5-alpha.22+04afb38",
57
+ "open": "^9.1.0",
58
+ "pkg-dir": "^7.0.0",
53
59
  "prismjs": "^1.29.0",
54
- "update-notifier": "^5.1.0",
60
+ "read-pkg-up": "^10.0.0",
61
+ "update-notifier": "^6.0.2",
55
62
  "webfontloader": "^1.6.28"
56
63
  },
57
- "gitHead": "fb81f2cb688f400f17f911774539267219f639ce"
64
+ "gitHead": "04afb38dd0a6f3b5e8eaffde3c29e9e38b215c35"
58
65
  }
package/types/index.d.ts CHANGED
@@ -4,4 +4,4 @@ import { develop } from './dev-server';
4
4
  export * from 'markmap-lib';
5
5
  export { develop };
6
6
  export declare function createMarkmap(options: IMarkmapCreateOptions & IDevelopOptions): Promise<void>;
7
- export declare function main(version: string): void;
7
+ export declare function main(): Promise<void>;
package/types/util.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IAssets } from 'markmap-lib';
1
+ import { IAssets, ITransformer } from 'markmap-lib';
2
2
  export interface IDevelopOptions {
3
3
  /**
4
4
  * Whether to open the generated markmap in browser
@@ -14,5 +14,6 @@ export interface IDevelopOptions {
14
14
  */
15
15
  offline: boolean;
16
16
  }
17
- export declare function addToolbar(assets: IAssets): IAssets;
17
+ export declare function addToolbar(transformer: ITransformer, assets: IAssets): IAssets;
18
18
  export declare function localProvider(path: string): string;
19
+ export declare function resolveFile(relpath: string): Promise<string>;