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.
package/lib/script.js CHANGED
@@ -1,18 +1,11 @@
1
1
  // Licensed under the MIT License
2
2
  // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
3
 
4
- /** @module lib/script */
5
-
6
- import {encodeQueryString} from 'schema-markdown/lib/encode.js';
7
4
  import {executeScriptAsync} from 'calc-script/lib/runtimeAsync.js';
5
+ import {lintScript} from 'calc-script/lib/model.js';
8
6
  import {markdownElements} from 'markdown-model/lib/elements.js';
9
7
  import {parseMarkdown} from 'markdown-model/lib/parser.js';
10
8
  import {parseScript} from 'calc-script/lib/parser.js';
11
- import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
12
- import {validateElements} from 'element-model/lib/elementModel.js';
13
-
14
-
15
- /* eslint-disable id-length, max-len */
16
9
 
17
10
 
18
11
  /**
@@ -21,25 +14,22 @@ import {validateElements} from 'element-model/lib/elementModel.js';
21
14
  *
22
15
  * @typedef {Object} MarkdownScriptOptions
23
16
  * @property {number} fontSize - The font size, in points
24
- * @property {Object} params - The hash parameters object
17
+ * @property {Object} params - The [hash parameters object]{@link https://craigahobbs.github.io/markdown-up/#cmd.help=1}
25
18
  * @property {Object} runtime - The [markdown-script runtime state]{@link module:lib/script.MarkdownScriptRuntime}
26
19
  * @property {Object} [variables] - The map of variable name to variable value
27
20
  * @property {Object} window - The web browser window object
28
21
  * @property {function} runtimeUpdateFn - The [runtime update callback function]{@link module:lib/util~MarkdownScriptRuntimeUpdateFn}
22
+ *
23
+ * @ignore
29
24
  */
30
25
 
26
+
31
27
  /**
32
28
  * A runtime update callback function
33
29
  *
34
30
  * @callback MarkdownScriptRuntimeUpdateFn
35
- */
36
-
37
- /**
38
- * A URL modifier function
39
31
  *
40
- * @callback URLFn
41
- * @param {string} url - The URL
42
- * @returns {string} The modified URL
32
+ * @ignore
43
33
  */
44
34
 
45
35
 
@@ -50,6 +40,8 @@ import {validateElements} from 'element-model/lib/elementModel.js';
50
40
  * @param {object} codeBlock - The code block model
51
41
  * @param {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
52
42
  * @returns {Object} The generated element model
43
+ *
44
+ * @ignore
53
45
  */
54
46
  export async function markdownScriptCodeBlock(codeBlock, options) {
55
47
  // Add the options variables to the runtime's globals
@@ -60,7 +52,22 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
60
52
  // Execute the calculation script
61
53
  let errorMessage = null;
62
54
  try {
63
- await executeScriptAsync(parseScript(codeBlock.lines, codeBlock.startLineNumber + 1), options);
55
+ // Parse the script
56
+ const script = parseScript(codeBlock.lines, codeBlock.startLineNumber + 1);
57
+
58
+ // Run the calc-script linter?
59
+ if ('logFn' in options) {
60
+ const warnings = lintScript(script);
61
+ if (warnings.length > 0) {
62
+ options.logFn(`Warnings for the script at line number ${codeBlock.startLineNumber + 1}:`);
63
+ for (const warning of warnings) {
64
+ options.logFn(` ${warning}`);
65
+ }
66
+ }
67
+ }
68
+
69
+ // Execute the script
70
+ await executeScriptAsync(script, options);
64
71
  } catch ({message}) {
65
72
  errorMessage = message;
66
73
  }
@@ -77,12 +84,11 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
77
84
  }
78
85
 
79
86
 
80
- // markdown-script runtime constants
87
+ // Constants
81
88
  const defaultDrawingWidth = 300;
82
89
  const defaultDrawingHeight = 200;
83
90
  const defaultFontFamily = 'Arial, Helvetica, sans-serif';
84
91
  const pixelsPerPoint = 4 / 3;
85
- const svgPrecision = 8;
86
92
 
87
93
 
88
94
  /**
@@ -94,7 +100,10 @@ const svgPrecision = 8;
94
100
  * @property {?string} documentTitle - The the runtime-set document title
95
101
  * @property {boolean} isDocumentReset - If true, the runtime requested a document-reset
96
102
  * @property {?string} windowLocation - The the runtime-set document location
97
- * @property {?function} windowTimeout - The the runtime-set timeout args (callback, delay)
103
+ * @property {?function} windowResize - The the runtime-set resize callback
104
+ * @property {?Array} windowTimeout - The the runtime-set timeout args (callback, delay)
105
+ *
106
+ * @ignore
98
107
  */
99
108
  export class MarkdownScriptRuntime {
100
109
  /**
@@ -133,16 +142,19 @@ export class MarkdownScriptRuntime {
133
142
  }
134
143
 
135
144
 
145
+ // Reset the runtime state
136
146
  reset() {
137
147
  this.documentFocus = null;
138
148
  this.documentTitle = null;
139
149
  this.isDocumentReset = false;
140
150
  this.windowLocation = null;
151
+ this.windowResize = null;
141
152
  this.windowTimeout = null;
142
153
  return this.resetElements();
143
154
  }
144
155
 
145
156
 
157
+ // Reset the runtime elements
146
158
  resetElements() {
147
159
  let elements = null;
148
160
  if (this.elements !== null) {
@@ -154,6 +166,7 @@ export class MarkdownScriptRuntime {
154
166
  }
155
167
 
156
168
 
169
+ // Helper method to prepare runtime for adding elements
157
170
  setElements() {
158
171
  if (this.elements === null) {
159
172
  this.elements = [];
@@ -163,13 +176,13 @@ export class MarkdownScriptRuntime {
163
176
  }
164
177
 
165
178
 
166
- getDrawingSVG() {
167
- const par = (this.elements.length !== 0 ? this.elements[this.elements.length - 1] : null);
168
- const svg = (par !== null && par.html === 'p' && 'elem' in par ? par.elem : null);
169
- return svg !== null && svg.svg === 'svg' ? svg : null;
179
+ // Helper method to add runtime elements
180
+ addElements(elements) {
181
+ this.elements.push(elements);
170
182
  }
171
183
 
172
184
 
185
+ // Helper method to start an SVG drawing
173
186
  setDrawing(newDrawing = false) {
174
187
  this.setElements();
175
188
  let svg = this.getDrawingSVG();
@@ -188,6 +201,15 @@ export class MarkdownScriptRuntime {
188
201
  }
189
202
 
190
203
 
204
+ // Helper method to get the current SVG drawing element
205
+ getDrawingSVG() {
206
+ const par = (this.elements.length !== 0 ? this.elements[this.elements.length - 1] : null);
207
+ const svg = (par !== null && par.html === 'p' && 'elem' in par ? par.elem : null);
208
+ return svg !== null && svg.svg === 'svg' ? svg : null;
209
+ }
210
+
211
+
212
+ // Helper method to start an SVG drawing path
191
213
  setDrawingPath() {
192
214
  if (this.drawingPath === null) {
193
215
  const svg = this.setDrawing();
@@ -206,6 +228,7 @@ export class MarkdownScriptRuntime {
206
228
  }
207
229
 
208
230
 
231
+ // Helper method to finish the current SVG drawing path
209
232
  finishDrawingPath() {
210
233
  if (this.drawingPath !== null) {
211
234
  const svg = this.getDrawingSVG();
@@ -217,6 +240,7 @@ export class MarkdownScriptRuntime {
217
240
  }
218
241
 
219
242
 
243
+ // Helper method to start a Markdown block
220
244
  setMarkdown() {
221
245
  if (this.markdown === null) {
222
246
  this.setElements();
@@ -225,316 +249,18 @@ export class MarkdownScriptRuntime {
225
249
  }
226
250
 
227
251
 
228
- finishMarkdown() {
229
- if (this.markdown !== null) {
230
- const markdownOptions = {'headerIds': true, 'urlFn': this.options.urlFn};
231
- this.elements.push(markdownElements(parseMarkdown(this.markdown), markdownOptions));
232
- this.markdown = null;
233
- }
234
- }
235
-
236
-
237
- //
238
- // Document functions
239
- //
240
-
241
-
242
- documentReset() {
243
- this.isDocumentReset = true;
244
- }
245
-
246
-
247
- setDocumentFocus(id) {
248
- this.documentFocus = id;
249
- }
250
-
251
-
252
- setDocumentTitle(title) {
253
- this.documentTitle = title;
254
- }
255
-
256
-
257
- setWindowLocation(location) {
258
- this.windowLocation = this.options.urlFn(location);
259
- }
260
-
261
-
262
- setWindowResize(callback) {
263
- this.windowResize = () => {
264
- this.options.statementCount = 0;
265
- callback([], this.options);
266
- this.options.runtimeUpdateFn();
267
- };
268
- }
269
-
270
-
271
- setWindowTimeout(callback, delay) {
272
- this.windowTimeout = [
273
- () => {
274
- this.options.statementCount = 0;
275
- callback([], this.options);
276
- this.options.runtimeUpdateFn();
277
- },
278
- delay
279
- ];
280
- }
281
-
282
-
283
- //
284
- // Drawing functions
285
- //
286
-
287
-
288
- drawArc(rx, ry, angle, largeArcFlag, sweepFlag, x, y) {
289
- this.setDrawingPath();
290
- this.drawingPath.push(
291
- `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${angle.toFixed(svgPrecision)} ` +
292
- `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`
293
- );
294
- }
295
-
296
-
297
- drawCircle(cx, cy, r) {
298
- const svg = this.setDrawing();
299
- svg.elem.push({
300
- 'svg': 'circle',
301
- 'attr': {
302
- 'fill': this.drawingPathFill,
303
- 'stroke': this.drawingPathStroke,
304
- 'stroke-width': this.drawingPathStrokeWidth.toFixed(svgPrecision),
305
- 'stroke-dasharray': this.drawingPathStrokeDashArray,
306
- 'cx': cx,
307
- 'cy': cy,
308
- 'r': r
309
- }
310
- });
311
- }
312
-
313
-
314
- drawClose() {
315
- this.setDrawingPath();
316
- this.drawingPath.push('Z');
317
- }
318
-
319
-
320
- drawEllipse(cx, cy, rx, ry) {
321
- const svg = this.setDrawing();
322
- svg.elem.push({
323
- 'svg': 'ellipse',
324
- 'attr': {
325
- 'fill': this.drawingPathFill,
326
- 'stroke': this.drawingPathStroke,
327
- 'stroke-width': this.drawingPathStrokeWidth.toFixed(svgPrecision),
328
- 'stroke-dasharray': this.drawingPathStrokeDashArray,
329
- 'cx': cx,
330
- 'cy': cy,
331
- 'rx': rx,
332
- 'ry': ry
333
- }
334
- });
335
- }
336
-
337
-
338
- drawHLine(x) {
339
- this.setDrawingPath();
340
- this.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
341
- }
342
-
343
-
344
- drawImage(x, y, width, height, href) {
345
- const svg = this.setDrawing();
346
- svg.elem.push({
347
- 'svg': 'image',
348
- 'attr': {
349
- 'x': x,
350
- 'y': y,
351
- 'width': width,
352
- 'height': height,
353
- 'href': this.options.urlFn(href)
354
- }
355
- });
356
- }
357
-
358
-
359
- drawLine(x, y) {
360
- this.setDrawingPath();
361
- this.drawingPath.push(`L ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
362
- }
363
-
364
-
365
- drawMove(x, y) {
366
- this.setDrawingPath();
367
- this.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
368
- }
369
-
370
-
371
- drawOnClick(callback) {
372
- const svg = this.setDrawing();
373
- const clickElement = svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1];
374
- clickElement.callback = (element) => {
375
- element.addEventListener('click', (event) => {
376
- const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
377
- this.options.statementCount = 0;
378
- callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], this.options);
379
- this.options.runtimeUpdateFn();
380
- });
381
- };
382
- }
383
-
384
-
385
- drawRect(x, y, width, height, rx = null, ry = null) {
386
- const svg = this.setDrawing();
387
- const element = {
388
- 'svg': 'rect',
389
- 'attr': {
390
- 'fill': this.drawingPathFill,
391
- 'stroke': this.drawingPathStroke,
392
- 'stroke-width': this.drawingPathStrokeWidth.toFixed(svgPrecision),
393
- 'stroke-dasharray': this.drawingPathStrokeDashArray,
394
- 'x': x,
395
- 'y': y,
396
- 'width': width,
397
- 'height': height
398
- }
399
- };
400
- if (rx !== null) {
401
- element.attr.rx = rx;
402
- }
403
- if (ry !== null) {
404
- element.attr.ry = ry;
405
- }
406
- svg.elem.push(element);
407
- }
408
-
409
-
410
- drawStyle(stroke = 'black', strokeWidth = 1, fill = 'none', strokeDashArray = 'none') {
411
- if (stroke !== this.drawingPathStroke || strokeWidth !== this.drawingPathStrokeWidth ||
412
- strokeDashArray !== this.drawingPathStrokeDashArray || fill !== this.drawingPathFill
413
- ) {
414
- this.setDrawing();
415
- this.drawingPathStroke = stroke;
416
- this.drawingPathStrokeWidth = strokeWidth;
417
- this.drawingPathStrokeDashArray = strokeDashArray;
418
- this.drawingPathFill = fill;
419
- }
420
- }
421
-
422
-
423
- drawText(text, x, y, textAnchor = 'middle', dominantBaseline = 'middle') {
424
- const svg = this.setDrawing();
425
- svg.elem.push({
426
- 'svg': 'text',
427
- 'attr': {
428
- 'fill': this.drawingFontFill,
429
- 'font-family': this.drawingFontFamily,
430
- 'font-size': this.drawingFontSizePx.toFixed(svgPrecision),
431
- 'text-anchor': textAnchor,
432
- 'dominant-baseline': dominantBaseline,
433
- 'font-weight': (this.drawingFontBold ? 'bold' : 'normal'),
434
- 'font-style': (this.drawingFontItalic ? 'italic' : 'normal'),
435
- 'x': x,
436
- 'y': y
437
- },
438
- 'elem': {'text': text}
439
- });
440
- }
441
-
442
-
443
- drawTextStyle(fontSizePx = null, textFill = 'black', bold = false, italic = false, fontFamily = null) {
444
- this.drawingFontSizePx = (fontSizePx !== null ? fontSizePx : this.options.fontSize * pixelsPerPoint);
445
- this.drawingFontFill = textFill;
446
- this.drawingFontBold = bold;
447
- this.drawingFontItalic = italic;
448
- this.drawingFontFamily = (fontFamily !== null ? fontFamily : defaultFontFamily);
449
- }
450
-
451
-
452
- drawVLine(y) {
453
- this.setDrawingPath();
454
- this.drawingPath.push(`V ${y.toFixed(svgPrecision)}`);
455
- }
456
-
457
-
458
- setDrawingSize(width, height) {
459
- this.drawingWidth = width;
460
- this.drawingHeight = height;
461
- this.setDrawing(true);
462
- }
463
-
464
-
465
- //
466
- // Element Model functions
467
- //
468
-
469
-
470
- elementModelRender(elements) {
471
- this.setElements();
472
- this.elementModelWrapCallbacks(elements);
473
- this.elements.push(validateElements(elements));
474
- }
475
-
476
-
477
- elementModelWrapCallbacks(elements) {
478
- if (Array.isArray(elements)) {
479
- for (const childElements of elements) {
480
- this.elementModelWrapCallbacks(childElements);
481
- }
482
- } else if (typeof elements === 'object') {
483
- if ('elem' in elements) {
484
- this.elementModelWrapCallbacks(elements.elem);
485
- }
486
-
487
- // Element callback attribute must be map of event => callback
488
- if ('callback' in elements) {
489
- const elementEvents = elements.callback;
490
- if (elementEvents !== null && typeof elementEvents === 'object') {
491
- // Wrap the event handler function
492
- elements.callback = (element) => {
493
- // On element render, add a listener for each event
494
- for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
495
- element.addEventListener(elementEvent, (event) => {
496
- // Determine the event callback args
497
- const eventArgs = [];
498
- if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
499
- eventArgs.push(event.keyCode);
500
- }
501
-
502
- // Call the event handler
503
- this.options.statementCount = 0;
504
- elementEventCallback(eventArgs, this.options);
505
- this.options.runtimeUpdateFn();
506
- });
507
- }
508
- };
509
- }
510
- }
511
- }
512
- }
513
-
514
-
515
- //
516
- // Markdown functions
517
- //
518
-
519
-
520
- markdownPrint(lines) {
521
- this.setMarkdown();
252
+ // Helper method to flatten and add Markdown text lines
253
+ addMarkdown(lines) {
522
254
  this.markdown.push(...lines.flat());
523
255
  }
524
256
 
525
257
 
526
- //
527
- // Schema functions
528
- //
529
-
530
-
531
- schemaPrint(types, typeName, actionURLs = null) {
532
- const params = encodeQueryString(this.options.params);
533
- const options = {params};
534
- if (actionURLs !== null) {
535
- options.actionURLs = actionURLs;
258
+ // Helper method to finish the current Markdown block and add its elements
259
+ finishMarkdown() {
260
+ if (this.markdown !== null) {
261
+ const markdownOptions = {'headerIds': true, 'urlFn': this.options.urlFn};
262
+ this.addElements(markdownElements(parseMarkdown(this.markdown), markdownOptions));
263
+ this.markdown = null;
536
264
  }
537
- this.setElements();
538
- this.elements.push(schemaMarkdownDoc(types, typeName, options));
539
265
  }
540
266
  }