markdown-up 1.8.7 → 1.8.9

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/README.md CHANGED
@@ -3,14 +3,19 @@
3
3
  [![npm](https://img.shields.io/npm/v/markdown-up)](https://www.npmjs.com/package/markdown-up)
4
4
  [![GitHub](https://img.shields.io/github/license/craigahobbs/markdown-up)](https://github.com/craigahobbs/markdown-up/blob/main/LICENSE)
5
5
 
6
- [MarkdownUp](https://craigahobbs.github.io/markdown-up/)
7
- is a Markdown viewer.
6
+ [MarkdownUp](https://craigahobbs.github.io/markdown-up/) is a Markdown viewer.
7
+
8
+
9
+ ## Links
10
+
11
+ - [MarkdownUp Application](https://craigahobbs.github.io/markdown-up/)
12
+ - [Source code](https://github.com/craigahobbs/markdown-up)
8
13
 
9
14
 
10
15
  ## View Local Markdown Files
11
16
 
12
17
  To view local Markdown files, use the
13
- [MarkdownUp launcher](https://pypi.org/project/markdown-up/)
18
+ [MarkdownUp launcher](https://github.com/craigahobbs/markdown-up-py#readme)
14
19
  from a terminal prompt:
15
20
 
16
21
  ~~~
@@ -142,8 +147,8 @@ CSV, manipulating data, and rendering tables and charts. For more information, r
142
147
 
143
148
  ## Development
144
149
 
145
- MarkdownUp is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme)
146
- and it was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme):
150
+ This package is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme).
151
+ It was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme) as follows:
147
152
 
148
153
  ~~~
149
154
  template-specialize javascript-template/template/ markdown-up/ -k package markdown-up -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs'
package/lib/app.js CHANGED
@@ -142,12 +142,6 @@ export class MarkdownUp {
142
142
  isError = true;
143
143
  }
144
144
 
145
- // Call the application main and validate the result
146
- this.clearRuntimeCallbacks();
147
- if (!isError) {
148
- result = await this.main();
149
- }
150
-
151
145
  // Set the colors
152
146
  const isDarkMode = this.paramsLocal.darkMode ?? this.darkMode;
153
147
  this.window.document.documentElement.style.setProperty('--markdown-model-dark-mode', isDarkMode ? '1' : '0');
@@ -160,6 +154,12 @@ export class MarkdownUp {
160
154
  const lineHeight = this.paramsLocal.lineHeight ?? this.lineHeight;
161
155
  this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
162
156
 
157
+ // Call the application main and validate the result
158
+ this.clearRuntimeCallbacks();
159
+ if (!isError) {
160
+ result = await this.main();
161
+ }
162
+
163
163
  // Set the window title
164
164
  const title = result.title ?? null;
165
165
  if (title !== null) {
@@ -268,8 +268,8 @@ export class MarkdownUp {
268
268
  return {
269
269
  'title': 'MarkdownUp',
270
270
  'elements': [
271
- schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)}),
272
- this.burgerElements()
271
+ this.burgerElements(),
272
+ schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)})
273
273
  ]
274
274
  };
275
275
  }
@@ -277,10 +277,30 @@ export class MarkdownUp {
277
277
  // Get the Markdown text
278
278
  const urlOverride = 'url' in this.params && this.params.url !== '';
279
279
  const url = urlOverride ? this.params.url : this.url;
280
+ const scriptOptions = this.createScriptOptions(url);
280
281
  let markdownText = null;
282
+ let timeBegin;
281
283
  if (this.markdownText !== null && !urlOverride) {
282
284
  ({markdownText} = this);
285
+
286
+ // Log Markdown render begin
287
+ if ('logFn' in scriptOptions) {
288
+ timeBegin = performance.now();
289
+ scriptOptions.logFn('MarkdownUp: ===== Rendering Markdown text');
290
+ }
283
291
  } else {
292
+ // Log Markdown render begin
293
+ if ('logFn' in scriptOptions) {
294
+ scriptOptions.logFn(`MarkdownUp: ===== Rendering Markdown document "${url}"`);
295
+ }
296
+
297
+ // Log Markdown fetch begin
298
+ let fetchBegin;
299
+ if ('logFn' in scriptOptions) {
300
+ fetchBegin = performance.now();
301
+ scriptOptions.logFn(`MarkdownUp: Fetching "${url}" ...`);
302
+ }
303
+
284
304
  // Fetch the Markdown text resource URL
285
305
  const response = await this.window.fetch(url);
286
306
  if (!response.ok) {
@@ -294,6 +314,13 @@ export class MarkdownUp {
294
314
  };
295
315
  }
296
316
  markdownText = await response.text();
317
+
318
+ // Log Markdown fetch end with timing
319
+ if ('logFn' in scriptOptions) {
320
+ const fetchEnd = performance.now();
321
+ timeBegin = performance.now();
322
+ scriptOptions.logFn(`MarkdownUp: Fetch completed in ${(fetchEnd - fetchBegin).toFixed(1)} milliseconds`);
323
+ }
297
324
  }
298
325
 
299
326
  // Parse the Markdown and get the title
@@ -305,17 +332,19 @@ export class MarkdownUp {
305
332
  return {
306
333
  'title': markdownTitle,
307
334
  'elements': [
308
- {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}},
309
- this.burgerElements()
335
+ this.burgerElements(),
336
+ {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}}
310
337
  ]
311
338
  };
312
339
  }
313
340
 
314
341
  // Render the Markdown
315
- const scriptOptions = this.createScriptOptions(url);
316
342
  const result = {
317
343
  'title': markdownTitle,
318
344
  'elements': [
345
+ // Menu "burger"
346
+ this.burgerElements(),
347
+
319
348
  // Add a top hash ID, if necessary
320
349
  Object.keys(this.params).length === 0
321
350
  ? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
@@ -327,10 +356,7 @@ export class MarkdownUp {
327
356
  },
328
357
  'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
329
358
  'headerIds': true
330
- }),
331
-
332
- // Popup menu burger
333
- this.burgerElements()
359
+ })
334
360
  ]
335
361
  };
336
362
 
@@ -354,6 +380,12 @@ export class MarkdownUp {
354
380
  // Reset the runtime
355
381
  scriptOptions.runtime.reset();
356
382
 
383
+ // Log Markdown render end
384
+ if ('logFn' in scriptOptions) {
385
+ const timeEnd = performance.now();
386
+ scriptOptions.logFn(`MarkdownUp: Markdown rendered in ${(timeEnd - timeBegin).toFixed(1)} milliseconds`);
387
+ }
388
+
357
389
  return result;
358
390
  }
359
391
 
@@ -386,7 +418,7 @@ export class MarkdownUp {
386
418
  scriptOptions.variables[varName] = evaluateExpression(parseExpression(varExprStr), scriptOptions.variables);
387
419
  } catch ({message}) {
388
420
  if (logFn !== null) {
389
- logFn(`Error evaluating variable "${varName}" expression "${varExprStr}": ${message}`);
421
+ logFn(`MarkdownUp: Error evaluating variable "${varName}" expression "${varExprStr}": ${message}`);
390
422
  }
391
423
  }
392
424
  }
@@ -412,12 +444,18 @@ export class MarkdownUp {
412
444
  }
413
445
 
414
446
  // Reset the runtime and render
415
- const {isDocumentReset, windowLocation} = runtime;
416
- const documentElements = [
417
- runtime.resetElements(),
418
- isDocumentReset ? this.burgerElements() : null
419
- ];
420
- renderElements(this.window.document.body, documentElements, isDocumentReset);
447
+ const elements = runtime.resetElements();
448
+ if (elements !== null) {
449
+ const {body} = this.window.document;
450
+ if (runtime.documentReset === null) {
451
+ renderElements(body, [this.burgerElements(), elements]);
452
+ } else {
453
+ while (body.lastChild !== null && body.lastChild.id !== runtime.documentReset) {
454
+ body.lastChild.remove();
455
+ }
456
+ renderElements(body, [elements], false);
457
+ }
458
+ }
421
459
 
422
460
  // Focus?
423
461
  if (runtime.documentFocus !== null) {
@@ -426,8 +464,8 @@ export class MarkdownUp {
426
464
 
427
465
  // Navigate?
428
466
  // Note: This is done after render since it may have no effect (in which case we want to render).
429
- if (windowLocation !== null) {
430
- this.window.location.href = windowLocation;
467
+ if (runtime.windowLocation !== null) {
468
+ this.window.location.href = runtime.windowLocation;
431
469
  }
432
470
 
433
471
  // Reset the runtime
package/lib/script.js CHANGED
@@ -49,6 +49,13 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
49
49
  Object.assign(options.globals, options.variables);
50
50
  }
51
51
 
52
+ // Log script execution begin
53
+ let timeBegin;
54
+ if ('logFn' in options) {
55
+ timeBegin = performance.now();
56
+ options.logFn(`MarkdownUp: Executing script at line number ${codeBlock.startLineNumber + 1} ...`);
57
+ }
58
+
52
59
  // Execute the calculation script
53
60
  let errorMessage = null;
54
61
  try {
@@ -58,10 +65,12 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
58
65
  // Run the calc-script linter?
59
66
  if ('logFn' in options) {
60
67
  const warnings = lintScript(script);
61
- if (warnings.length > 0) {
62
- options.logFn(`Warnings for the script at line number ${codeBlock.startLineNumber + 1}:`);
68
+ if (warnings.length === 0) {
69
+ options.logFn(`MarkdownUp: Script static analysis... OK`);
70
+ } else {
71
+ options.logFn(`MarkdownUp: Script static analysis ... ${warnings.length} warning${warnings.length > 1 ? 's' : ''}:`);
63
72
  for (const warning of warnings) {
64
- options.logFn(` ${warning}`);
73
+ options.logFn(`MarkdownUp: ${warning}`);
65
74
  }
66
75
  }
67
76
  }
@@ -72,6 +81,12 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
72
81
  errorMessage = message;
73
82
  }
74
83
 
84
+ // Log script execution end with timing
85
+ if ('logFn' in options) {
86
+ const timeEnd = performance.now();
87
+ options.logFn(`MarkdownUp: Script executed in ${(timeEnd - timeBegin).toFixed(1)} milliseconds`);
88
+ }
89
+
75
90
  // Reset the runtime
76
91
  let elements = options.runtime.resetElements();
77
92
 
@@ -96,12 +111,12 @@ const pixelsPerPoint = 4 / 3;
96
111
  *
97
112
  * @property {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
98
113
  * @property {?Object[]} elements - The runtime-generated element model
99
- * @property {?string} documentFocus - The the runtime-set element ID to set input focus
100
- * @property {?string} documentTitle - The the runtime-set document title
101
- * @property {boolean} isDocumentReset - If true, the runtime requested a document-reset
102
- * @property {?string} windowLocation - The the runtime-set document location
103
- * @property {?function} windowResize - The the runtime-set resize callback
104
- * @property {?Array} windowTimeout - The the runtime-set timeout args (callback, delay)
114
+ * @property {?string} documentFocus - The runtime-set input-focus element ID
115
+ * @property {?string} documentReset - The runtime-set document-reset element ID
116
+ * @property {?string} documentTitle - The runtime-set document title
117
+ * @property {?string} windowLocation - The runtime-set document location
118
+ * @property {?function} windowResize - The runtime-set resize callback
119
+ * @property {?Array} windowTimeout - The runtime-set timeout args (callback, delay)
105
120
  *
106
121
  * @ignore
107
122
  */
@@ -113,8 +128,8 @@ export class MarkdownScriptRuntime {
113
128
  this.options = options;
114
129
  this.elements = null;
115
130
  this.documentFocus = null;
131
+ this.documentReset = null;
116
132
  this.documentTitle = null;
117
- this.isDocumentReset = false;
118
133
  this.windowLocation = null;
119
134
  this.windowResize = null;
120
135
  this.windowTimeout = null;
@@ -145,8 +160,8 @@ export class MarkdownScriptRuntime {
145
160
  // Reset the runtime state
146
161
  reset() {
147
162
  this.documentFocus = null;
163
+ this.documentReset = null;
148
164
  this.documentTitle = null;
149
- this.isDocumentReset = false;
150
165
  this.windowLocation = null;
151
166
  this.windowResize = null;
152
167
  this.windowTimeout = null;
@@ -144,14 +144,6 @@ export const markdownScriptFunctions = {
144
144
  // Document functions
145
145
  //
146
146
 
147
- // $function: documentReset
148
- // $group: Document
149
- // $doc: Reset the document. This is useful within callback functions that re-render the document.
150
- 'documentReset': (unused, options) => {
151
- const {runtime} = options;
152
- runtime.isDocumentReset = true;
153
- },
154
-
155
147
  // $function: documentURL
156
148
  // $group: Document
157
149
  // $doc: Fix-up relative URLs
@@ -196,6 +188,15 @@ export const markdownScriptFunctions = {
196
188
  runtime.documentFocus = id;
197
189
  },
198
190
 
191
+ // $function: setDocumentReset
192
+ // $group: Document
193
+ // $doc: Set the document reset element
194
+ // $arg id: The element ID
195
+ 'setDocumentReset': ([id], options) => {
196
+ const {runtime} = options;
197
+ runtime.documentReset = id;
198
+ },
199
+
199
200
  // $function: setDocumentTitle
200
201
  // $group: Document
201
202
  // $doc: Set the document title
@@ -220,9 +221,9 @@ export const markdownScriptFunctions = {
220
221
  // $arg callback: The window resize callback function
221
222
  'setWindowResize': ([callback], options) => {
222
223
  const {runtime} = options;
223
- runtime.windowResize = () => {
224
+ runtime.windowResize = async () => {
224
225
  options.statementCount = 0;
225
- callback([], options);
226
+ await callback([], options);
226
227
  options.runtimeUpdateFn();
227
228
  };
228
229
  },
@@ -235,9 +236,9 @@ export const markdownScriptFunctions = {
235
236
  'setWindowTimeout': ([callback, delay], options) => {
236
237
  const {runtime} = options;
237
238
  runtime.windowTimeout = [
238
- () => {
239
+ async () => {
239
240
  options.statementCount = 0;
240
- callback([], options);
241
+ await callback([], options);
241
242
  options.runtimeUpdateFn();
242
243
  },
243
244
  delay
@@ -387,12 +388,13 @@ export const markdownScriptFunctions = {
387
388
  'drawOnClick': ([callback], options) => {
388
389
  const {runtime} = options;
389
390
  const svg = runtime.setDrawing();
390
- const clickElement = svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1];
391
+ const clickElement = (svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1]);
392
+ clickElement.attr.style = 'cursor: pointer; user-select: none;';
391
393
  clickElement.callback = (element) => {
392
- element.addEventListener('click', (event) => {
394
+ element.addEventListener('click', async (event) => {
393
395
  const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
394
396
  options.statementCount = 0;
395
- callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
397
+ await callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
396
398
  options.runtimeUpdateFn();
397
399
  });
398
400
  };
@@ -714,7 +716,7 @@ function elementModelWrapCallbacks(elements, options) {
714
716
  elements.callback = (element) => {
715
717
  // On element render, add a listener for each event
716
718
  for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
717
- element.addEventListener(elementEvent, (event) => {
719
+ element.addEventListener(elementEvent, async (event) => {
718
720
  // Determine the event callback args
719
721
  const eventArgs = [];
720
722
  if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
@@ -723,7 +725,7 @@ function elementModelWrapCallbacks(elements, options) {
723
725
 
724
726
  // Call the event handler
725
727
  options.statementCount = 0;
726
- elementEventCallback(eventArgs, options);
728
+ await elementEventCallback(eventArgs, options);
727
729
  options.runtimeUpdateFn();
728
730
  });
729
731
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.8.7",
4
+ "version": "1.8.9",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -29,9 +29,9 @@
29
29
  "schema-markdown-doc": "~1.5"
30
30
  },
31
31
  "devDependencies": {
32
- "ava": "~5.0",
32
+ "ava": "~5.1",
33
33
  "c8": "~7.12",
34
- "eslint": "~8.27",
34
+ "eslint": "~8.31",
35
35
  "jsdoc": "~4.0",
36
36
  "jsdom": "~20.0"
37
37
  }