markmap-autoloader 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.
Files changed (2) hide show
  1. package/dist/index.js +56 -26
  2. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! markmap-autoloader v0.14.5-alpha.21+fb81f2c | MIT License */
1
+ /*! markmap-autoloader v0.14.5-alpha.22+04afb38 | MIT License */
2
2
  this.markmap = this.markmap || {};
3
3
  (function (exports) {
4
4
  'use strict';
@@ -18,7 +18,7 @@ function _extends$1() {
18
18
  return _extends$1.apply(this, arguments);
19
19
  }
20
20
 
21
- /*! markmap-common v0.14.5-alpha.21+fb81f2c | MIT License */
21
+ /*! markmap-common v0.14.5-alpha.22+04afb38 | MIT License */
22
22
  function _extends() {
23
23
  _extends = Object.assign ? Object.assign.bind() : function (target) {
24
24
  for (var i = 1; i < arguments.length; i++) {
@@ -33,6 +33,49 @@ function _extends() {
33
33
  };
34
34
  return _extends.apply(this, arguments);
35
35
  }
36
+ const testPath = "npm2url/dist/index.cjs";
37
+ const defaultProviders = {
38
+ jsdelivr: path => `https://cdn.jsdelivr.net/npm/${path}`,
39
+ unpkg: path => `https://unpkg.com/${path}`
40
+ };
41
+ class UrlBuilder {
42
+ constructor() {
43
+ this.providers = _extends({}, defaultProviders);
44
+ this.provider = "jsdelivr";
45
+ }
46
+ getFastestProvider(path = testPath) {
47
+ return Promise.any(Object.entries(this.providers).map(async ([name, factory]) => {
48
+ const res = await fetch(factory(path));
49
+ if (!res.ok) {
50
+ throw res;
51
+ }
52
+ await res.text();
53
+ return name;
54
+ }));
55
+ }
56
+ async findFastestProvider() {
57
+ this.provider = await this.getFastestProvider();
58
+ return this.provider;
59
+ }
60
+ setProvider(name, factory) {
61
+ if (factory) {
62
+ this.providers[name] = factory;
63
+ } else {
64
+ delete this.providers[name];
65
+ }
66
+ }
67
+ getFullUrl(path, provider = this.provider) {
68
+ if (path.includes("://")) {
69
+ return path;
70
+ }
71
+ const factory = this.providers[provider];
72
+ if (!factory) {
73
+ throw new Error(`Provider ${provider} not found`);
74
+ }
75
+ return factory(path);
76
+ }
77
+ }
78
+ const urlBuilder = new UrlBuilder();
36
79
  Math.random().toString(36).slice(2, 8);
37
80
  function memoize(fn) {
38
81
  const cache = {};
@@ -203,17 +246,6 @@ function mountDom(vnode) {
203
246
  function hm(...args) {
204
247
  return mountDom(h(...args));
205
248
  }
206
- const providers = {
207
- jsdelivr: path => `https://cdn.jsdelivr.net/npm/${path}`,
208
- unpkg: path => `https://unpkg.com/${path}`
209
- };
210
- function cdnUrl(provider, path) {
211
- const factory = providers[provider];
212
- if (!factory) {
213
- throw new Error(`Provider ${provider} not found`);
214
- }
215
- return factory(path);
216
- }
217
249
  const memoizedPreloadJS = memoize(url => {
218
250
  document.head.append(hm('link', {
219
251
  rel: 'preload',
@@ -279,24 +311,19 @@ function loadCSS(items) {
279
311
  loadCSSItem(item);
280
312
  }
281
313
  }
282
- let provider = 'jsdelivr';
283
- function getFullUrl(path, overrideProvider = provider) {
284
- if (path.includes('://')) return path;
285
- return cdnUrl(overrideProvider, path);
286
- }
287
- function buildJSItem(path, overrideProvider) {
314
+ function buildJSItem(path) {
288
315
  return {
289
316
  type: 'script',
290
317
  data: {
291
- src: getFullUrl(path, overrideProvider)
318
+ src: path
292
319
  }
293
320
  };
294
321
  }
295
- function buildCSSItem(path, overrideProvider) {
322
+ function buildCSSItem(path) {
296
323
  return {
297
324
  type: 'stylesheet',
298
325
  data: {
299
- href: getFullUrl(path, overrideProvider)
326
+ href: path
300
327
  }
301
328
  };
302
329
  }
@@ -304,12 +331,14 @@ function buildCSSItem(path, overrideProvider) {
304
331
  var _window$markmap;
305
332
  const enabled = {};
306
333
  const autoLoaderOptions = _extends$1({
307
- baseJs: [`d3@${"7.8.5"}`, `markmap-lib@${"0.14.5-alpha.21"}`, `markmap-view@${"0.14.5-alpha.21"}`, `markmap-toolbar@${"0.14.5-alpha.21"}`],
308
- baseCss: [`markmap-toolbar@${"0.14.5-alpha.21"}/dist/style.css`],
334
+ baseJs: [`d3@${"7.8.5"}`, `markmap-lib@${"0.14.5-alpha.22"}`, `markmap-view@${"0.14.5-alpha.22"}`, `markmap-toolbar@${"0.14.5-alpha.22"}`],
335
+ baseCss: [`markmap-toolbar@${"0.14.5-alpha.22"}/dist/style.css`],
309
336
  manual: false,
310
337
  toolbar: false
311
338
  }, (_window$markmap = window.markmap) == null ? void 0 : _window$markmap.autoLoader);
312
- const ready = Promise.all([loadJS(autoLoaderOptions.baseJs.map(item => typeof item === 'string' ? buildJSItem(item) : item)), loadCSS(autoLoaderOptions.baseCss.map(item => typeof item === 'string' ? buildCSSItem(item) : item))]).then(() => {
339
+ async function initialize() {
340
+ await urlBuilder.findFastestProvider();
341
+ await Promise.all([loadJS(autoLoaderOptions.baseJs.map(item => typeof item === 'string' ? buildJSItem(urlBuilder.getFullUrl(item)) : item)), loadCSS(autoLoaderOptions.baseCss.map(item => typeof item === 'string' ? buildCSSItem(urlBuilder.getFullUrl(item)) : item))]);
313
342
  const {
314
343
  markmap
315
344
  } = window;
@@ -318,7 +347,8 @@ const ready = Promise.all([loadJS(autoLoaderOptions.baseJs.map(item => typeof it
318
347
  // Insert global CSS to body so it has higher priority than prism.css, etc.
319
348
  document.body.prepend(style);
320
349
  autoLoaderOptions.onReady == null ? void 0 : autoLoaderOptions.onReady();
321
- });
350
+ }
351
+ const ready = initialize();
322
352
  function transform(transformer, content) {
323
353
  const result = transformer.transform(content);
324
354
  const keys = Object.keys(result.features).filter(key => !enabled[key]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markmap-autoloader",
3
- "version": "0.14.5-alpha.21+fb81f2c",
3
+ "version": "0.14.5-alpha.22+04afb38",
4
4
  "description": "Load markmaps automatically in HTML",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
@@ -38,10 +38,10 @@
38
38
  "markmap-common": "*"
39
39
  },
40
40
  "devDependencies": {
41
- "markmap-common": "0.14.5-alpha.21+fb81f2c",
42
- "markmap-lib": "0.14.5-alpha.21+fb81f2c",
43
- "markmap-toolbar": "0.14.5-alpha.21+fb81f2c",
44
- "markmap-view": "0.14.5-alpha.21+fb81f2c"
41
+ "markmap-common": "0.14.5-alpha.22+04afb38",
42
+ "markmap-lib": "0.14.5-alpha.22+04afb38",
43
+ "markmap-toolbar": "0.14.5-alpha.22+04afb38",
44
+ "markmap-view": "0.14.5-alpha.22+04afb38"
45
45
  },
46
- "gitHead": "fb81f2cb688f400f17f911774539267219f639ce"
46
+ "gitHead": "04afb38dd0a6f3b5e8eaffde3c29e9e38b215c35"
47
47
  }