markdown-up 1.7.5 → 1.8.1

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.
@@ -1,37 +1,137 @@
1
1
  // Licensed under the MIT License
2
2
  // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
3
 
4
- import {escapeMarkdownLinkURL, escapeMarkdownText, markdownHeaderId} from 'markdown-model/lib/elements.js';
4
+ import {addCalculatedField, aggregateData, filterData, joinData, parseCSV, sortData, validateAggregation, validateData} from './data.js';
5
+ import {dataTableElements, validateDataTable} from './dataTable.js';
6
+ import {escapeMarkdownText, markdownHeaderId} from 'markdown-model/lib/elements.js';
5
7
  import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
8
+ import {lineChartElements, validateLineChart} from './lineChart.js';
6
9
  import {validateType, validateTypeModel} from 'schema-markdown/lib/schema.js';
7
- import {SchemaMarkdownParser} from 'schema-markdown/lib/parser.js';
10
+ import {encodeQueryString} from 'schema-markdown/lib/encode.js';
11
+ import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
12
+ import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
8
13
  import {typeModel} from 'schema-markdown/lib/typeModel.js';
14
+ import {validateElements} from 'element-model/lib/elementModel.js';
9
15
 
10
16
 
11
- /* eslint-disable id-length, max-len */
17
+ /* eslint-disable id-length */
12
18
 
13
19
 
14
20
  // Constants
21
+ const defaultFontFamily = 'Arial, Helvetica, sans-serif';
15
22
  const fontWidthRatio = 0.6;
16
23
  const pixelsPerPoint = 4 / 3;
24
+ const svgPrecision = 8;
17
25
 
18
26
 
19
- // Helper function to flatten and line-split nested arrays of strings
20
- function flattenAndSplitLines(lines) {
21
- return lines.flat().reduce(
22
- (result, line) => {
23
- result.push(...line.split(rLineSplit));
24
- return result;
25
- },
26
- []
27
- );
28
- }
27
+ // markdown-script library functions
28
+ export const markdownScriptFunctions = {
29
+ //
30
+ // Data and charting functions
31
+ //
29
32
 
30
- const rLineSplit = /\r?\n/;
33
+ // $function: dataAggregate
34
+ // $group: Data
35
+ // $doc: Aggregate a data array
36
+ // $arg data: The data array
37
+ // $arg aggregation: The [aggregation model](model.html#var.vName='Aggregation')
38
+ // $return: The aggregated data array
39
+ 'dataAggregate': ([data, aggregation]) => aggregateData(data, validateAggregation(aggregation)),
40
+
41
+ // $function: dataCalculatedField
42
+ // $group: Data
43
+ // $doc: Add a calculated field to a data array
44
+ // $arg data: The data array
45
+ // $arg fieldName: The calculated field name
46
+ // $arg expr: The calculated field expression
47
+ // $arg variables: Optional (default is null). A variables object the expression evaluation.
48
+ // $return: The updated data array
49
+ 'dataCalculatedField': ([data, fieldName, expr, variables = null]) => addCalculatedField(data, fieldName, expr, variables),
50
+
51
+ // $function: dataFilter
52
+ // $group: Data
53
+ // $doc: Filter a data array
54
+ // $arg data: The data array
55
+ // $arg expr: The filter expression
56
+ // $arg variables: Optional (default is null). A variables object the expression evaluation.
57
+ // $return: The filtered data array
58
+ 'dataFilter': ([data, expr, variables = null]) => filterData(data, expr, variables),
59
+
60
+ // $function: dataJoin
61
+ // $group: Data
62
+ // $doc: Join two data arrays
63
+ // $arg leftData: The left data array
64
+ // $arg rightData: The right data array
65
+ // $arg joinExpr: The [join expression](https://craigahobbs.github.io/calc-script/language/#expressions)
66
+ // $arg rightExpr: Optional (default is null).
67
+ // $arg rightExpr: The right [join expression](https://craigahobbs.github.io/calc-script/language/#expressions)
68
+ // $arg isLeftJoin: Optional (default is false). If true, perform a left join (always include left row).
69
+ // $arg variables: Optional (default is null). A variables object for join expression evaluation.
70
+ // $return: The joined data array
71
+ 'dataJoin': ([leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null]) => (
72
+ joinData(leftData, rightData, joinExpr, rightExpr, isLeftJoin, variables)
73
+ ),
74
+
75
+ // $function: dataLineChart
76
+ // $group: Data
77
+ // $doc: Draw a line chart
78
+ // $arg data: The data array
79
+ // $arg lineChart: The [line chart model](model.html#var.vName='LineChart')
80
+ 'dataLineChart': ([data, lineChart], options) => {
81
+ const {runtime} = options;
82
+
83
+ // Render the line chart
84
+ const elements = lineChartElements(data, validateLineChart(lineChart), options);
85
+ runtime.setElements();
86
+ runtime.addElements({'html': 'p', 'elem': elements});
87
+
88
+ // Set the drawing size (the chart is now the active drawing)
89
+ runtime.drawingWidth = elements.attr.width;
90
+ runtime.drawingHeight = elements.attr.height;
91
+ },
92
+
93
+ // $function: dataParseCSV
94
+ // $group: Data
95
+ // $doc: Parse CSV text to a data array
96
+ // $arg text: The CSV text
97
+ // $return: The data array
98
+ 'dataParseCSV': (text) => {
99
+ const data = parseCSV(text);
100
+ validateData(data, true);
101
+ return data;
102
+ },
103
+
104
+ // $function: dataSort
105
+ // $group: Data
106
+ // $doc: Sort a data array
107
+ // $arg data: The data array
108
+ // $arg sorts: The sort field-name/descending-sort tuples
109
+ // $return: The sorted data array
110
+ 'dataSort': ([data, sorts]) => sortData(data, sorts),
111
+
112
+ // $function: dataTable
113
+ // $group: Data
114
+ // $doc: Draw a data table
115
+ // $arg data: The data array
116
+ // $arg dataTable: Optional (default is null). The [data table model](model.html#var.vName='DataTable').
117
+ 'dataTable': ([data, dataTable = null], options) => {
118
+ const {runtime} = options;
119
+ runtime.setElements();
120
+ const dataTableValidated = (dataTable !== null ? validateDataTable(dataTable, options) : null);
121
+ runtime.addElements(dataTableElements(data, dataTableValidated, options));
122
+ },
123
+
124
+ // $function: dataValidate
125
+ // $group: Data
126
+ // $doc: Validate a data array
127
+ // $arg data: The data array
128
+ // $return: The validated data array
129
+ 'dataValidate': ([data]) => {
130
+ validateData(data);
131
+ return data;
132
+ },
31
133
 
32
134
 
33
- // markdown-script library functions
34
- export const markdownScriptFunctions = {
35
135
  //
36
136
  // Document functions
37
137
  //
@@ -39,7 +139,10 @@ export const markdownScriptFunctions = {
39
139
  // $function: documentReset
40
140
  // $group: Document
41
141
  // $doc: Reset the document. This is useful within callback functions that re-render the document.
42
- 'documentReset': (unused, options) => options.runtime.documentReset(),
142
+ 'documentReset': (unused, options) => {
143
+ const {runtime} = options;
144
+ runtime.isDocumentReset = true;
145
+ },
43
146
 
44
147
  // $function: documentURL
45
148
  // $group: Document
@@ -80,32 +183,58 @@ export const markdownScriptFunctions = {
80
183
  // $group: Document
81
184
  // $doc: Set focus to an element
82
185
  // $arg id: The element ID
83
- 'setDocumentFocus': ([id], options) => options.runtime.setDocumentFocus(id),
186
+ 'setDocumentFocus': ([id], options) => {
187
+ const {runtime} = options;
188
+ runtime.documentFocus = id;
189
+ },
84
190
 
85
191
  // $function: setDocumentTitle
86
192
  // $group: Document
87
193
  // $doc: Set the document title
88
194
  // $arg title: The document title string
89
- 'setDocumentTitle': ([title], options) => options.runtime.setDocumentTitle(title),
195
+ 'setDocumentTitle': ([title], options) => {
196
+ const {runtime} = options;
197
+ runtime.documentTitle = title;
198
+ },
90
199
 
91
200
  // $function: setWindowLocation
92
201
  // $group: Document
93
202
  // $doc: Navigate the browser window to a location URL
94
203
  // $arg url: The new location URL
95
- 'setWindowLocation': ([location], options) => options.runtime.setWindowLocation(location),
204
+ 'setWindowLocation': ([location], options) => {
205
+ const {runtime} = options;
206
+ runtime.windowLocation = options.urlFn(location);
207
+ },
96
208
 
97
209
  // $function: setWindowResize
98
210
  // $group: Document
99
211
  // $doc: Set the browser window resize event handler
100
212
  // $arg callback: The window resize callback function
101
- 'setWindowResize': ([callback], options) => options.runtime.setWindowResize(callback),
213
+ 'setWindowResize': ([callback], options) => {
214
+ const {runtime} = options;
215
+ runtime.windowResize = () => {
216
+ options.statementCount = 0;
217
+ callback([], options);
218
+ options.runtimeUpdateFn();
219
+ };
220
+ },
102
221
 
103
222
  // $function: setWindowTimeout
104
223
  // $group: Document
105
224
  // $doc: Set the browser window timeout event handler
106
225
  // $arg callback: The window timeout callback function
107
226
  // $arg delay: The delay, in milliseconds, to ellapse before calling the timeout
108
- 'setWindowTimeout': ([callback, delay], options) => options.runtime.setWindowTimeout(callback, delay),
227
+ 'setWindowTimeout': ([callback, delay], options) => {
228
+ const {runtime} = options;
229
+ runtime.windowTimeout = [
230
+ () => {
231
+ options.statementCount = 0;
232
+ callback([], options);
233
+ options.runtimeUpdateFn();
234
+ },
235
+ delay
236
+ ];
237
+ },
109
238
 
110
239
 
111
240
  //
@@ -122,9 +251,14 @@ export const markdownScriptFunctions = {
122
251
  // $arg sweepFlag: Either clockwise turning arc (1) or counterclockwise turning arc (0)
123
252
  // $arg x: The x-coordinate of the end point
124
253
  // $arg y: The y-coordinate of the end point
125
- 'drawArc': ([rx, ry, angle, largeArcFlag, sweepFlag, x, y], options) => (
126
- options.runtime.drawArc(rx, ry, angle, largeArcFlag, sweepFlag, x, y)
127
- ),
254
+ 'drawArc': ([rx, ry, angle, largeArcFlag, sweepFlag, x, y], options) => {
255
+ const {runtime} = options;
256
+ runtime.setDrawingPath();
257
+ runtime.drawingPath.push(
258
+ `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${angle.toFixed(svgPrecision)} ` +
259
+ `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`
260
+ );
261
+ },
128
262
 
129
263
  // $function: drawCircle
130
264
  // $group: Drawing
@@ -132,12 +266,31 @@ export const markdownScriptFunctions = {
132
266
  // $arg cx: The x-coordinate of the center of the circle
133
267
  // $arg cy: The y-coordinate of the center of the circle
134
268
  // $arg r: The radius of the circle
135
- 'drawCircle': ([cx, cy, r], options) => options.runtime.drawCircle(cx, cy, r),
269
+ 'drawCircle': ([cx, cy, r], options) => {
270
+ const {runtime} = options;
271
+ const svg = runtime.setDrawing();
272
+ svg.elem.push({
273
+ 'svg': 'circle',
274
+ 'attr': {
275
+ 'fill': runtime.drawingPathFill,
276
+ 'stroke': runtime.drawingPathStroke,
277
+ 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
278
+ 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
279
+ 'cx': cx,
280
+ 'cy': cy,
281
+ 'r': r
282
+ }
283
+ });
284
+ },
136
285
 
137
286
  // $function: drawClose
138
287
  // $group: Drawing
139
288
  // $doc: Close the current drawing path
140
- 'drawClose': (unused, options) => options.runtime.drawClose(),
289
+ 'drawClose': (unused, options) => {
290
+ const {runtime} = options;
291
+ runtime.setDrawingPath();
292
+ runtime.drawingPath.push('Z');
293
+ },
141
294
 
142
295
  // $function: drawEllipse
143
296
  // $group: Drawing
@@ -146,13 +299,33 @@ export const markdownScriptFunctions = {
146
299
  // $arg cy: The y-coordinate of the center of the ellipse
147
300
  // $arg rx: The x-radius of the ellipse
148
301
  // $arg ry: The y-radius of the ellipse
149
- 'drawEllipse': ([cx, cy, rx, ry], options) => options.runtime.drawEllipse(cx, cy, rx, ry),
302
+ 'drawEllipse': ([cx, cy, rx, ry], options) => {
303
+ const {runtime} = options;
304
+ const svg = runtime.setDrawing();
305
+ svg.elem.push({
306
+ 'svg': 'ellipse',
307
+ 'attr': {
308
+ 'fill': runtime.drawingPathFill,
309
+ 'stroke': runtime.drawingPathStroke,
310
+ 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
311
+ 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
312
+ 'cx': cx,
313
+ 'cy': cy,
314
+ 'rx': rx,
315
+ 'ry': ry
316
+ }
317
+ });
318
+ },
150
319
 
151
320
  // $function: drawHLine
152
321
  // $group: Drawing
153
322
  // $doc: Draw a horizontal line from the current point to the end point
154
323
  // $arg x: The x-coordinate of the end point
155
- 'drawHLine': ([x], options) => options.runtime.drawHLine(x),
324
+ 'drawHLine': ([x], options) => {
325
+ const {runtime} = options;
326
+ runtime.setDrawingPath();
327
+ runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
328
+ },
156
329
 
157
330
  // $function: drawImage
158
331
  // $group: Drawing
@@ -162,27 +335,60 @@ export const markdownScriptFunctions = {
162
335
  // $arg width: The width of the image
163
336
  // $arg height: The height of the image
164
337
  // $arg href: The image resource URL
165
- 'drawImage': ([x, y, width, height, href], options) => options.runtime.drawImage(x, y, width, height, href),
338
+ 'drawImage': ([x, y, width, height, href], options) => {
339
+ const {runtime} = options;
340
+ const svg = runtime.setDrawing();
341
+ svg.elem.push({
342
+ 'svg': 'image',
343
+ 'attr': {
344
+ 'x': x,
345
+ 'y': y,
346
+ 'width': width,
347
+ 'height': height,
348
+ 'href': options.urlFn(href)
349
+ }
350
+ });
351
+ },
166
352
 
167
353
  // $function: drawLine
168
354
  // $group: Drawing
169
355
  // $doc: Draw a line from the current point to the end point
170
356
  // $arg x: The x-coordinate of the end point
171
357
  // $arg y: The y-coordinate of the end point
172
- 'drawLine': ([x, y], options) => options.runtime.drawLine(x, y),
358
+ 'drawLine': ([x, y], options) => {
359
+ const {runtime} = options;
360
+ runtime.setDrawingPath();
361
+ runtime.drawingPath.push(`L ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
362
+ },
173
363
 
174
364
  // $function: drawMove
175
365
  // $group: Drawing
176
366
  // $doc: Move the path's drawing point
177
367
  // $arg x: The x-coordinate of the new drawing point
178
368
  // $arg y: The y-coordinate of the new drawing point
179
- 'drawMove': ([x, y], options) => options.runtime.drawMove(x, y),
369
+ 'drawMove': ([x, y], options) => {
370
+ const {runtime} = options;
371
+ runtime.setDrawingPath();
372
+ runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
373
+ },
180
374
 
181
375
  // $function: drawOnClick
182
376
  // $group: Drawing
183
377
  // $doc: Set the most recent drawing object's on-click event handler
184
378
  // $arg callback: The on-click event callback function (x, y)
185
- 'drawOnClick': ([callback], options) => options.runtime.drawOnClick(callback),
379
+ 'drawOnClick': ([callback], options) => {
380
+ const {runtime} = options;
381
+ const svg = runtime.setDrawing();
382
+ const clickElement = svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1];
383
+ clickElement.callback = (element) => {
384
+ element.addEventListener('click', (event) => {
385
+ const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
386
+ options.statementCount = 0;
387
+ callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
388
+ options.runtimeUpdateFn();
389
+ });
390
+ };
391
+ },
186
392
 
187
393
  // $function: drawRect
188
394
  // $group: Drawing
@@ -193,7 +399,30 @@ export const markdownScriptFunctions = {
193
399
  // $arg height: The height of the rectangle
194
400
  // $arg rx: Optional (default is null). The horizontal corner radius of the rectangle.
195
401
  // $arg ry: Optional (default is null). The vertical corner radius of the rectangle.
196
- 'drawRect': ([x, y, width, height, rx = null, ry = null], options) => options.runtime.drawRect(x, y, width, height, rx, ry),
402
+ 'drawRect': ([x, y, width, height, rx = null, ry = null], options) => {
403
+ const {runtime} = options;
404
+ const svg = runtime.setDrawing();
405
+ const element = {
406
+ 'svg': 'rect',
407
+ 'attr': {
408
+ 'fill': runtime.drawingPathFill,
409
+ 'stroke': runtime.drawingPathStroke,
410
+ 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
411
+ 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
412
+ 'x': x,
413
+ 'y': y,
414
+ 'width': width,
415
+ 'height': height
416
+ }
417
+ };
418
+ if (rx !== null) {
419
+ element.attr.rx = rx;
420
+ }
421
+ if (ry !== null) {
422
+ element.attr.ry = ry;
423
+ }
424
+ svg.elem.push(element);
425
+ },
197
426
 
198
427
  // $function: drawStyle
199
428
  // $group: Drawing
@@ -203,9 +432,18 @@ export const markdownScriptFunctions = {
203
432
  // $arg fill: Optional (default is 'none'). The fill color.
204
433
  // $arg strokeDashArray: Optional (default is 'none'). The stroke
205
434
  // $arg strokeDashArray: [dash array](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray#usage_notes).
206
- 'drawStyle': ([stroke = 'black', strokeWidth = 1, fill = 'none', strokeDashArray = 'none'], options) => (
207
- options.runtime.drawStyle(stroke, strokeWidth, fill, strokeDashArray)
208
- ),
435
+ 'drawStyle': ([stroke = 'black', strokeWidth = 1, fill = 'none', strokeDashArray = 'none'], options) => {
436
+ const {runtime} = options;
437
+ if (stroke !== runtime.drawingPathStroke || strokeWidth !== runtime.drawingPathStrokeWidth ||
438
+ strokeDashArray !== runtime.drawingPathStrokeDashArray || fill !== runtime.drawingPathFill
439
+ ) {
440
+ runtime.setDrawing();
441
+ runtime.drawingPathStroke = stroke;
442
+ runtime.drawingPathStrokeWidth = strokeWidth;
443
+ runtime.drawingPathStrokeDashArray = strokeDashArray;
444
+ runtime.drawingPathFill = fill;
445
+ }
446
+ },
209
447
 
210
448
  // $function: drawText
211
449
  // $group: Drawing
@@ -216,10 +454,27 @@ export const markdownScriptFunctions = {
216
454
  // $arg textAnchor: Optional (default is 'middle'). The
217
455
  // $arg textAnchor: [text anchor](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor#usage_notes) style.
218
456
  // $arg dominantBaseline: Optional (default is 'middle'). The
219
- // $arg dominantBaseline: [dominant baseline](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dominant-baseline#usage_notes) style.
220
- 'drawText': ([text, x, y, textAnchor = 'middle', dominantBaseline = 'middle'], options) => (
221
- options.runtime.drawText(text, x, y, textAnchor, dominantBaseline)
222
- ),
457
+ // $arg dominantBaseline: [dominant baseline](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dominant-baseline#usage_notes)
458
+ // $arg dominantBaseline: style.
459
+ 'drawText': ([text, x, y, textAnchor = 'middle', dominantBaseline = 'middle'], options) => {
460
+ const {runtime} = options;
461
+ const svg = runtime.setDrawing();
462
+ svg.elem.push({
463
+ 'svg': 'text',
464
+ 'attr': {
465
+ 'fill': runtime.drawingFontFill,
466
+ 'font-family': runtime.drawingFontFamily,
467
+ 'font-size': runtime.drawingFontSizePx.toFixed(svgPrecision),
468
+ 'text-anchor': textAnchor,
469
+ 'dominant-baseline': dominantBaseline,
470
+ 'font-weight': (runtime.drawingFontBold ? 'bold' : 'normal'),
471
+ 'font-style': (runtime.drawingFontItalic ? 'italic' : 'normal'),
472
+ 'x': x,
473
+ 'y': y
474
+ },
475
+ 'elem': {'text': text}
476
+ });
477
+ },
223
478
 
224
479
  // $function: drawTextStyle
225
480
  // $group: Drawing
@@ -229,15 +484,24 @@ export const markdownScriptFunctions = {
229
484
  // $arg bold: Optional (default is false). If true, text is bold.
230
485
  // $arg italic: Optional (default is false). If true, text is italic.
231
486
  // $arg fontFamily: Optional (default is null, the default font family). The text font family.
232
- 'drawTextStyle': ([fontSizePx = null, textFill = 'black', bold = false, italic = false, fontFamily = null], options) => (
233
- options.runtime.drawTextStyle(fontSizePx, textFill, bold, italic, fontFamily)
234
- ),
487
+ 'drawTextStyle': ([fontSizePx = null, textFill = 'black', bold = false, italic = false, fontFamily = null], options) => {
488
+ const {runtime} = options;
489
+ runtime.drawingFontSizePx = (fontSizePx !== null ? fontSizePx : options.fontSize * pixelsPerPoint);
490
+ runtime.drawingFontFill = textFill;
491
+ runtime.drawingFontBold = bold;
492
+ runtime.drawingFontItalic = italic;
493
+ runtime.drawingFontFamily = (fontFamily !== null ? fontFamily : defaultFontFamily);
494
+ },
235
495
 
236
496
  // $function: drawVLine
237
497
  // $group: Drawing
238
498
  // $doc: Draw a vertical line from the current point to the end point
239
499
  // $arg y: The y-coordinate of the end point
240
- 'drawVLine': ([y], options) => options.runtime.drawVLine(y),
500
+ 'drawVLine': ([y], options) => {
501
+ const {runtime} = options;
502
+ runtime.setDrawingPath();
503
+ runtime.drawingPath.push(`V ${y.toFixed(svgPrecision)}`);
504
+ },
241
505
 
242
506
  // $function: getDrawingHeight
243
507
  // $group: Drawing
@@ -272,7 +536,12 @@ export const markdownScriptFunctions = {
272
536
  // $doc: Set the current drawing's size
273
537
  // $arg width: The width of the drawing
274
538
  // $arg height: The height of the drawing
275
- 'setDrawingSize': ([width, height], options) => options.runtime.setDrawingSize(width, height),
539
+ 'setDrawingSize': ([width, height], options) => {
540
+ const {runtime} = options;
541
+ runtime.drawingWidth = width;
542
+ runtime.drawingHeight = height;
543
+ runtime.setDrawing(true);
544
+ },
276
545
 
277
546
 
278
547
  //
@@ -283,7 +552,12 @@ export const markdownScriptFunctions = {
283
552
  // $group: Element Model
284
553
  // $doc: Render an [element model](https://github.com/craigahobbs/element-model#readme)
285
554
  // $arg element: The [element model](https://github.com/craigahobbs/element-model#readme)
286
- 'elementModelRender': ([elements], options) => options.runtime.elementModelRender(elements),
555
+ 'elementModelRender': ([elements], options) => {
556
+ const {runtime} = options;
557
+ runtime.setElements();
558
+ elementModelWrapCallbacks(elements, options);
559
+ runtime.addElements(validateElements(elements));
560
+ },
287
561
 
288
562
 
289
563
  //
@@ -323,17 +597,10 @@ export const markdownScriptFunctions = {
323
597
  // $function: markdownEscape
324
598
  // $group: Markdown
325
599
  // $doc: Escape text for inclusion in Markdown text
326
- // $arg text: The text to escape
600
+ // $arg text: The text
327
601
  // $return: The escaped text
328
602
  'markdownEscape': ([text]) => escapeMarkdownText(text),
329
603
 
330
- // $function: markdownEscapeLinkURL
331
- // $group: Markdown
332
- // $doc: Escape a URL for inclusion in a Markdown link
333
- // $arg text: The URL to escape
334
- // $return: The escaped URL
335
- 'markdownEscapeLinkURL': ([url]) => escapeMarkdownLinkURL(url),
336
-
337
604
  // $function: markdownHeaderId
338
605
  // $group: Markdown
339
606
  // $doc: Compute the Markdown header element ID for some text
@@ -352,7 +619,11 @@ export const markdownScriptFunctions = {
352
619
  // $group: Markdown
353
620
  // $doc: Render Markdown text
354
621
  // $arg lines: The Markdown text lines (may contain nested arrays of un-split lines)
355
- 'markdownPrint': (lines, options) => options.runtime.markdownPrint(lines),
622
+ 'markdownPrint': (lines, options) => {
623
+ const {runtime} = options;
624
+ runtime.setMarkdown();
625
+ runtime.addMarkdown(lines);
626
+ },
356
627
 
357
628
  // $function: markdownTitle
358
629
  // $group: Markdown
@@ -372,11 +643,7 @@ export const markdownScriptFunctions = {
372
643
  // $arg lines: The [Schema Markdown](https://craigahobbs.github.io/schema-markdown/schema-markdown.html)
373
644
  // $arg lines: text lines (may contain nested arrays of un-split lines)
374
645
  // $return: The schema's [type model](https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='Types')
375
- 'schemaParse': (lines) => {
376
- const parser = new SchemaMarkdownParser();
377
- parser.parse(flattenAndSplitLines(lines));
378
- return parser.types;
379
- },
646
+ 'schemaParse': (lines) => parseSchemaMarkdown(lines),
380
647
 
381
648
  // $function: schemaPrint
382
649
  // $group: Schema
@@ -385,7 +652,16 @@ export const markdownScriptFunctions = {
385
652
  // $arg typeName: The type name
386
653
  // $arg actionURLs: Optional (default is null). The
387
654
  // $arg actionURLs: [action URL overrides](https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='ActionURL').
388
- 'schemaPrint': ([types, typeName, actionURLs = null], options) => options.runtime.schemaPrint(types, typeName, actionURLs),
655
+ 'schemaPrint': ([types, typeName, actionURLs = null], options) => {
656
+ const {runtime} = options;
657
+ const params = encodeQueryString(options.params);
658
+ const schemaOptions = {params};
659
+ if (actionURLs !== null) {
660
+ schemaOptions.actionURLs = actionURLs;
661
+ }
662
+ runtime.setElements();
663
+ runtime.addElements(schemaMarkdownDoc(types, typeName, schemaOptions));
664
+ },
389
665
 
390
666
  // $function: schemaTypeModel
391
667
  // $group: Schema
@@ -439,3 +715,42 @@ export const markdownScriptFunctions = {
439
715
  // $arg key: The key string
440
716
  'sessionStorageRemove': ([key], options) => options.window.sessionStorage.removeItem(key)
441
717
  };
718
+
719
+
720
+ // Helper function for elementModelRender function
721
+ function elementModelWrapCallbacks(elements, options) {
722
+ if (Array.isArray(elements)) {
723
+ for (const childElements of elements) {
724
+ elementModelWrapCallbacks(childElements, options);
725
+ }
726
+ } else if (typeof elements === 'object') {
727
+ if ('elem' in elements) {
728
+ elementModelWrapCallbacks(elements.elem, options);
729
+ }
730
+
731
+ // Element callback attribute must be map of event => callback
732
+ if ('callback' in elements) {
733
+ const elementEvents = elements.callback;
734
+ if (elementEvents !== null && typeof elementEvents === 'object') {
735
+ // Wrap the event handler function
736
+ elements.callback = (element) => {
737
+ // On element render, add a listener for each event
738
+ for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
739
+ element.addEventListener(elementEvent, (event) => {
740
+ // Determine the event callback args
741
+ const eventArgs = [];
742
+ if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
743
+ eventArgs.push(event.keyCode);
744
+ }
745
+
746
+ // Call the event handler
747
+ options.statementCount = 0;
748
+ elementEventCallback(eventArgs, options);
749
+ options.runtimeUpdateFn();
750
+ });
751
+ }
752
+ };
753
+ }
754
+ }
755
+ }
756
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.7.5",
4
+ "version": "1.8.1",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -22,16 +22,16 @@
22
22
  "test": "ava -v test/"
23
23
  },
24
24
  "dependencies": {
25
- "calc-script": "~1.1",
25
+ "calc-script": "~1.3",
26
26
  "element-model": "~1.1",
27
27
  "markdown-model": "~1.6",
28
- "schema-markdown": "~1.1",
28
+ "schema-markdown": "~1.2",
29
29
  "schema-markdown-doc": "~1.5"
30
30
  },
31
31
  "devDependencies": {
32
32
  "ava": "~4.3",
33
33
  "c8": "~7.12",
34
- "eslint": "~8.21",
34
+ "eslint": "~8.23",
35
35
  "jsdoc": "~3.6",
36
36
  "jsdom": "~20.0"
37
37
  }