markdown-up 1.8.9 → 1.8.10
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 +2 -2
- package/lib/data.js +44 -10
- package/lib/scriptLibrary.js +95 -33
- package/package.json +3 -3
package/lib/app.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
/** @module lib/app */
|
|
5
5
|
|
|
6
6
|
import {MarkdownScriptRuntime, markdownScriptCodeBlock} from './script.js';
|
|
7
|
-
import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
|
|
7
|
+
import {decodeQueryString, encodeQueryString, jsonStringifySortKeys} from 'schema-markdown/lib/encode.js';
|
|
8
8
|
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
9
9
|
import {evaluateExpression} from 'calc-script/lib/runtime.js';
|
|
10
10
|
import {markdownElementsAsync} from 'markdown-model/lib/elements.js';
|
|
@@ -131,7 +131,7 @@ export class MarkdownUp {
|
|
|
131
131
|
this.updateParams();
|
|
132
132
|
|
|
133
133
|
// Skip the render if the page params haven't changed
|
|
134
|
-
if (!forceRender && paramsPrev !== null &&
|
|
134
|
+
if (!forceRender && paramsPrev !== null && jsonStringifySortKeys(paramsPrev) === jsonStringifySortKeys(this.params)) {
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
137
|
} catch ({message}) {
|
package/lib/data.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
/** @module lib/data */
|
|
5
5
|
|
|
6
6
|
import {evaluateExpression} from 'calc-script/lib/runtime.js';
|
|
7
|
+
import {jsonStringifySortKeys} from 'schema-markdown/lib/encode.js';
|
|
7
8
|
import {parseExpression} from 'calc-script/lib/parser.js';
|
|
8
9
|
import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
|
|
9
10
|
import {validateType} from 'schema-markdown/lib/schema.js';
|
|
@@ -181,9 +182,10 @@ const rDatetime = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2
|
|
|
181
182
|
* @param {?string} [rightExpr = null] - The right join [expression]{@link https://craigahobbs.github.io/calc-script/language/#expressions}
|
|
182
183
|
* @param {boolean} [isLeftJoin = false] - If true, perform a left join (always include left row)
|
|
183
184
|
* @param {?Object} [variables = null] - Additional variables for expression evaluation
|
|
185
|
+
* @param {?Object} [options = null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
|
|
184
186
|
* @returns {Object[]} The joined data array
|
|
185
187
|
*/
|
|
186
|
-
export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null) {
|
|
188
|
+
export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null, options = null) {
|
|
187
189
|
// Compute the map of row field name to joined row field name
|
|
188
190
|
const leftNames = {};
|
|
189
191
|
for (const row of leftData) {
|
|
@@ -212,15 +214,25 @@ export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeft
|
|
|
212
214
|
}
|
|
213
215
|
}
|
|
214
216
|
|
|
217
|
+
// Create the evaluation options object
|
|
218
|
+
let evalOptions = options;
|
|
219
|
+
if (variables !== null) {
|
|
220
|
+
evalOptions = (options !== null ? {...options} : {});
|
|
221
|
+
if ('globals' in evalOptions) {
|
|
222
|
+
evalOptions.globals = {...evalOptions.globals, ...variables};
|
|
223
|
+
} else {
|
|
224
|
+
evalOptions.globals = variables;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
215
228
|
// Parse the left and right expressions
|
|
216
|
-
const evalOptions = (variables !== null ? {'globals': variables} : null);
|
|
217
229
|
const leftExpression = parseExpression(joinExpr);
|
|
218
230
|
const rightExpression = (rightExpr !== null ? parseExpression(rightExpr) : leftExpression);
|
|
219
231
|
|
|
220
232
|
// Bucket the right rows by the right expression value
|
|
221
233
|
const rightCategoryRows = {};
|
|
222
234
|
for (const rightRow of rightData) {
|
|
223
|
-
const categoryKey =
|
|
235
|
+
const categoryKey = jsonStringifySortKeys(evaluateExpression(rightExpression, evalOptions, rightRow));
|
|
224
236
|
if (!(categoryKey in rightCategoryRows)) {
|
|
225
237
|
rightCategoryRows[categoryKey] = [];
|
|
226
238
|
}
|
|
@@ -230,7 +242,7 @@ export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeft
|
|
|
230
242
|
// Join the left with the right
|
|
231
243
|
const data = [];
|
|
232
244
|
for (const leftRow of leftData) {
|
|
233
|
-
const categoryKey =
|
|
245
|
+
const categoryKey = jsonStringifySortKeys(evaluateExpression(leftExpression, evalOptions, leftRow));
|
|
234
246
|
if (categoryKey in rightCategoryRows) {
|
|
235
247
|
for (const rightRow of rightCategoryRows[categoryKey]) {
|
|
236
248
|
const joinRow = {...leftRow};
|
|
@@ -255,14 +267,25 @@ export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeft
|
|
|
255
267
|
* @param {string} fieldName - The calculated field name
|
|
256
268
|
* @param {string} expr - The calculated field expression
|
|
257
269
|
* @param {?Object} [variables = null] - Additional variables for expression evaluation
|
|
270
|
+
* @param {?Object} [options = null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
|
|
258
271
|
* @returns {Object[]} The updated data array
|
|
259
272
|
*/
|
|
260
|
-
export function addCalculatedField(data, fieldName, expr, variables = null) {
|
|
273
|
+
export function addCalculatedField(data, fieldName, expr, variables = null, options = null) {
|
|
261
274
|
// Parse the calculation expression
|
|
262
275
|
const calcExpr = parseExpression(expr);
|
|
263
276
|
|
|
277
|
+
// Create the evaluation options object
|
|
278
|
+
let evalOptions = options;
|
|
279
|
+
if (variables !== null) {
|
|
280
|
+
evalOptions = (options !== null ? {...options} : {});
|
|
281
|
+
if ('globals' in evalOptions) {
|
|
282
|
+
evalOptions.globals = {...evalOptions.globals, ...variables};
|
|
283
|
+
} else {
|
|
284
|
+
evalOptions.globals = variables;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
264
288
|
// Compute the calculated field for each row
|
|
265
|
-
const evalOptions = (variables !== null ? {'globals': variables} : null);
|
|
266
289
|
for (const row of data) {
|
|
267
290
|
row[fieldName] = evaluateExpression(calcExpr, evalOptions, row);
|
|
268
291
|
}
|
|
@@ -276,16 +299,27 @@ export function addCalculatedField(data, fieldName, expr, variables = null) {
|
|
|
276
299
|
* @param {Object[]} data - The data array
|
|
277
300
|
* @param {string} expr - The boolean filter [expression]{@link https://craigahobbs.github.io/calc-script/language/#expressions}
|
|
278
301
|
* @param {?Object} [variables = null] - Additional variables for expression evaluation
|
|
302
|
+
* @param {?Object} [options = null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
|
|
279
303
|
* @returns {Object[]} The filtered data array
|
|
280
304
|
*/
|
|
281
|
-
export function filterData(data, expr, variables = null) {
|
|
305
|
+
export function filterData(data, expr, variables = null, options = null) {
|
|
282
306
|
const result = [];
|
|
283
307
|
|
|
284
308
|
// Parse the filter expression
|
|
285
309
|
const filterExpr = parseExpression(expr);
|
|
286
310
|
|
|
311
|
+
// Create the evaluation options object
|
|
312
|
+
let evalOptions = options;
|
|
313
|
+
if (variables !== null) {
|
|
314
|
+
evalOptions = (options !== null ? {...options} : {});
|
|
315
|
+
if ('globals' in evalOptions) {
|
|
316
|
+
evalOptions.globals = {...evalOptions.globals, ...variables};
|
|
317
|
+
} else {
|
|
318
|
+
evalOptions.globals = variables;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
287
322
|
// Filter the data
|
|
288
|
-
const evalOptions = (variables !== null ? {'globals': variables} : null);
|
|
289
323
|
for (const row of data) {
|
|
290
324
|
if (evaluateExpression(filterExpr, evalOptions, row)) {
|
|
291
325
|
result.push(row);
|
|
@@ -377,7 +411,7 @@ export function aggregateData(data, aggregation) {
|
|
|
377
411
|
|
|
378
412
|
// Get or create the aggregate row
|
|
379
413
|
let aggregateRow;
|
|
380
|
-
const rowKey = (categoryValues !== null ?
|
|
414
|
+
const rowKey = (categoryValues !== null ? jsonStringifySortKeys(categoryValues) : '');
|
|
381
415
|
if (rowKey in categoryRows) {
|
|
382
416
|
aggregateRow = categoryRows[rowKey];
|
|
383
417
|
} else {
|
|
@@ -461,7 +495,7 @@ export function topData(data, count, categoryFields = null) {
|
|
|
461
495
|
const categoryOrder = [];
|
|
462
496
|
for (const row of data) {
|
|
463
497
|
const categoryKey = categoryFields === null ? ''
|
|
464
|
-
:
|
|
498
|
+
: jsonStringifySortKeys(categoryFields.map((field) => (field in row ? row[field] : null)));
|
|
465
499
|
if (!(categoryKey in categoryRows)) {
|
|
466
500
|
categoryRows[categoryKey] = [];
|
|
467
501
|
categoryOrder.push(categoryKey);
|
package/lib/scriptLibrary.js
CHANGED
|
@@ -45,7 +45,9 @@ export const markdownScriptFunctions = {
|
|
|
45
45
|
// $arg expr: The calculated field expression
|
|
46
46
|
// $arg variables: Optional (default is null). A variables object the expression evaluation.
|
|
47
47
|
// $return: The updated data array
|
|
48
|
-
'dataCalculatedField': ([data, fieldName, expr, variables = null]) =>
|
|
48
|
+
'dataCalculatedField': ([data, fieldName, expr, variables = null], options) => (
|
|
49
|
+
addCalculatedField(data, fieldName, expr, variables, options)
|
|
50
|
+
),
|
|
49
51
|
|
|
50
52
|
// $function: dataFilter
|
|
51
53
|
// $group: Data
|
|
@@ -54,7 +56,7 @@ export const markdownScriptFunctions = {
|
|
|
54
56
|
// $arg expr: The filter expression
|
|
55
57
|
// $arg variables: Optional (default is null). A variables object the expression evaluation.
|
|
56
58
|
// $return: The filtered data array
|
|
57
|
-
'dataFilter': ([data, expr, variables = null]) => filterData(data, expr, variables),
|
|
59
|
+
'dataFilter': ([data, expr, variables = null], options) => filterData(data, expr, variables, options),
|
|
58
60
|
|
|
59
61
|
// $function: dataJoin
|
|
60
62
|
// $group: Data
|
|
@@ -67,8 +69,8 @@ export const markdownScriptFunctions = {
|
|
|
67
69
|
// $arg isLeftJoin: Optional (default is false). If true, perform a left join (always include left row).
|
|
68
70
|
// $arg variables: Optional (default is null). A variables object for join expression evaluation.
|
|
69
71
|
// $return: The joined data array
|
|
70
|
-
'dataJoin': ([leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null]) => (
|
|
71
|
-
joinData(leftData, rightData, joinExpr, rightExpr, isLeftJoin, variables)
|
|
72
|
+
'dataJoin': ([leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null], options) => (
|
|
73
|
+
joinData(leftData, rightData, joinExpr, rightExpr, isLeftJoin, variables, options)
|
|
72
74
|
),
|
|
73
75
|
|
|
74
76
|
// $function: dataLineChart
|
|
@@ -223,7 +225,13 @@ export const markdownScriptFunctions = {
|
|
|
223
225
|
const {runtime} = options;
|
|
224
226
|
runtime.windowResize = async () => {
|
|
225
227
|
options.statementCount = 0;
|
|
226
|
-
|
|
228
|
+
try {
|
|
229
|
+
await callback([], options);
|
|
230
|
+
} catch ({message}) {
|
|
231
|
+
if ('logFn' in options) {
|
|
232
|
+
options.logFn(`MarkdownUp: Error executing setWindowResize callback: ${message}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
227
235
|
options.runtimeUpdateFn();
|
|
228
236
|
};
|
|
229
237
|
},
|
|
@@ -238,7 +246,13 @@ export const markdownScriptFunctions = {
|
|
|
238
246
|
runtime.windowTimeout = [
|
|
239
247
|
async () => {
|
|
240
248
|
options.statementCount = 0;
|
|
241
|
-
|
|
249
|
+
try {
|
|
250
|
+
await callback([], options);
|
|
251
|
+
} catch ({message}) {
|
|
252
|
+
if ('logFn' in options) {
|
|
253
|
+
options.logFn(`MarkdownUp: Error executing setWindowTimeout callback: ${message}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
242
256
|
options.runtimeUpdateFn();
|
|
243
257
|
},
|
|
244
258
|
delay
|
|
@@ -389,17 +403,40 @@ export const markdownScriptFunctions = {
|
|
|
389
403
|
const {runtime} = options;
|
|
390
404
|
const svg = runtime.setDrawing();
|
|
391
405
|
const clickElement = (svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1]);
|
|
392
|
-
clickElement.attr.style = 'cursor: pointer;
|
|
406
|
+
clickElement.attr.style = 'cursor: pointer;';
|
|
393
407
|
clickElement.callback = (element) => {
|
|
394
408
|
element.addEventListener('click', async (event) => {
|
|
395
409
|
const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
|
|
396
410
|
options.statementCount = 0;
|
|
397
|
-
|
|
411
|
+
try {
|
|
412
|
+
await callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
|
|
413
|
+
} catch ({message}) {
|
|
414
|
+
if ('logFn' in options) {
|
|
415
|
+
options.logFn(`MarkdownUp: Error executing drawOnClick callback: ${message}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
398
418
|
options.runtimeUpdateFn();
|
|
399
419
|
});
|
|
400
420
|
};
|
|
401
421
|
},
|
|
402
422
|
|
|
423
|
+
// $function: drawPathRect
|
|
424
|
+
// $group: Drawing
|
|
425
|
+
// $doc: Draw a rectangle as a path
|
|
426
|
+
// $arg x: The x-coordinate of the top-left of the rectangle
|
|
427
|
+
// $arg y: The y-coordinate of the top-left of the rectangle
|
|
428
|
+
// $arg width: The width of the rectangle
|
|
429
|
+
// $arg height: The height of the rectangle
|
|
430
|
+
'drawPathRect': ([x, y, width, height], options) => {
|
|
431
|
+
const {runtime} = options;
|
|
432
|
+
runtime.setDrawingPath();
|
|
433
|
+
runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
|
|
434
|
+
runtime.drawingPath.push(`H ${(x + width).toFixed(svgPrecision)}`);
|
|
435
|
+
runtime.drawingPath.push(`V ${(y + height).toFixed(svgPrecision)}`);
|
|
436
|
+
runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
|
|
437
|
+
runtime.drawingPath.push('Z');
|
|
438
|
+
},
|
|
439
|
+
|
|
403
440
|
// $function: drawRect
|
|
404
441
|
// $group: Drawing
|
|
405
442
|
// $doc: Draw a rectangle
|
|
@@ -561,6 +598,10 @@ export const markdownScriptFunctions = {
|
|
|
561
598
|
// $function: elementModelRender
|
|
562
599
|
// $group: Element Model
|
|
563
600
|
// $doc: Render an [element model](https://github.com/craigahobbs/element-model#readme)
|
|
601
|
+
// $doc:
|
|
602
|
+
// $doc: **Note:** Element model "callback" members are a map of event name (e.g., "click") to
|
|
603
|
+
// $doc: event callback function. Data can be passed to the callback functions by setting a
|
|
604
|
+
// $doc: "callbackData" member.
|
|
564
605
|
// $arg element: The [element model](https://github.com/craigahobbs/element-model#readme)
|
|
565
606
|
'elementModelRender': ([elements], options) => {
|
|
566
607
|
const {runtime} = options;
|
|
@@ -699,38 +740,59 @@ export const markdownScriptFunctions = {
|
|
|
699
740
|
|
|
700
741
|
// Helper function for elementModelRender function
|
|
701
742
|
function elementModelWrapCallbacks(elements, options) {
|
|
743
|
+
// Ignore non-objects
|
|
744
|
+
if (elements === null || typeof elements !== 'object') {
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Array?
|
|
702
749
|
if (Array.isArray(elements)) {
|
|
703
750
|
for (const childElements of elements) {
|
|
704
751
|
elementModelWrapCallbacks(childElements, options);
|
|
705
752
|
}
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// Wrap child elements
|
|
757
|
+
const elementsElem = elements.elem ?? null;
|
|
758
|
+
if (elementsElem !== null) {
|
|
759
|
+
elementModelWrapCallbacks(elementsElem, options);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// Element callback attribute must be map of event => callback
|
|
763
|
+
const elementEvents = elements.callback ?? null;
|
|
764
|
+
if (elementEvents !== null && typeof elementEvents === 'object') {
|
|
765
|
+
// Get the callback data, if any
|
|
766
|
+
let callbackData = null;
|
|
767
|
+
if ('callbackData' in elements) {
|
|
768
|
+
({callbackData} = elements);
|
|
769
|
+
delete elements.callbackData;
|
|
709
770
|
}
|
|
710
771
|
|
|
711
|
-
//
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
// Determine the event callback args
|
|
721
|
-
const eventArgs = [];
|
|
722
|
-
if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
|
|
723
|
-
eventArgs.push(event.keyCode);
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
// Call the event handler
|
|
727
|
-
options.statementCount = 0;
|
|
728
|
-
await elementEventCallback(eventArgs, options);
|
|
729
|
-
options.runtimeUpdateFn();
|
|
730
|
-
});
|
|
772
|
+
// Wrap the event handler function
|
|
773
|
+
elements.callback = (element) => {
|
|
774
|
+
// On element render, add a listener for each event
|
|
775
|
+
for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
|
|
776
|
+
element.addEventListener(elementEvent, async (event) => {
|
|
777
|
+
// Determine the event callback args
|
|
778
|
+
const eventArgs = [];
|
|
779
|
+
if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
|
|
780
|
+
eventArgs.push(event.keyCode);
|
|
731
781
|
}
|
|
732
|
-
|
|
782
|
+
eventArgs.push(callbackData);
|
|
783
|
+
|
|
784
|
+
// Call the event handler
|
|
785
|
+
options.statementCount = 0;
|
|
786
|
+
try {
|
|
787
|
+
await elementEventCallback(eventArgs, options);
|
|
788
|
+
} catch ({message}) {
|
|
789
|
+
if ('logFn' in options) {
|
|
790
|
+
options.logFn(`MarkdownUp: Error executing elementModelRender callback: ${message}`);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
options.runtimeUpdateFn();
|
|
794
|
+
});
|
|
733
795
|
}
|
|
734
|
-
}
|
|
796
|
+
};
|
|
735
797
|
}
|
|
736
798
|
}
|
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.10",
|
|
5
5
|
"description": "MarkdownUp is a Markdown viewer",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"markdown",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"ava": "~5.1",
|
|
33
33
|
"c8": "~7.12",
|
|
34
|
-
"eslint": "~8.
|
|
34
|
+
"eslint": "~8.32",
|
|
35
35
|
"jsdoc": "~4.0",
|
|
36
|
-
"jsdom": "~
|
|
36
|
+
"jsdom": "~21.1"
|
|
37
37
|
}
|
|
38
38
|
}
|