markdown-up 1.8.9 → 1.8.11

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
@@ -81,9 +81,6 @@ helloCount = if(vCount != null, vCount, 3)
81
81
  width = 200
82
82
  height = 60
83
83
  borderSize = 5
84
- borderColor = 'red'
85
- fillColor = 'blue'
86
- textColor = 'white'
87
84
 
88
85
  # Render the more/less menu
89
86
  markdownPrint( \
@@ -94,20 +91,20 @@ markdownPrint( \
94
91
 
95
92
  # Render many hellos
96
93
  ixHello = 0
97
- helloLoop:
94
+ while ixHello < helloCount do
98
95
  # Render the hello title
99
96
  helloTitle = 'Hello #' + (ixHello + 1)
100
97
  markdownPrint('', '## ' + helloTitle)
101
98
 
102
99
  # Render the hello drawing
103
100
  setDrawingSize(width, height)
104
- drawStyle(borderColor, borderSize, fillColor)
101
+ drawStyle('red', borderSize, 'blue')
105
102
  drawRect(0.5 * borderSize, 0.5 * borderSize, width - borderSize, height - borderSize)
106
- drawTextStyle(0.67 * height, textColor, true)
103
+ drawTextStyle(0.67 * height, 'white', true)
107
104
  drawText(helloTitle, 0.5 * width, 0.55 * height)
108
105
 
109
106
  ixHello = ixHello + 1
110
- jumpif (ixHello < helloCount) helloLoop
107
+ endwhile
111
108
  ~~~
112
109
  ```
113
110
 
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 && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
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 = JSON.stringify(evaluateExpression(rightExpression, evalOptions, rightRow));
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 = JSON.stringify(evaluateExpression(leftExpression, evalOptions, leftRow));
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 ? JSON.stringify(categoryValues) : '');
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
- : JSON.stringify(categoryFields.map((field) => (field in row ? row[field] : null)));
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/script.js CHANGED
@@ -65,10 +65,11 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
65
65
  // Run the calc-script linter?
66
66
  if ('logFn' in options) {
67
67
  const warnings = lintScript(script);
68
+ const warningPrefix = `MarkdownUp: Script static analysis...`;
68
69
  if (warnings.length === 0) {
69
- options.logFn(`MarkdownUp: Script static analysis... OK`);
70
+ options.logFn(`${warningPrefix} OK`);
70
71
  } else {
71
- options.logFn(`MarkdownUp: Script static analysis ... ${warnings.length} warning${warnings.length > 1 ? 's' : ''}:`);
72
+ options.logFn(`${warningPrefix} ${warnings.length} warning${warnings.length > 1 ? 's' : ''}:`);
72
73
  for (const warning of warnings) {
73
74
  options.logFn(`MarkdownUp: ${warning}`);
74
75
  }
@@ -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]) => addCalculatedField(data, fieldName, expr, variables),
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
- await callback([], options);
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
- await callback([], options);
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; user-select: none;';
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
- await callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
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,61 @@ 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
- } else if (elements !== null && typeof elements === 'object') {
707
- if ('elem' in elements) {
708
- elementModelWrapCallbacks(elements.elem, options);
709
- }
753
+ return;
754
+ }
710
755
 
711
- // Element callback attribute must be map of event => callback
712
- if ('callback' in elements) {
713
- const elementEvents = elements.callback;
714
- if (elementEvents !== null && typeof elementEvents === 'object') {
715
- // Wrap the event handler function
716
- elements.callback = (element) => {
717
- // On element render, add a listener for each event
718
- for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
719
- element.addEventListener(elementEvent, async (event) => {
720
- // Determine the event callback args
721
- const eventArgs = [];
722
- if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
723
- eventArgs.push(event.keyCode);
724
- }
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
+ if ('callback' in elements) {
764
+ // Get the callback data, if any
765
+ let callbackData = null;
766
+ if ('callbackData' in elements) {
767
+ ({callbackData} = elements);
768
+ delete elements.callbackData;
769
+ }
725
770
 
726
- // Call the event handler
727
- options.statementCount = 0;
771
+ // Wrap the event handler function
772
+ const elementEvents = elements.callback;
773
+ if (elementEvents !== null) {
774
+ elements.callback = (element) => {
775
+ // On element render, add a listener for each event
776
+ for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
777
+ element.addEventListener(elementEvent, async (event) => {
778
+ // Determine the event callback args
779
+ const eventArgs = [];
780
+ if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
781
+ eventArgs.push(event.keyCode);
782
+ }
783
+ eventArgs.push(callbackData);
784
+
785
+ // Call the event handler
786
+ options.statementCount = 0;
787
+ try {
728
788
  await elementEventCallback(eventArgs, options);
729
- options.runtimeUpdateFn();
730
- });
731
- }
732
- };
733
- }
789
+ } catch ({message}) {
790
+ if ('logFn' in options) {
791
+ options.logFn(`MarkdownUp: Error executing elementModelRender callback: ${message}`);
792
+ }
793
+ }
794
+ options.runtimeUpdateFn();
795
+ });
796
+ }
797
+ };
734
798
  }
735
799
  }
736
800
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.8.9",
4
+ "version": "1.8.11",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -22,17 +22,17 @@
22
22
  "test": "ava -v test/"
23
23
  },
24
24
  "dependencies": {
25
- "calc-script": "~1.3",
25
+ "calc-script": "~1.4",
26
26
  "element-model": "~1.1",
27
27
  "markdown-model": "~1.7",
28
28
  "schema-markdown": "~1.2",
29
29
  "schema-markdown-doc": "~1.5"
30
30
  },
31
31
  "devDependencies": {
32
- "ava": "~5.1",
33
- "c8": "~7.12",
34
- "eslint": "~8.31",
32
+ "ava": "~5.2",
33
+ "c8": "~7.13",
34
+ "eslint": "~8.35",
35
35
  "jsdoc": "~4.0",
36
- "jsdom": "~20.0"
36
+ "jsdom": "~21.1"
37
37
  }
38
38
  }