markdown-up 3.2.1 → 3.3.1

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/lib/app.js CHANGED
@@ -75,6 +75,9 @@ enum MarkdownUpView
75
75
  * The MarkdownUp application options
76
76
  *
77
77
  * @typedef {Object} MarkdownUpOptions
78
+ * @property {?Object.<string, function>} [codeBlocks = null] - The
79
+ * [code block]{@link https://craigahobbs.github.io/markdown-model/module-lib_elements.html#~CodeBlockFn}
80
+ * render function map
78
81
  * @property {?boolean} [darkMode = false] - If true, use dark mode by default
79
82
  * @property {?number} [fontSize = 12] - The font size, in points
80
83
  * @property {?Object} [globals = null] - Global script runtime variables
@@ -101,6 +104,7 @@ export class MarkdownUp {
101
104
  this.params = null;
102
105
  this.paramsLocal = null;
103
106
  this.paramsSession = null;
107
+ this.codeBlocks = (options !== null ? options.codeBlocks : null) ?? null;
104
108
  this.darkMode = (options !== null ? options.darkMode : null) ??
105
109
  (this.window.matchMedia && this.window.matchMedia('(prefers-color-scheme: dark)').matches);
106
110
  this.fontSize = (options !== null ? options.fontSize : null) ?? 12;
@@ -264,40 +268,44 @@ export class MarkdownUp {
264
268
 
265
269
 
266
270
  async main() {
271
+ const url = this.getResourceURL();
272
+ const scriptOptions = this.createScriptOptions();
273
+
267
274
  // Help?
268
275
  if (this.params.view === 'help') {
276
+ const schemaMarkdownDocOptions = {
277
+ 'params': encodeQueryString(this.params),
278
+ 'markdownOptions': scriptOptions.markdownOptions
279
+ };
269
280
  return {
270
281
  'title': 'MarkdownUp',
271
282
  'elements': [
272
283
  this.burgerElements(),
273
- schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)})
284
+ schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', schemaMarkdownDocOptions)
274
285
  ]
275
286
  };
276
287
  }
277
288
 
278
289
  // Get the Markdown text
279
- const urlOverride = 'url' in this.params && this.params.url !== '';
280
- const url = urlOverride ? this.params.url : this.url;
281
- const scriptOptions = this.createScriptOptions(url);
282
290
  let markdownText;
283
291
  let timeBegin;
284
- if (this.markdownText !== null && !urlOverride) {
292
+ if (this.markdownText !== null && !('url' in this.params && this.params.url !== '')) {
285
293
  ({markdownText} = this);
286
294
 
287
295
  // Log Markdown render begin
288
- if ('logFn' in scriptOptions && scriptOptions.debug) {
296
+ if (scriptOptions.debug) {
289
297
  timeBegin = performance.now();
290
298
  scriptOptions.logFn('MarkdownUp: ===== Rendering Markdown text');
291
299
  }
292
300
  } else {
293
301
  // Log Markdown render begin
294
- if ('logFn' in scriptOptions && scriptOptions.debug) {
302
+ if (scriptOptions.debug) {
295
303
  scriptOptions.logFn(`MarkdownUp: ===== Rendering Markdown document "${url}"`);
296
304
  }
297
305
 
298
306
  // Log Markdown fetch begin
299
307
  let fetchBegin;
300
- if ('logFn' in scriptOptions && scriptOptions.debug) {
308
+ if (scriptOptions.debug) {
301
309
  fetchBegin = performance.now();
302
310
  scriptOptions.logFn(`MarkdownUp: Fetching "${url}" ...`);
303
311
  }
@@ -317,7 +325,7 @@ export class MarkdownUp {
317
325
  markdownText = await response.text();
318
326
 
319
327
  // Log Markdown fetch end with timing
320
- if ('logFn' in scriptOptions && scriptOptions.debug) {
328
+ if (scriptOptions.debug) {
321
329
  const fetchEnd = performance.now();
322
330
  timeBegin = performance.now();
323
331
  scriptOptions.logFn(`MarkdownUp: Fetch completed in ${(fetchEnd - fetchBegin).toFixed(1)} milliseconds`);
@@ -339,17 +347,13 @@ export class MarkdownUp {
339
347
  };
340
348
  }
341
349
 
350
+ // Copy the markdown options and override the markdown-script code block renderer
351
+ const scriptMarkdownOptions = {...scriptOptions.markdownOptions};
352
+ scriptMarkdownOptions.codeBlocks = {...scriptMarkdownOptions.codeBlocks};
353
+ scriptMarkdownOptions.codeBlocks['markdown-script'] =
354
+ (codeBlock, _options) => markdownScriptCodeBlock(codeBlock, scriptOptions);
355
+
342
356
  // Render the Markdown
343
- const markdownOptions = {
344
- 'codeBlocks': {
345
- 'markdown-script': (codeBlock) => markdownScriptCodeBlock(codeBlock, scriptOptions)
346
- },
347
- 'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
348
- 'headerIds': true
349
- };
350
- if (this.window.navigator.clipboard) {
351
- markdownOptions.copyFn = (text) => this.window.navigator.clipboard.writeText(text);
352
- }
353
357
  const result = {
354
358
  'title': markdownTitle,
355
359
  'elements': [
@@ -357,7 +361,7 @@ export class MarkdownUp {
357
361
  this.burgerElements(),
358
362
 
359
363
  // Render the markdown
360
- await markdownElementsAsync(markdownModel, markdownOptions)
364
+ await markdownElementsAsync(markdownModel, scriptMarkdownOptions)
361
365
  ]
362
366
  };
363
367
 
@@ -382,7 +386,7 @@ export class MarkdownUp {
382
386
  scriptOptions.runtime.reset();
383
387
 
384
388
  // Log Markdown render end
385
- if ('logFn' in scriptOptions && scriptOptions.debug) {
389
+ if (scriptOptions.debug) {
386
390
  const timeEnd = performance.now();
387
391
  scriptOptions.logFn(`MarkdownUp: Markdown rendered in ${(timeEnd - timeBegin).toFixed(1)} milliseconds`);
388
392
  }
@@ -391,7 +395,28 @@ export class MarkdownUp {
391
395
  }
392
396
 
393
397
 
394
- createScriptOptions(resourceURL) {
398
+ getResourceURL() {
399
+ if ('url' in this.params && this.params.url !== '') {
400
+ return this.params.url;
401
+ }
402
+ return this.url;
403
+ }
404
+
405
+
406
+ createScriptOptions() {
407
+ // Create the markdown options object
408
+ const urlFn = (url) => this.modifyURL(url);
409
+ const markdownOptions = {
410
+ 'codeBlocks': this.codeBlocks ?? {},
411
+ urlFn,
412
+ 'headerIds': true,
413
+ 'usedHeaderIds': new Set()
414
+ };
415
+ if (this.window.navigator.clipboard) {
416
+ markdownOptions.copyFn = (text) => this.window.navigator.clipboard.writeText(text);
417
+ }
418
+
419
+ // Create the script options object
395
420
  const logFn = (text) => {
396
421
  this.window.console.log(text);
397
422
  };
@@ -401,9 +426,10 @@ export class MarkdownUp {
401
426
  'fetchFn': async (fetchURL, options) => this.window.fetch(fetchURL, options),
402
427
  'fontSize': this.paramsLocal.fontSize ?? this.fontSize,
403
428
  logFn,
429
+ markdownOptions,
404
430
  'params': this.params,
405
431
  'systemPrefix': this.systemPrefix,
406
- 'urlFn': (url) => this.modifyURL(url, resourceURL),
432
+ urlFn,
407
433
  'window': this.window
408
434
  };
409
435
 
@@ -415,7 +441,7 @@ export class MarkdownUp {
415
441
  try {
416
442
  scriptOptions.variables[varName] = evaluateExpression(parseExpression(varExprStr), scriptOptions.variables);
417
443
  } catch ({message}) {
418
- if (logFn !== null && scriptOptions.debug) {
444
+ if (scriptOptions.debug) {
419
445
  logFn(`MarkdownUp: Error evaluating variable "${varName}" expression "${varExprStr}": ${message}`);
420
446
  }
421
447
  }
@@ -474,7 +500,9 @@ export class MarkdownUp {
474
500
  }
475
501
 
476
502
 
477
- modifyURL(url, resourceURL) {
503
+ modifyURL(url) {
504
+ const resourceURL = this.getResourceURL();
505
+
478
506
  // Hash URL?
479
507
  if (url.startsWith('#')) {
480
508
  // Fixup the "url" param if its relative
package/lib/script.js CHANGED
@@ -274,12 +274,8 @@ export class MarkdownScriptRuntime {
274
274
  // Helper method to finish the current Markdown block and add its elements
275
275
  finishMarkdown() {
276
276
  if (this.markdown !== null) {
277
- const markdownOptions = {'headerIds': true, 'urlFn': this.options.urlFn};
278
- if (this.options.window.navigator.clipboard) {
279
- markdownOptions.copyFn = (text) => this.options.window.navigator.clipboard.writeText(text);
280
- }
281
277
  try {
282
- this.addElements(markdownElements(parseMarkdown(this.markdown), markdownOptions));
278
+ this.addElements(markdownElements(parseMarkdown(this.markdown), this.options.markdownOptions));
283
279
  } catch {
284
280
  // Do nothing
285
281
  }
@@ -864,8 +864,10 @@ const markdownTitleArgs = valueArgsModel([
864
864
  // $return: The schema type's documentation [element model](https://github.com/craigahobbs/element-model#readme)
865
865
  function schemaElements(args, options) {
866
866
  const [types, typeName, actionURLs = null] = valueArgsValidate(schemaElementsArgs, args);
867
- const params = encodeQueryString(options.params);
868
- const schemaOptions = {params};
867
+ const schemaOptions = {
868
+ 'params': encodeQueryString(options.params),
869
+ 'markdownOptions': options.markdownOptions
870
+ };
869
871
  if (actionURLs !== null) {
870
872
  schemaOptions.actionURLs = actionURLs;
871
873
  }
@@ -1023,7 +1025,7 @@ function windowSetResize(args, options) {
1023
1025
  try {
1024
1026
  await callback([], options);
1025
1027
  } catch ({message}) {
1026
- if ('logFn' in options && options.debug) {
1028
+ if (options.debug) {
1027
1029
  options.logFn(`MarkdownUp: Error executing windowSetResize callback: ${message}`);
1028
1030
  }
1029
1031
  }
@@ -1050,7 +1052,7 @@ function windowSetTimeout(args, options) {
1050
1052
  try {
1051
1053
  await callback([], options);
1052
1054
  } catch ({message}) {
1053
- if ('logFn' in options && options.debug) {
1055
+ if (options.debug) {
1054
1056
  options.logFn(`MarkdownUp: Error executing windowSetTimeout callback: ${message}`);
1055
1057
  }
1056
1058
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "3.2.1",
4
+ "version": "3.3.1",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -24,9 +24,9 @@
24
24
  "dependencies": {
25
25
  "bare-script": "~3.0",
26
26
  "element-model": "~1.1",
27
- "markdown-model": "~2.1",
27
+ "markdown-model": "~2.2",
28
28
  "schema-markdown": "~1.2",
29
- "schema-markdown-doc": "~1.8"
29
+ "schema-markdown-doc": "~2.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "c8": "~10.1",