markdown-up 1.8.1 → 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.
- package/README.md +2 -1
- package/lib/app.js +183 -153
- package/lib/data.js +35 -0
- package/lib/scriptLibrary.js +12 -1
- 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
|
-
#
|
|
34
|
-
optional
|
|
33
|
+
# Variable expressions
|
|
34
|
+
optional string{} var
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
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
|
|
44
|
+
optional float(>= 1, <= 2) lineHeight
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
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
|
|
53
|
-
|
|
60
|
+
# A MarkdownUp application view
|
|
61
|
+
enum MarkdownUpView
|
|
54
62
|
|
|
55
63
|
# Display the hash parameters documentation
|
|
56
|
-
|
|
64
|
+
help
|
|
57
65
|
|
|
58
66
|
# Show the resource's Markdown text
|
|
59
|
-
|
|
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,
|
|
93
|
+
constructor(window, options = null) {
|
|
86
94
|
this.window = window;
|
|
87
95
|
this.params = null;
|
|
88
|
-
this.
|
|
89
|
-
this.
|
|
90
|
-
this.
|
|
91
|
-
this.
|
|
92
|
-
this.
|
|
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 =
|
|
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 =
|
|
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
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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 (
|
|
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 (
|
|
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':
|
|
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
|
-
|
|
329
|
-
|
|
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
|
|
339
|
-
|
|
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.
|
|
460
|
+
!('menu' in this.paramsSession) ? null : {
|
|
426
461
|
'html': 'div',
|
|
427
462
|
'attr': {'class': 'menu'},
|
|
428
463
|
'elem': [
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
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
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
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
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
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
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
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
|
-
//
|
|
503
|
-
const
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
const
|
|
520
|
-
|
|
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
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
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/lib/data.js
CHANGED
|
@@ -447,6 +447,41 @@ export function sortData(data, sorts) {
|
|
|
447
447
|
}
|
|
448
448
|
|
|
449
449
|
|
|
450
|
+
/**
|
|
451
|
+
* Top data rows
|
|
452
|
+
*
|
|
453
|
+
* @param {Object[]} data - The data array
|
|
454
|
+
* @param {number} count - The number of rows to keep
|
|
455
|
+
* @param {?string[]} [categoryFields = null] - The category fields
|
|
456
|
+
* @returns {Object[]} The top data array
|
|
457
|
+
*/
|
|
458
|
+
export function topData(data, count, categoryFields = null) {
|
|
459
|
+
// Bucket rows by category
|
|
460
|
+
const categoryRows = {};
|
|
461
|
+
const categoryOrder = [];
|
|
462
|
+
for (const row of data) {
|
|
463
|
+
const categoryKey = categoryFields === null ? ''
|
|
464
|
+
: JSON.stringify(categoryFields.map((field) => (field in row ? row[field] : null)));
|
|
465
|
+
if (!(categoryKey in categoryRows)) {
|
|
466
|
+
categoryRows[categoryKey] = [];
|
|
467
|
+
categoryOrder.push(categoryKey);
|
|
468
|
+
}
|
|
469
|
+
categoryRows[categoryKey].push(row);
|
|
470
|
+
}
|
|
471
|
+
// Take only the top rows
|
|
472
|
+
const dataTop = [];
|
|
473
|
+
const topCount = count;
|
|
474
|
+
for (const categoryKey of categoryOrder) {
|
|
475
|
+
const categoryKeyRows = categoryRows[categoryKey];
|
|
476
|
+
const categoryKeyLength = categoryKeyRows.length;
|
|
477
|
+
for (let ixRow = 0; ixRow < topCount && ixRow < categoryKeyLength; ixRow++) {
|
|
478
|
+
dataTop.push(categoryKeyRows[ixRow]);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return dataTop;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
|
|
450
485
|
// Helper function to format labels
|
|
451
486
|
export function formatValue(value, precision = null, datetime = null) {
|
|
452
487
|
if (value instanceof Date) {
|
package/lib/scriptLibrary.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// Licensed under the MIT License
|
|
2
2
|
// https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
addCalculatedField, aggregateData, filterData, joinData, parseCSV, sortData, topData, validateAggregation, validateData
|
|
6
|
+
} from './data.js';
|
|
5
7
|
import {dataTableElements, validateDataTable} from './dataTable.js';
|
|
6
8
|
import {escapeMarkdownText, markdownHeaderId} from 'markdown-model/lib/elements.js';
|
|
7
9
|
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
@@ -121,6 +123,15 @@ export const markdownScriptFunctions = {
|
|
|
121
123
|
runtime.addElements(dataTableElements(data, dataTableValidated, options));
|
|
122
124
|
},
|
|
123
125
|
|
|
126
|
+
// $function: dataTop
|
|
127
|
+
// $group: Data
|
|
128
|
+
// $doc: Keep the top rows for each category
|
|
129
|
+
// $arg data: The data array
|
|
130
|
+
// $arg count: The number of rows to keep
|
|
131
|
+
// $arg categoryFields: Optional (default is null). The category fields.
|
|
132
|
+
// $return: The top data array
|
|
133
|
+
'dataTop': ([data, count, categoryFields = null]) => topData(data, count, categoryFields),
|
|
134
|
+
|
|
124
135
|
// $function: dataValidate
|
|
125
136
|
// $group: Data
|
|
126
137
|
// $doc: Validate a data array
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "markdown-up",
|
|
4
|
-
"version": "1.8.
|
|
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.
|
|
34
|
+
"eslint": "~8.24",
|
|
35
35
|
"jsdoc": "~3.6",
|
|
36
36
|
"jsdom": "~20.0"
|
|
37
37
|
}
|