markdown-up 2.4.1 → 3.1.0

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
@@ -69,7 +69,7 @@ parse CSV, render data tables, draw line charts, and more. For more information,
69
69
 
70
70
  For example:
71
71
 
72
- ```
72
+ ``` markdown
73
73
  # Dynamic Markdown Example
74
74
 
75
75
  ~~~ markdown-script
@@ -117,7 +117,7 @@ following example that uses the
117
117
  [unittest.mds](https://craigahobbs.github.io/markdown-up/library/include.html#var.vGroup='unittest.mds')
118
118
  system library:
119
119
 
120
- ~~~
120
+ ~~~ barescript
121
121
  include <unittest.mds>
122
122
 
123
123
  function testSanity():
package/lib/app.js CHANGED
@@ -182,23 +182,21 @@ export class MarkdownUp {
182
182
  // Render the element model
183
183
  renderElements(this.window.document.body, result.elements);
184
184
 
185
- // Focus?
186
- if ('focus' in result) {
187
- this.setDocumentFocus(result.focus);
188
- }
189
-
190
185
  // Navigate?
191
186
  // Note: This is done after render since it may have no effect (in which case we need to render)
192
187
  if ('location' in result && result.location !== null) {
193
188
  this.window.location.href = result.location;
194
- return;
195
- }
196
189
 
197
190
  // If there is a header ID, re-navigate to scroll to the header ID since it was just rendered.
198
191
  // The re-render is short-circuited by the unchanged hash param check above.
199
- if (!isError && getHeaderId(this.window.location.hash) !== null) {
192
+ } else if (!isError && getHeaderId(this.window.location.hash) !== null) {
200
193
  this.window.location.href = this.window.location.hash;
201
194
  }
195
+
196
+ // Focus?
197
+ if ('focus' in result) {
198
+ this.setDocumentFocus(result.focus);
199
+ }
202
200
  }
203
201
 
204
202
 
@@ -280,7 +278,7 @@ export class MarkdownUp {
280
278
  const urlOverride = 'url' in this.params && this.params.url !== '';
281
279
  const url = urlOverride ? this.params.url : this.url;
282
280
  const scriptOptions = this.createScriptOptions(url);
283
- let markdownText = null;
281
+ let markdownText;
284
282
  let timeBegin;
285
283
  if (this.markdownText !== null && !urlOverride) {
286
284
  ({markdownText} = this);
@@ -452,17 +450,17 @@ export class MarkdownUp {
452
450
  }
453
451
  }
454
452
 
455
- // Focus?
456
- if (runtime.documentFocus !== null) {
457
- this.setDocumentFocus(runtime.documentFocus);
458
- }
459
-
460
453
  // Navigate?
461
454
  // Note: This is done after render since it may have no effect (in which case we want to render).
462
455
  if (runtime.windowLocation !== null) {
463
456
  this.window.location.href = runtime.windowLocation;
464
457
  }
465
458
 
459
+ // Focus?
460
+ if (runtime.documentFocus !== null) {
461
+ this.setDocumentFocus(runtime.documentFocus);
462
+ }
463
+
466
464
  // Reset the runtime
467
465
  runtime.reset();
468
466
  };
package/lib/dataTable.js CHANGED
@@ -3,12 +3,12 @@
3
3
 
4
4
  /** @module lib/dataTable */
5
5
 
6
- import {compareValues} from 'bare-script/lib/data.js';
7
6
  import {formatValue} from './dataUtil.js';
8
7
  import {markdownElements} from 'markdown-model/lib/elements.js';
9
8
  import {parseMarkdown} from 'markdown-model/lib/parser.js';
10
9
  import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
11
10
  import {validateType} from 'schema-markdown/lib/schema.js';
11
+ import {valueCompare} from 'bare-script/lib/value.js';
12
12
 
13
13
 
14
14
  // The data table model's Schema Markdown
@@ -164,7 +164,7 @@ export function dataTableElements(data, dataTable = null, options = null) {
164
164
 
165
165
  // Skip this value?
166
166
  if (skip) {
167
- skip = (compareValues(value, rowPrev[field] ?? null) === 0);
167
+ skip = (valueCompare(value, rowPrev[field] ?? null) === 0);
168
168
  }
169
169
 
170
170
  const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
package/lib/dataUtil.js CHANGED
@@ -1,36 +1,39 @@
1
1
  // Licensed under the MIT License
2
2
  // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
3
 
4
+ import {valueString, valueType} from 'bare-script/lib/value.js';
5
+
4
6
 
5
7
  // Helper function to format labels
6
8
  export function formatValue(value, precision = null, datetime = null, trim = null) {
7
- if (value instanceof Date) {
9
+ const type = valueType(value);
10
+ if (type === 'datetime') {
8
11
  if (datetime === 'year') {
9
12
  // Round to nearest year
10
13
  let valueRounded = value;
11
- if (value.getUTCMonth() > 5) {
14
+ if (value.getMonth() > 5) {
12
15
  valueRounded = new Date(value);
13
- valueRounded.setUTCFullYear(value.getUTCFullYear() + 1);
16
+ valueRounded.setFullYear(value.getFullYear() + 1);
14
17
  }
15
18
 
16
- const isoFormat = valueRounded.toISOString();
19
+ const isoFormat = valueString(valueRounded);
17
20
  return isoFormat.slice(0, isoFormat.indexOf('T') - 6);
18
21
  } else if (datetime === 'month') {
19
22
  // Round to the nearest month
20
23
  let valueRounded = value;
21
- if (value.getUTCDate() > 15) {
24
+ if (value.getDate() > 15) {
22
25
  valueRounded = new Date(value);
23
- valueRounded.setMonth(value.getUTCMonth() + 1);
26
+ valueRounded.setMonth(value.getMonth() + 1);
24
27
  }
25
28
 
26
- const isoFormat = valueRounded.toISOString();
29
+ const isoFormat = valueString(valueRounded);
27
30
  return isoFormat.slice(0, isoFormat.indexOf('T') - 3);
28
31
  } else if (datetime === 'day') {
29
- const isoFormat = value.toISOString();
32
+ const isoFormat = valueString(value);
30
33
  return isoFormat.slice(0, isoFormat.indexOf('T'));
31
34
  }
32
- return value.toISOString().replace(rDateCleanup, '');
33
- } else if (typeof value === 'number') {
35
+ return valueString(value).replace(rDateCleanup, '');
36
+ } else if (type === 'number') {
34
37
  const numberFormat = value.toFixed(precision ?? defaultPrecision);
35
38
  return (trim ?? defaultTrim) ? numberFormat.replace(rNumberCleanup, '') : numberFormat;
36
39
  }
@@ -40,7 +43,7 @@ export function formatValue(value, precision = null, datetime = null, trim = nul
40
43
  const defaultPrecision = 2;
41
44
  const defaultTrim = true;
42
45
  const rNumberCleanup = /\.0*$/;
43
- const rDateCleanup = /(?:(?:(?:-01)?T00:00)?:00)?\.\d\d\dZ$/;
46
+ const rDateCleanup = /(?:(?:(?:-01)?T00:00)?:00)?(?:\.\d{1,6})?(?:Z|[+-]\d\d:\d\d)$/;
44
47
 
45
48
 
46
49
  // Helper function to compute a value's parameter
@@ -49,7 +52,7 @@ export function valueParameter(value, minValue, maxValue) {
49
52
  return 0;
50
53
  }
51
54
 
52
- if (minValue instanceof Date) {
55
+ if (valueType(minValue) === 'datetime') {
53
56
  const minDateValue = minValue.valueOf();
54
57
  return (value.valueOf() - minDateValue) / (maxValue.valueOf() - minDateValue);
55
58
  }
@@ -60,7 +63,7 @@ export function valueParameter(value, minValue, maxValue) {
60
63
 
61
64
  // Helper function to compute a value from a parameter
62
65
  export function parameterValue(param, minValue, maxValue) {
63
- if (minValue instanceof Date) {
66
+ if (valueType(minValue) === 'datetime') {
64
67
  const minDateValue = minValue.valueOf();
65
68
  return new Date(minDateValue + param * (maxValue.valueOf() - minDateValue));
66
69
  }
package/lib/lineChart.js CHANGED
@@ -4,9 +4,9 @@
4
4
  /** @module lib/lineChart */
5
5
 
6
6
  import {formatValue, parameterValue, valueParameter} from './dataUtil.js';
7
- import {compareValues} from 'bare-script/lib/data.js';
8
7
  import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
9
8
  import {validateType} from 'schema-markdown/lib/schema.js';
9
+ import {valueCompare} from 'bare-script/lib/value.js';
10
10
 
11
11
 
12
12
  // The line chart model's Schema Markdown
@@ -185,7 +185,7 @@ export function lineChartElements(data, lineChart, options = null) {
185
185
  data.sort((row1, row2) => {
186
186
  const x1 = row1[xField] ?? null;
187
187
  const x2 = row2[xField] ?? null;
188
- return compareValues(x1, x2);
188
+ return valueCompare(x1, x2);
189
189
  });
190
190
 
191
191
  // Generate the line points - [(label, color, points), ...]