markdown-up 1.8.2 → 1.8.4

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 +199 -160
  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,44 @@ 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
39
+
40
+ # If set, dark mode is enabled
41
+ optional int(== 1) darkMode
38
42
 
39
43
  # The font size
40
44
  optional int(>= 8, <= 18) fontSize
41
45
 
42
46
  # The line height
43
- optional int(>= 1, <= 2) lineHeight
47
+ optional float(>= 1, <= 2) lineHeight
44
48
 
45
- # Variable expressions
46
- optional string{} var
47
49
 
48
- # If true, enable debug behavior
50
+ # The MarkdownUp session storage JSON schema
51
+ struct MarkdownUpSession
52
+
53
+ # Optional command
54
+ optional MarkdownUpView view
55
+
56
+ # If set, the menu is expanded (default is collapsed)
57
+ optional int(== 1) menu
58
+
59
+ # If set, enable debug behavior
49
60
  optional int(== 1) debug
50
61
 
51
62
 
52
- # A MarkdownUp application command
53
- union Command
63
+ # A MarkdownUp application view
64
+ enum MarkdownUpView
54
65
 
55
66
  # Display the hash parameters documentation
56
- int(== 1) help
67
+ help
57
68
 
58
69
  # Show the resource's Markdown text
59
- int(== 1) markdown
70
+ markdown
60
71
  `);
61
72
 
62
73
 
@@ -64,6 +75,7 @@ union Command
64
75
  * The MarkdownUp application options
65
76
  *
66
77
  * @typedef {Object} MarkdownUpOptions
78
+ * @property {boolean} [darkMode = false] - If true, use dark mode by default
67
79
  * @property {number} [fontSize = 12] - The font size, in points
68
80
  * @property {number} [lineHeight = 1.2] - The line height, in em
69
81
  * @property {?string} [markdownText = null] - The default Markdown text
@@ -82,14 +94,17 @@ export class MarkdownUp {
82
94
  * @param {Object} window - The web browser window object
83
95
  * @param {Object} [options] - The [application options]{@link module:lib/app~MarkdownUpOptions}
84
96
  */
85
- constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
97
+ constructor(window, options = null) {
86
98
  this.window = window;
87
99
  this.params = null;
88
- this.fontSize = fontSize;
89
- this.lineHeight = lineHeight;
90
- this.markdownText = markdownText;
91
- this.menu = menu;
92
- this.url = url;
100
+ this.paramsLocal = null;
101
+ this.paramsSession = null;
102
+ this.darkMode = (options !== null ? options.darkMode : null) ?? false;
103
+ this.fontSize = (options !== null ? options.fontSize : null) ?? 12;
104
+ this.lineHeight = (options !== null ? options.lineHeight : null) ?? 1.2;
105
+ this.markdownText = (options !== null ? options.markdownText : null) ?? null;
106
+ this.menu = (options !== null ? options.menu : null) ?? true;
107
+ this.url = (options !== null ? options.url : null) ?? 'README.md';
93
108
  this.runtimeWindowResize = null;
94
109
  this.runtimeTimeoutId = null;
95
110
  }
@@ -104,7 +119,7 @@ export class MarkdownUp {
104
119
  }
105
120
 
106
121
 
107
- async render() {
122
+ async render(forceRender = false) {
108
123
  // Parse the hash parameters and render the application element model
109
124
  let result;
110
125
  let isError = false;
@@ -114,7 +129,7 @@ export class MarkdownUp {
114
129
  this.updateParams();
115
130
 
116
131
  // Skip the render if the page params haven't changed
117
- if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
132
+ if (!forceRender && paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
118
133
  return;
119
134
  }
120
135
  } catch ({message}) {
@@ -131,12 +146,16 @@ export class MarkdownUp {
131
146
  result = await this.main();
132
147
  }
133
148
 
149
+ // Set the colors
150
+ const isDarkMode = this.paramsLocal.darkMode ?? this.darkMode;
151
+ this.window.document.documentElement.style.setProperty('--markdown-model-dark-mode', isDarkMode ? '1' : '0');
152
+
134
153
  // Set the font size
135
- const fontSize = (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
154
+ const fontSize = this.paramsLocal.fontSize ?? this.fontSize;
136
155
  this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
137
156
 
138
157
  // Set the line height
139
- const lineHeight = (this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight);
158
+ const lineHeight = this.paramsLocal.lineHeight ?? this.lineHeight;
140
159
  this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
141
160
 
142
161
  // Set the window title
@@ -179,15 +198,38 @@ export class MarkdownUp {
179
198
  }
180
199
 
181
200
 
182
- updateParams(paramString = null) {
201
+ updateParams(paramString = null, localJSONString = null, sessionJSONString = null) {
183
202
  // Clear, then validate the hash parameters (may throw)
184
203
  this.params = null;
204
+ this.paramsLocal = {};
205
+ this.paramsSession = {};
206
+
207
+ // Decode and validate the hash parameters
208
+ this.params = validateType(
209
+ markdownUpTypes,
210
+ 'MarkdownUp',
211
+ decodeQueryString(paramString ?? this.window.location.hash.slice(1))
212
+ );
213
+
214
+ // Decode and validate the local storage paramters
215
+ const localJSON = localJSONString ?? this.window.localStorage.getItem('MarkdownUp');
216
+ if (localJSON !== null) {
217
+ try {
218
+ this.paramsLocal = validateType(markdownUpTypes, 'MarkdownUpLocal', JSON.parse(localJSON));
219
+ } catch {
220
+ // Do nothing
221
+ }
222
+ }
185
223
 
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);
224
+ // Decode and validate the session storage parameters
225
+ const sessionJSON = sessionJSONString ?? this.window.sessionStorage.getItem('MarkdownUp');
226
+ if (sessionJSON !== null) {
227
+ try {
228
+ this.paramsSession = validateType(markdownUpTypes, 'MarkdownUpSession', JSON.parse(sessionJSON));
229
+ } catch {
230
+ // Do nothing
231
+ }
232
+ }
191
233
  }
192
234
 
193
235
 
@@ -220,7 +262,7 @@ export class MarkdownUp {
220
262
 
221
263
  async main() {
222
264
  // Help?
223
- if ('cmd' in this.params && 'help' in this.params.cmd) {
265
+ if (this.paramsSession.view === 'help') {
224
266
  return {
225
267
  'title': 'MarkdownUp',
226
268
  'elements': [
@@ -257,7 +299,7 @@ export class MarkdownUp {
257
299
  const markdownTitle = getMarkdownTitle(markdownModel);
258
300
 
259
301
  // Display the Markdown?
260
- if ('cmd' in this.params && 'markdown' in this.params.cmd) {
302
+ if (this.paramsSession.view === 'markdown') {
261
303
  return {
262
304
  'title': markdownTitle,
263
305
  'elements': [
@@ -318,25 +360,33 @@ export class MarkdownUp {
318
360
  const scriptOptions = {
319
361
  // eslint-disable-next-line require-await
320
362
  'fetchFn': async (fetchURL, options) => this.window.fetch(fetchURL, options),
321
- 'fontSize': (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize),
363
+ 'fontSize': this.paramsLocal.fontSize ?? this.fontSize,
322
364
  'params': this.params,
323
365
  'urlFn': (url) => this.modifyURL(url, resourceURL),
324
366
  'window': this.window
325
367
  };
326
368
 
327
369
  // Add log function, if debugging
328
- if ('debug' in this.params) {
329
- scriptOptions.logFn = (text) => {
370
+ let logFn = null;
371
+ if ('debug' in this.paramsSession) {
372
+ logFn = (text) => {
330
373
  this.window.console.log(text);
331
374
  };
375
+ scriptOptions.logFn = logFn;
332
376
  }
333
377
 
334
378
  // Add hash parameter variables, if any
335
379
  if ('var' in this.params) {
336
380
  scriptOptions.variables = {};
337
381
  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);
382
+ const varExprStr = this.params.var[varName];
383
+ try {
384
+ scriptOptions.variables[varName] = evaluateExpression(parseExpression(varExprStr), scriptOptions.variables);
385
+ } catch ({message}) {
386
+ if (logFn !== null) {
387
+ logFn(`Error evaluating variable "${varName}" expression "${varExprStr}": ${message}`);
388
+ }
389
+ }
340
390
  }
341
391
  }
342
392
 
@@ -412,80 +462,138 @@ export class MarkdownUp {
412
462
  {
413
463
  'html': 'div',
414
464
  '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
- ]
465
+ 'elem': this.menuValueToggle('menu', {
466
+ 'size': 32,
467
+ 'noCheck': true,
468
+ 'path': 'M3,5 L21,5 M3,12 L21,12 M3,19 L21,19'
469
+ })
422
470
  },
423
471
 
424
472
  // Popup menu
425
- !('menu' in this.params) ? null : {
473
+ !('menu' in this.paramsSession) ? null : {
426
474
  'html': 'div',
427
475
  'attr': {'class': 'menu'},
428
476
  '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
- }
477
+ this.menuViewToggle('markdown', {
478
+ 'path': 'M4,2 L20,2 L20,22 L4,22 Z',
479
+ 'path2': 'M7,7.5 L17,7.5 M7,12 L17,12 M7,16.5 L17,16.5'
480
+ }),
481
+ this.menuValueToggle('darkMode', {
482
+ 'path': 'M16,3 A10,10,0,1,1,3,18 A14,14,0,0,0,17,3'
483
+ }, true),
484
+ this.menuValueCycle('fontSize', this.fontSize, 2, {
485
+ 'path': 'M4,22 L10,2 L14,2 L20,22 M6,14 L18,14',
486
+ 'strokeWidth': 4
487
+ }),
488
+ this.menuValueCycle('lineHeight', this.lineHeight, 0.2, {
489
+ 'path2': 'M2,3 L22,3 M2,9 L22,9 M2,15 L22,15 M2,21 L22,21'
490
+ }),
491
+ this.menuValueToggle('debug', {
492
+ 'path': '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 ' +
493
+ 'M15,9 L20,6 M15,12 L21,12 M15,15 L20,18'
494
+ }),
495
+ this.menuViewToggle('help', {
496
+ 'path': 'M7,9 L7,4 L17,4 L17,12 L12,12 L12,16 M12,19 L12,22'
497
+ })
449
498
  ]
450
499
  }
451
500
  ];
452
501
  }
453
502
 
454
503
 
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)}`;
504
+ menuValueToggle(valueName, icon, isLocal = false) {
505
+ const params = (isLocal ? this.paramsLocal : this.paramsSession);
506
+ icon.checked = valueName in params;
507
+ return this.menuButton(
508
+ () => {
509
+ if (valueName in params) {
510
+ delete params[valueName];
511
+ } else {
512
+ params[valueName] = 1;
513
+ }
514
+ const storage = (isLocal ? this.window.localStorage : this.window.sessionStorage);
515
+ storage.setItem('MarkdownUp', JSON.stringify(params));
516
+ this.render(true);
517
+ },
518
+ icon
519
+ );
466
520
  }
467
521
 
468
522
 
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)}`;
523
+ menuViewToggle(viewValue, icon) {
524
+ icon.checked = this.paramsSession.view === viewValue;
525
+ return this.menuButton(
526
+ () => {
527
+ if (this.paramsSession.view === viewValue) {
528
+ delete this.paramsSession.view;
529
+ } else {
530
+ this.paramsSession.view = viewValue;
531
+ }
532
+ this.window.sessionStorage.setItem('MarkdownUp', JSON.stringify(this.paramsSession));
533
+ this.render(true);
534
+ },
535
+ icon
536
+ );
478
537
  }
479
538
 
480
539
 
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)}`;
540
+ menuValueCycle(valueName, valueDefault, valueDelta, icon) {
541
+ return this.menuButton(
542
+ () => {
543
+ const {attr} = markdownUpTypes.MarkdownUpLocal.struct.members.find((member) => member.name === valueName);
544
+ let valueNew = (this.paramsLocal[valueName] ?? valueDefault) + valueDelta;
545
+ if (valueNew > attr.lte) {
546
+ valueNew = attr.gte;
547
+ }
548
+ this.paramsLocal[valueName] = valueNew;
549
+ this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
550
+ this.render(true);
551
+ },
552
+ icon
553
+ );
554
+ }
555
+
556
+
557
+ menuButton(onClick, {checked = false, noCheck = false, path = null, path2 = null, strokeWidth = 3, size = 48}) {
558
+ const isDarkMode = this.paramsLocal.darkMode ?? this.darkMode;
559
+ const stroke = (isDarkMode ? (checked && !noCheck ? 'black' : 'white') : (checked && !noCheck ? 'white' : 'black'));
560
+ const borderSize = (0.125 * size).toFixed(3);
561
+ const innerSize = (0.75 * size).toFixed(3);
562
+ return {
563
+ 'html': 'div',
564
+ 'attr': {'style': 'cursor: pointer; user-select: none;'},
565
+ 'elem': {
566
+ 'svg': 'svg',
567
+ 'attr': {'width': size, 'height': size},
568
+ 'elem': [
569
+ !checked || noCheck ? null : {
570
+ 'svg': 'rect',
571
+ 'attr': {'fill': (isDarkMode ? 'white' : 'black'), 'stroke': 'none', 'width': size, 'height': size}
572
+ },
573
+ {
574
+ 'svg': 'g',
575
+ 'attr': {'transform': `translate(${borderSize}, ${borderSize})`},
576
+ 'elem': {
577
+ 'svg': 'svg',
578
+ 'attr': {'width': innerSize, 'height': innerSize, 'viewBox': '0 0 24 24'},
579
+ 'elem': [
580
+ path === null ? null : {
581
+ 'svg': 'path',
582
+ 'attr': {'fill': 'none', 'stroke': stroke, 'stroke-width': strokeWidth, 'd': path}
583
+ },
584
+ path2 === null ? null : {
585
+ 'svg': 'path',
586
+ 'attr': {'fill': 'none', 'stroke': stroke, 'stroke-width': strokeWidth - 1, 'd': path2}
587
+ }
588
+ ]
589
+ }
590
+ }
591
+ ]
592
+ },
593
+ 'callback': (element) => {
594
+ element.addEventListener('click', onClick);
595
+ }
596
+ };
489
597
  }
490
598
  }
491
599
 
@@ -497,72 +605,3 @@ function getHashID(url) {
497
605
  }
498
606
 
499
607
  const rHashId = /[#&]([^=]+)$/;
500
-
501
-
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
- },
549
- {
550
- '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
- }
555
- }
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'}
567
- }
568
- };
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.4",
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
  }