markdown-up 1.8.2 → 1.8.3

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 (3) hide show
  1. package/README.md +2 -1
  2. package/lib/app.js +183 -153
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -117,10 +117,11 @@ For example:
117
117
  debugLog('Hello debug')
118
118
  ~~~
119
119
 
120
- Add the "#debug=1" hash parameter to the application URL to turn on debug mode.
121
120
  Debug mode also enables the CalcScript linter, which performs static code analysis on your code and
122
121
  reports warnings for any issues found.
123
122
 
123
+ To turn on debug mode, click the debug button in the MarkdownUp menu in the upper-right of the page.
124
+
124
125
 
125
126
  ### Links
126
127
 
package/lib/app.js CHANGED
@@ -30,33 +30,41 @@ struct MarkdownUp
30
30
  # The resource URL
31
31
  optional string url
32
32
 
33
- # Optional command
34
- optional Command cmd
33
+ # Variable expressions
34
+ optional string{} var
35
35
 
36
- # The menu visibility (default is collapsed)
37
- optional int(== 1) menu
36
+
37
+ # The MarkdownUp local storage JSON schema
38
+ struct MarkdownUpLocal
38
39
 
39
40
  # The font size
40
41
  optional int(>= 8, <= 18) fontSize
41
42
 
42
43
  # The line height
43
- optional int(>= 1, <= 2) lineHeight
44
+ optional float(>= 1, <= 2) lineHeight
44
45
 
45
- # Variable expressions
46
- optional string{} var
46
+
47
+ # The MarkdownUp session storage JSON schema
48
+ struct MarkdownUpSession
49
+
50
+ # Optional command
51
+ optional MarkdownUpView view
52
+
53
+ # The menu visibility (default is collapsed)
54
+ optional int(== 1) menu
47
55
 
48
56
  # If true, enable debug behavior
49
57
  optional int(== 1) debug
50
58
 
51
59
 
52
- # A MarkdownUp application command
53
- union Command
60
+ # A MarkdownUp application view
61
+ enum MarkdownUpView
54
62
 
55
63
  # Display the hash parameters documentation
56
- int(== 1) help
64
+ help
57
65
 
58
66
  # Show the resource's Markdown text
59
- int(== 1) markdown
67
+ markdown
60
68
  `);
61
69
 
62
70
 
@@ -82,14 +90,16 @@ export class MarkdownUp {
82
90
  * @param {Object} window - The web browser window object
83
91
  * @param {Object} [options] - The [application options]{@link module:lib/app~MarkdownUpOptions}
84
92
  */
85
- constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
93
+ constructor(window, options = null) {
86
94
  this.window = window;
87
95
  this.params = null;
88
- this.fontSize = fontSize;
89
- this.lineHeight = lineHeight;
90
- this.markdownText = markdownText;
91
- this.menu = menu;
92
- this.url = url;
96
+ this.paramsLocal = null;
97
+ this.paramsSession = null;
98
+ this.fontSize = (options !== null ? options.fontSize : null) ?? 12;
99
+ this.lineHeight = (options !== null ? options.lineHeight : null) ?? 1.2;
100
+ this.markdownText = (options !== null ? options.markdownText : null) ?? null;
101
+ this.menu = (options !== null ? options.menu : null) ?? true;
102
+ this.url = (options !== null ? options.url : null) ?? 'README.md';
93
103
  this.runtimeWindowResize = null;
94
104
  this.runtimeTimeoutId = null;
95
105
  }
@@ -104,7 +114,7 @@ export class MarkdownUp {
104
114
  }
105
115
 
106
116
 
107
- async render() {
117
+ async render(forceRender = false) {
108
118
  // Parse the hash parameters and render the application element model
109
119
  let result;
110
120
  let isError = false;
@@ -114,7 +124,7 @@ export class MarkdownUp {
114
124
  this.updateParams();
115
125
 
116
126
  // Skip the render if the page params haven't changed
117
- if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
127
+ if (!forceRender && paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
118
128
  return;
119
129
  }
120
130
  } catch ({message}) {
@@ -132,11 +142,11 @@ export class MarkdownUp {
132
142
  }
133
143
 
134
144
  // Set the font size
135
- const fontSize = (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
145
+ const fontSize = this.paramsLocal.fontSize ?? this.fontSize;
136
146
  this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
137
147
 
138
148
  // Set the line height
139
- const lineHeight = (this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight);
149
+ const lineHeight = this.paramsLocal.lineHeight ?? this.lineHeight;
140
150
  this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
141
151
 
142
152
  // Set the window title
@@ -179,15 +189,38 @@ export class MarkdownUp {
179
189
  }
180
190
 
181
191
 
182
- updateParams(paramString = null) {
192
+ updateParams(paramString = null, localJSONString = null, sessionJSONString = null) {
183
193
  // Clear, then validate the hash parameters (may throw)
184
194
  this.params = null;
195
+ this.paramsLocal = {};
196
+ this.paramsSession = {};
197
+
198
+ // Decode and validate the hash parameters
199
+ this.params = validateType(
200
+ markdownUpTypes,
201
+ 'MarkdownUp',
202
+ decodeQueryString(paramString ?? this.window.location.hash.slice(1))
203
+ );
204
+
205
+ // Decode and validate the local storage paramters
206
+ const localJSON = localJSONString ?? this.window.localStorage.getItem('MarkdownUp');
207
+ if (localJSON !== null) {
208
+ try {
209
+ this.paramsLocal = validateType(markdownUpTypes, 'MarkdownUpLocal', JSON.parse(localJSON));
210
+ } catch {
211
+ // Do nothing
212
+ }
213
+ }
185
214
 
186
- // Decode the params string
187
- const params = decodeQueryString(paramString !== null ? paramString : this.window.location.hash.slice(1));
188
-
189
- // Validate the params
190
- this.params = validateType(markdownUpTypes, 'MarkdownUp', params);
215
+ // Decode and validate the session storage parameters
216
+ const sessionJSON = sessionJSONString ?? this.window.sessionStorage.getItem('MarkdownUp');
217
+ if (sessionJSON !== null) {
218
+ try {
219
+ this.paramsSession = validateType(markdownUpTypes, 'MarkdownUpSession', JSON.parse(sessionJSON));
220
+ } catch {
221
+ // Do nothing
222
+ }
223
+ }
191
224
  }
192
225
 
193
226
 
@@ -220,7 +253,7 @@ export class MarkdownUp {
220
253
 
221
254
  async main() {
222
255
  // Help?
223
- if ('cmd' in this.params && 'help' in this.params.cmd) {
256
+ if (this.paramsSession.view === 'help') {
224
257
  return {
225
258
  'title': 'MarkdownUp',
226
259
  'elements': [
@@ -257,7 +290,7 @@ export class MarkdownUp {
257
290
  const markdownTitle = getMarkdownTitle(markdownModel);
258
291
 
259
292
  // Display the Markdown?
260
- if ('cmd' in this.params && 'markdown' in this.params.cmd) {
293
+ if (this.paramsSession.view === 'markdown') {
261
294
  return {
262
295
  'title': markdownTitle,
263
296
  'elements': [
@@ -318,25 +351,33 @@ export class MarkdownUp {
318
351
  const scriptOptions = {
319
352
  // eslint-disable-next-line require-await
320
353
  'fetchFn': async (fetchURL, options) => this.window.fetch(fetchURL, options),
321
- 'fontSize': (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize),
354
+ 'fontSize': this.paramsLocal.fontSize ?? this.fontSize,
322
355
  'params': this.params,
323
356
  'urlFn': (url) => this.modifyURL(url, resourceURL),
324
357
  'window': this.window
325
358
  };
326
359
 
327
360
  // Add log function, if debugging
328
- if ('debug' in this.params) {
329
- scriptOptions.logFn = (text) => {
361
+ let logFn = null;
362
+ if ('debug' in this.paramsSession) {
363
+ logFn = (text) => {
330
364
  this.window.console.log(text);
331
365
  };
366
+ scriptOptions.logFn = logFn;
332
367
  }
333
368
 
334
369
  // Add hash parameter variables, if any
335
370
  if ('var' in this.params) {
336
371
  scriptOptions.variables = {};
337
372
  for (const varName of Object.keys(this.params.var)) {
338
- const varExpr = parseExpression(this.params.var[varName]);
339
- scriptOptions.variables[varName] = evaluateExpression(varExpr, scriptOptions.variables);
373
+ const varExprStr = this.params.var[varName];
374
+ try {
375
+ scriptOptions.variables[varName] = evaluateExpression(parseExpression(varExprStr), scriptOptions.variables);
376
+ } catch ({message}) {
377
+ if (logFn !== null) {
378
+ logFn(`Error evaluating variable "${varName}" expression "${varExprStr}": ${message}`);
379
+ }
380
+ }
340
381
  }
341
382
  }
342
383
 
@@ -412,80 +453,83 @@ export class MarkdownUp {
412
453
  {
413
454
  'html': 'div',
414
455
  'attr': {'class': 'menu-burger'},
415
- 'elem': [
416
- {
417
- 'html': 'a',
418
- 'attr': {'href': this.linkValueToggle('menu', '1'), 'aria-label': 'Menu'},
419
- 'elem': menuSVGElements
420
- }
421
- ]
456
+ 'elem': this.menuValueToggle('menu', menuSVGElements, 'Menu')
422
457
  },
423
458
 
424
459
  // Popup menu
425
- !('menu' in this.params) ? null : {
460
+ !('menu' in this.paramsSession) ? null : {
426
461
  'html': 'div',
427
462
  'attr': {'class': 'menu'},
428
463
  'elem': [
429
- {
430
- 'html': 'a',
431
- 'attr': {'href': this.linkCommandToggle('markdown', '1'), 'aria-label': 'Show Markdown'},
432
- 'elem': markdownSVGElements
433
- },
434
- {
435
- 'html': 'a',
436
- 'attr': {'href': this.linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
437
- 'elem': fontSizeSVGElements
438
- },
439
- {
440
- 'html': 'a',
441
- 'attr': {'href': this.linkCycle('lineHeight', this.lineHeight, 0.2, 1), 'aria-label': 'Line height'},
442
- 'elem': lineHeightSVGElements
443
- },
444
- {
445
- 'html': 'a',
446
- 'attr': {'href': this.linkCommandToggle('help', '1'), 'aria-label': 'Help'},
447
- 'elem': helpSVGElements
448
- }
464
+ this.menuViewToggle('markdown', markdownSVGElements, 'Show Markdown'),
465
+ this.menuValueCycle('fontSize', fontSizeSVGElements, 'Cycle font size', this.fontSize, 2),
466
+ this.menuValueCycle('lineHeight', lineHeightSVGElements, 'Cycle line height', this.lineHeight, 0.2),
467
+ this.menuValueToggle('debug', debugSVGElements, 'Debug'),
468
+ this.menuViewToggle('help', helpSVGElements, 'Help')
449
469
  ]
450
470
  }
451
471
  ];
452
472
  }
453
473
 
454
474
 
455
- linkCycle(paramName, defaultValue, delta, fixed = null) {
456
- const value = paramName in this.params ? this.params[paramName] : defaultValue;
457
- const {attr} = markdownUpTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
458
- let valueNew = value + delta;
459
- if (valueNew > attr.lte) {
460
- valueNew = attr.gte;
461
- }
462
- valueNew = Math.max(attr.gte, Math.min(attr.lte, valueNew));
463
- const params = {...this.params};
464
- params[paramName] = fixed === null ? `${valueNew}` : `${valueNew.toFixed(fixed)}`;
465
- return `#${encodeQueryString(params)}`;
475
+ menuValueToggle(value, svgElements, ariaLabel) {
476
+ return {
477
+ 'html': 'a',
478
+ 'attr': {'style': 'cursor: pointer; user-select: none;', 'aria-label': ariaLabel},
479
+ 'elem': svgElements(value in this.paramsSession),
480
+ 'callback': (element) => {
481
+ element.addEventListener('click', () => {
482
+ if (value in this.paramsSession) {
483
+ delete this.paramsSession[value];
484
+ } else {
485
+ this.paramsSession[value] = 1;
486
+ }
487
+ this.window.sessionStorage.setItem('MarkdownUp', JSON.stringify(this.paramsSession));
488
+ this.render(true);
489
+ });
490
+ }
491
+ };
466
492
  }
467
493
 
468
494
 
469
- linkCommandToggle(command, value) {
470
- const params = {...this.params};
471
- if ('cmd' in this.params && command in this.params.cmd) {
472
- delete params.cmd;
473
- } else {
474
- params.cmd = {};
475
- params.cmd[command] = `${value}`;
476
- }
477
- return `#${encodeQueryString(params)}`;
495
+ menuViewToggle(value, svgElements, ariaLabel) {
496
+ return {
497
+ 'html': 'a',
498
+ 'attr': {'style': 'cursor: pointer; user-select: none;', 'aria-label': ariaLabel},
499
+ 'elem': svgElements(this.paramsSession.view === value),
500
+ 'callback': (element) => {
501
+ element.addEventListener('click', () => {
502
+ if (this.paramsSession.view === value) {
503
+ delete this.paramsSession.view;
504
+ } else {
505
+ this.paramsSession.view = value;
506
+ }
507
+ this.window.sessionStorage.setItem('MarkdownUp', JSON.stringify(this.paramsSession));
508
+ this.render(true);
509
+ });
510
+ }
511
+ };
478
512
  }
479
513
 
480
514
 
481
- linkValueToggle(name, value) {
482
- const params = {...this.params};
483
- if (!(name in this.params)) {
484
- params[name] = value;
485
- } else {
486
- delete params[name];
487
- }
488
- return `#${encodeQueryString(params)}`;
515
+ menuValueCycle(valueName, svgElements, ariaLabel, valueDefault, valueDelta) {
516
+ return {
517
+ 'html': 'a',
518
+ 'attr': {'style': 'cursor: pointer; user-select: none;', 'aria-label': ariaLabel},
519
+ 'elem': svgElements(),
520
+ 'callback': (element) => {
521
+ element.addEventListener('click', () => {
522
+ const {attr} = markdownUpTypes.MarkdownUpLocal.struct.members.find((member) => member.name === valueName);
523
+ let valueNew = (this.paramsLocal[valueName] ?? valueDefault) + valueDelta;
524
+ if (valueNew > attr.lte) {
525
+ valueNew = attr.gte;
526
+ }
527
+ this.paramsLocal[valueName] = valueNew;
528
+ this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
529
+ this.render(true);
530
+ });
531
+ }
532
+ };
489
533
  }
490
534
  }
491
535
 
@@ -499,70 +543,56 @@ function getHashID(url) {
499
543
  const rHashId = /[#&]([^=]+)$/;
500
544
 
501
545
 
502
- // Icon sizes
503
- const burgerSize = '24';
504
- const iconSize = '36';
505
-
506
-
507
- // "menu" SVG element model
508
- const menuSVGElements = {
509
- 'svg': 'svg',
510
- 'attr': {'width': burgerSize, 'height': burgerSize, 'viewBox': '0 0 24 24'},
511
- 'elem': {
512
- 'svg': 'path',
513
- 'attr': {'d': 'M3,5 L21,5 M3,12 L21,12 M3,19 L21,19', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
514
- }
515
- };
516
-
517
-
518
- // Font size cycle SVG element model
519
- const fontSizeSVGElements = {
520
- 'svg': 'svg',
521
- 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
522
- 'elem': {
523
- 'svg': 'path',
524
- 'attr': {'d': 'M4,22 L10,2 L14,2 L20,22 M6,14 L18,14', 'stroke': 'black', 'stroke-width': '4', 'fill': 'none'}
525
- }
526
- };
527
-
528
-
529
- // Line height cycle SVG element model
530
- const lineHeightSVGElements = {
531
- 'svg': 'svg',
532
- 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
533
- 'elem': {
534
- 'svg': 'path',
535
- 'attr': {'d': 'M2,3 L22,3 M2,9 L22,9 M2,15 L22,15 M2,21 L22,21', 'stroke': 'black', 'stroke-width': '2', 'fill': 'none'}
536
- }
537
- };
538
-
539
-
540
- // "markdown" SVG element model
541
- const markdownSVGElements = {
542
- 'svg': 'svg',
543
- 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
544
- 'elem': [
545
- {
546
- 'svg': 'path',
547
- 'attr': {'d': 'M4,2 L20,2 L20,22 L4,22 Z', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
548
- },
546
+ // Menu icons
547
+ const menuSVGElements = () => menuSVGHelper('M3,5 L21,5 M3,12 L21,12 M3,19 L21,19', false, 3, null, 32);
548
+ const debugSVGElements = (isChecked) => menuSVGHelper(
549
+ 'M12,5 A4,7,0,1,0,12,19 A4,7,0,1,0,12,5 M9,9 L15,9 M9,9 L4,6 M9,12 L3,12 M9,15 L4,18 M15,9 L20,6 M15,12 L21,12 M15,15 L20,18',
550
+ isChecked
551
+ );
552
+ const fontSizeSVGElements = () => menuSVGHelper('M4,22 L10,2 L14,2 L20,22 M6,14 L18,14', false, 4);
553
+ const helpSVGElements = (isChecked) => menuSVGHelper('M7,9 L7,4 L17,4 L17,12 L12,12 L12,16 M12,19 L12,22', isChecked);
554
+ const lineHeightSVGElements = () => menuSVGHelper('M2,3 L22,3 M2,9 L22,9 M2,15 L22,15 M2,21 L22,21', false, 2);
555
+ const markdownSVGElements = (isChecked) => (
556
+ menuSVGHelper('M4,2 L20,2 L20,22 L4,22 Z', isChecked, 3, 'M7,7.5 L17,7.5 M7,12 L17,12 M7,16.5 L17,16.5')
557
+ );
558
+
559
+
560
+ // Menu SVG helper function
561
+ function menuSVGHelper(pathD, isChecked = false, strokeWidth = 3, pathD2 = null, iconSize = 48) {
562
+ const borderSize = (0.125 * iconSize).toFixed(3);
563
+ const innerSize = (0.75 * iconSize).toFixed(3);
564
+ const iconElems = [
549
565
  {
550
566
  'svg': 'path',
551
- 'attr': {
552
- 'd': 'M7,7.5 L17,7.5 M7,12 L17,12 M7,16.5 L17,16.5',
553
- 'stroke': 'black', 'stroke-width': '2', 'fill': 'none'
554
- }
567
+ 'attr': {'fill': 'none', 'stroke': (isChecked ? 'white' : 'black'), 'stroke-width': strokeWidth, 'd': pathD}
555
568
  }
556
- ]
557
- };
558
-
559
-
560
- // "help" SVG element model
561
- const helpSVGElements = {
562
- 'svg': 'svg',
563
- 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
564
- 'elem': {
565
- 'svg': 'path',
566
- 'attr': {'d': 'M7,9 L7,4 L17,4 L17,12 L12,12 L12,16 M12,19 L12,22', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
569
+ ];
570
+ const svg = {
571
+ 'svg': 'svg',
572
+ 'attr': {'width': iconSize, 'height': iconSize},
573
+ 'elem': [
574
+ {
575
+ 'svg': 'g',
576
+ 'attr': {'transform': `translate(${borderSize}, ${borderSize})`},
577
+ 'elem': {
578
+ 'svg': 'svg',
579
+ 'attr': {'width': innerSize, 'height': innerSize, 'viewBox': '0 0 24 24'},
580
+ 'elem': iconElems
581
+ }
582
+ }
583
+ ]
584
+ };
585
+ if (isChecked) {
586
+ svg.elem.unshift({
587
+ 'svg': 'rect',
588
+ 'attr': {'fill': 'black', 'stroke': 'none', 'width': iconSize, 'height': iconSize}
589
+ });
567
590
  }
568
- };
591
+ if (pathD2 !== null) {
592
+ iconElems.push({
593
+ 'svg': 'path',
594
+ 'attr': {'fill': 'none', 'stroke': (isChecked ? 'white' : 'black'), 'stroke-width': strokeWidth - 1, 'd': pathD2}
595
+ });
596
+ }
597
+ return svg;
598
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.8.2",
4
+ "version": "1.8.3",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -31,7 +31,7 @@
31
31
  "devDependencies": {
32
32
  "ava": "~4.3",
33
33
  "c8": "~7.12",
34
- "eslint": "~8.23",
34
+ "eslint": "~8.24",
35
35
  "jsdoc": "~3.6",
36
36
  "jsdom": "~20.0"
37
37
  }