markdown-up 1.7.2 → 1.7.5

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/barChart.js CHANGED
@@ -8,16 +8,18 @@ import {loadChartData} from './data.js';
8
8
  import {validateBarChart} from './model.js';
9
9
 
10
10
 
11
+ /* c8 ignore start */
12
+
13
+
11
14
  /**
12
15
  * Bar chart code block function
13
16
  *
14
- * @param {string} language - The code block language
15
- * @param {string[]} lines - The code block's text lines
16
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
17
+ * @param {object} codeBlock - The code block model
18
+ * @param {Object} [options=null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
17
19
  * @returns {Object} The bar chart element model
18
20
  */
19
- export function barChartCodeBlock(language, lines, options = null) {
20
- return chartCodeBlock(language, lines, options, validateBarChart, barChartElements);
21
+ export function barChartCodeBlock(codeBlock, options = null) {
22
+ return chartCodeBlock(codeBlock, options, validateBarChart, barChartElements);
21
23
  }
22
24
 
23
25
 
@@ -25,7 +27,7 @@ export function barChartCodeBlock(language, lines, options = null) {
25
27
  * Render a bar chart
26
28
  *
27
29
  * @param {Object} barChart - The bar chart model
28
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
30
+ * @param {Object} [options=null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
29
31
  * @returns {Object} The bar chart element model
30
32
  */
31
33
  export async function barChartElements(barChart, options = null) {
package/lib/csv.js CHANGED
@@ -4,6 +4,9 @@
4
4
  /** @module lib/csv */
5
5
 
6
6
 
7
+ /* c8 ignore start */
8
+
9
+
7
10
  /**
8
11
  * Parse CSV text to a JSON data array
9
12
  *
@@ -32,6 +35,9 @@ const rCSVFieldSplit = /"?,"?/;
32
35
 
33
36
  /**
34
37
  * Parse a CSV number
38
+ *
39
+ * @param {string} text - The text to parse
40
+ * @returns {number} The parsed number
35
41
  */
36
42
  export function parseCSVNumber(text) {
37
43
  const value = Number.parseFloat(text);
@@ -44,6 +50,9 @@ export function parseCSVNumber(text) {
44
50
 
45
51
  /**
46
52
  * Parse a CSV datetime
53
+ *
54
+ * @param {string} text - The text to parse
55
+ * @returns {Date} The parsed Date
47
56
  */
48
57
  export function parseCSVDatetime(text) {
49
58
  if (rCSVDate.test(text)) {
package/lib/data.js CHANGED
@@ -5,26 +5,30 @@
5
5
 
6
6
  import {parseCSV, parseCSVDatetime, parseCSVNumber} from './csv.js';
7
7
  import {compareValues} from './util.js';
8
- import {encodeMarkdownText} from 'markdown-model/lib/parser.js';
8
+ import {escapeMarkdownText} from 'markdown-model/lib/elements.js';
9
9
  import {evaluateExpression} from 'calc-script/lib/runtime.js';
10
10
  import {parseExpression} from 'calc-script/lib/parser.js';
11
11
 
12
12
 
13
+ /* c8 ignore start */
14
+
15
+
13
16
  // Data expression functions
14
17
  const dataExprFunctions = {
15
- 'markdownEncode': ([text]) => encodeMarkdownText(text)
18
+ 'markdownEscape': ([text]) => escapeMarkdownText(text)
16
19
  };
17
20
 
18
21
 
19
22
  // Helper function to create the data expression's variables argument
20
- function createDataVariables(chartModel, variables = null) {
23
+ function createDataVariables(chart, variables = null) {
21
24
  const dataVariables = {...dataExprFunctions};
25
+ const evalOptions = {'globals': dataVariables};
22
26
 
23
27
  // Add the chart model variable expressions
24
- if ('var' in chartModel) {
25
- for (const varName of Object.keys(chartModel.var)) {
26
- const varExpr = parseExpression(chartModel.var[varName]);
27
- dataVariables[varName] = evaluateExpression(varExpr, dataVariables);
28
+ if (chart !== null && 'var' in chart) {
29
+ for (const varName of Object.keys(chart.var)) {
30
+ const varExpr = parseExpression(chart.var[varName]);
31
+ dataVariables[varName] = evaluateExpression(varExpr, evalOptions);
28
32
  }
29
33
  }
30
34
 
@@ -51,52 +55,49 @@ function createDataVariables(chartModel, variables = null) {
51
55
  * Load the chart's data
52
56
  *
53
57
  * @async
54
- * @param {Object} chartModel - The chart model
55
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
56
- * @returns {module:lib/data~LoadChartDataResult}
58
+ * @param {Object} chart - The chart model
59
+ * @param {Object} [options=null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
60
+ * @returns [LoadChartDataResult]{@link module:lib/data~LoadChartDataResult}
57
61
  */
58
- export async function loadChartData(chartModel, options = null) {
59
- // Compute the data variable values
60
- const variables = createDataVariables(chartModel, options !== null ? options.variables : null);
61
-
62
+ export async function loadChartData(chart, options = null) {
62
63
  // Load the data resources
63
- if (options === null || !('fetchFn' in options)) {
64
- throw new Error('loadChartData - fetch function is not defined');
65
- }
66
- let {data, types} = await loadData(chartModel.data, options.fetchFn, variables);
64
+ let {data, types} = await loadData(chart.data, options);
65
+
66
+ // Compute the data variable values
67
+ const variables = createDataVariables(chart, options !== null ? options.variables : null);
67
68
 
68
69
  // Add calculated fields
69
- if ('calc' in chartModel) {
70
- for (const calculatedField of chartModel.calc) {
70
+ if ('calc' in chart) {
71
+ for (const calculatedField of chart.calc) {
71
72
  types[calculatedField.name] = addCalculatedField(data, calculatedField.name, calculatedField.expr, variables);
72
73
  }
73
74
  }
74
75
 
75
76
  // Filter the data
76
- if ('filter' in chartModel) {
77
- data = filterData(data, chartModel.filter, variables);
77
+ if ('filter' in chart) {
78
+ data = filterData(data, chart.filter, variables);
78
79
  }
79
80
 
80
81
  // Aggregate the data
81
- if ('agg' in chartModel) {
82
- ({data, types} = aggregateData(chartModel.agg, data, types));
82
+ if ('agg' in chart) {
83
+ ({data, types} = aggregateData(chart.agg, data, types));
83
84
  }
84
85
 
85
86
  // Add the post-aggregation calculated fields
86
- if ('aggcalc' in chartModel) {
87
- for (const calculatedField of chartModel.aggcalc) {
87
+ if ('aggcalc' in chart) {
88
+ for (const calculatedField of chart.aggcalc) {
88
89
  types[calculatedField.name] = addCalculatedField(data, calculatedField.name, calculatedField.expr, variables);
89
90
  }
90
91
  }
91
92
 
92
93
  // Sort the data
93
- if ('sort' in chartModel) {
94
- sortData(chartModel.sort, data);
94
+ if ('sort' in chart) {
95
+ sortData(chart.sort, data);
95
96
  }
96
97
 
97
98
  // Top the data
98
- if ('top' in chartModel) {
99
- data = topData(chartModel.top, data);
99
+ if ('top' in chart) {
100
+ data = topData(chart.top, data);
100
101
  }
101
102
 
102
103
  return {data, types, variables};
@@ -117,18 +118,18 @@ export async function loadChartData(chartModel, options = null) {
117
118
  *
118
119
  * @async
119
120
  * @param {Object} dataModel - The data model
120
- * @param {module:lib/util~FetchFn} fetchFn - The URL fetch function
121
- * @param {Object} [variables = null] - Map of variables to variable value
122
- * @returns {module:lib/data~LoadDataResult}
121
+ * @param {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
122
+ * @returns [LoadDataResult]{@link module:lib/data~LoadDataResult}
123
123
  */
124
- export async function loadData(dataModel, fetchFn, variables = null) {
124
+ export async function loadData(dataModel, options) {
125
125
  const dataBases = [dataModel, ...('join' in dataModel ? dataModel.join : [])];
126
126
  const dataURLs = dataBases.map((base) => base.url);
127
- const dataResponses = await Promise.all(dataURLs.map((joinURL) => fetchFn(joinURL)));
127
+ const dataResponses = await Promise.all(dataURLs.map((joinURL) => options.fetchFn(options.urlFn(joinURL))));
128
128
  const dataTypes = await Promise.all(dataResponses.map((response, ixResponse) => dataResponseHandler(dataURLs[ixResponse], response)));
129
129
 
130
130
  // Compute the data variable values
131
- const dataVariables = variables ?? createDataVariables(null);
131
+ const dataVariables = options.variables ?? createDataVariables(null);
132
+ const evalOptions = {'globals': dataVariables};
132
133
 
133
134
  // Join the data
134
135
  let [{data}] = dataTypes;
@@ -158,7 +159,7 @@ export async function loadData(dataModel, fetchFn, variables = null) {
158
159
  // Bucket the right rows by the right expression value
159
160
  const rightCategoryRows = {};
160
161
  for (const row of rightData) {
161
- const categoryKey = JSON.stringify(evaluateExpression(rightExpression, dataVariables, row));
162
+ const categoryKey = JSON.stringify(evaluateExpression(rightExpression, evalOptions, row));
162
163
  if (!(categoryKey in rightCategoryRows)) {
163
164
  rightCategoryRows[categoryKey] = [];
164
165
  }
@@ -176,7 +177,7 @@ export async function loadData(dataModel, fetchFn, variables = null) {
176
177
 
177
178
  data = [];
178
179
  for (const row of leftData) {
179
- const categoryKey = JSON.stringify(evaluateExpression(leftExpression, dataVariables, row));
180
+ const categoryKey = JSON.stringify(evaluateExpression(leftExpression, evalOptions, row));
180
181
  if (categoryKey in rightCategoryRows) {
181
182
  for (const rightRow of rightCategoryRows[categoryKey]) {
182
183
  const joinRow = {...row};
@@ -316,11 +317,12 @@ export function addCalculatedField(data, name, expr, variables = null) {
316
317
 
317
318
  // Compute the data variable values
318
319
  const dataVariables = variables ?? createDataVariables(null);
320
+ const evalOptions = {'globals': dataVariables};
319
321
 
320
322
  // Compute the calculated fields for each row
321
323
  let calcType = null;
322
324
  for (const row of data) {
323
- const calcValue = evaluateExpression(calcExpr, dataVariables, row);
325
+ const calcValue = evaluateExpression(calcExpr, evalOptions, row);
324
326
  row[name] = calcValue;
325
327
 
326
328
  // Determine the calculated field type
@@ -363,11 +365,12 @@ export function filterData(data, expr, variables = null) {
363
365
 
364
366
  // Compute the data variable values
365
367
  const dataVariables = variables ?? createDataVariables(null);
368
+ const evalOptions = {'globals': dataVariables};
366
369
 
367
370
  // Filter the data
368
371
  const filteredData = [];
369
372
  for (const row of data) {
370
- if (evaluateExpression(filterExpr, dataVariables, row)) {
373
+ if (evaluateExpression(filterExpr, evalOptions, row)) {
371
374
  filteredData.push(row);
372
375
  }
373
376
  }
@@ -381,7 +384,7 @@ export function filterData(data, expr, variables = null) {
381
384
  * @param {Object} aggregationModel - The aggregation model
382
385
  * @param {Object[]} data - The data array
383
386
  * @param {Object} types - The map of field name to field type ("datetime", "number", "string")
384
- * @returns {module:lib/data~LoadDataResult}
387
+ * @returns [LoadDataResult]{@link module:lib/data~LoadDataResult}
385
388
  */
386
389
  export function aggregateData(aggregationModel, data, types) {
387
390
  // Compute the aggregate field types
package/lib/dataTable.js CHANGED
@@ -10,16 +10,18 @@ import {parseMarkdown} from 'markdown-model/lib/parser.js';
10
10
  import {validateDataTable} from './model.js';
11
11
 
12
12
 
13
+ /* c8 ignore start */
14
+
15
+
13
16
  /**
14
17
  * Data table code block function
15
18
  *
16
- * @param {string} language - The code block language
17
- * @param {string[]} lines - The code block's text lines
18
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
19
+ * @param {object} codeBlock - The code block model
20
+ * @param {Object} [options=null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
19
21
  * @returns {Object} The data table element model
20
22
  */
21
- export function dataTableCodeBlock(language, lines, options = null) {
22
- return chartCodeBlock(language, lines, options, validateDataTable, dataTableElements);
23
+ export function dataTableCodeBlock(codeBlock, options = null) {
24
+ return chartCodeBlock(codeBlock, options, validateDataTable, dataTableElements);
23
25
  }
24
26
 
25
27
 
@@ -27,7 +29,7 @@ export function dataTableCodeBlock(language, lines, options = null) {
27
29
  * Render a data table
28
30
  *
29
31
  * @param {Object} dataTable - The data table model
30
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
32
+ * @param {Object} [options=null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
31
33
  * @returns {Object} The data table element model
32
34
  */
33
35
  export async function dataTableElements(dataTable, options = null) {
package/lib/lineChart.js CHANGED
@@ -12,6 +12,9 @@ import {parseExpression} from 'calc-script/lib/parser.js';
12
12
  import {validateLineChart} from './model.js';
13
13
 
14
14
 
15
+ /* c8 ignore start */
16
+
17
+
15
18
  // Line chart defaults
16
19
  const defaultWidth = 640;
17
20
  const defaultHeight = 320;
@@ -41,13 +44,12 @@ const chartLineWidth = 3;
41
44
  /**
42
45
  * Line chart code block function
43
46
  *
44
- * @param {string} language - The code block language
45
- * @param {string[]} lines - The code block's text lines
46
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
47
+ * @param {object} codeBlock - The code block model
48
+ * @param {Object} [options=null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
47
49
  * @returns {Object} The line chart element model
48
50
  */
49
- export function lineChartCodeBlock(language, lines, options = null) {
50
- return chartCodeBlock(language, lines, options, validateLineChart, lineChartElements);
51
+ export function lineChartCodeBlock(codeBlock, options = null) {
52
+ return chartCodeBlock(codeBlock, options, validateLineChart, lineChartElements);
51
53
  }
52
54
 
53
55
 
@@ -55,7 +57,7 @@ export function lineChartCodeBlock(language, lines, options = null) {
55
57
  * Render a line chart
56
58
  *
57
59
  * @param {Object} lineChart - The line chart model
58
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
60
+ * @param {Object} [options=null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
59
61
  * @returns {Object} The line chart element model
60
62
  */
61
63
  export async function lineChartElements(lineChart, options = null) {
@@ -162,8 +164,9 @@ export async function lineChartElements(lineChart, options = null) {
162
164
  }
163
165
 
164
166
  // Calculated expression helper function
167
+ const evalOptions = {'globals': variables};
165
168
  const computeExpr = (exprText, expectedType = null, desc = null) => {
166
- const exprValue = evaluateExpression(parseExpression(exprText), variables);
169
+ const exprValue = evaluateExpression(parseExpression(exprText), evalOptions);
167
170
  if (exprValue !== null && expectedType !== null) {
168
171
  const exprType = getCalculatedValueType(exprValue);
169
172
  if (exprType !== expectedType) {
package/lib/model.js CHANGED
@@ -14,7 +14,7 @@ import {validateType} from 'schema-markdown/lib/schema.js';
14
14
  * @returns {Object}
15
15
  */
16
16
  export function validateBarChart(barChart) {
17
- return validateType(chartModel.types, 'BarChart', barChart);
17
+ return validateType(chartTypes, 'BarChart', barChart);
18
18
  }
19
19
 
20
20
 
@@ -25,7 +25,7 @@ export function validateBarChart(barChart) {
25
25
  * @returns {Object}
26
26
  */
27
27
  export function validateDataTable(dataTable) {
28
- return validateType(chartModel.types, 'DataTable', dataTable);
28
+ return validateType(chartTypes, 'DataTable', dataTable);
29
29
  }
30
30
 
31
31
 
@@ -36,14 +36,14 @@ export function validateDataTable(dataTable) {
36
36
  * @returns {Object}
37
37
  */
38
38
  export function validateLineChart(lineChart) {
39
- return validateType(chartModel.types, 'LineChart', lineChart);
39
+ return validateType(chartTypes, 'LineChart', lineChart);
40
40
  }
41
41
 
42
42
 
43
43
  /**
44
44
  * The chart model defined using Schema Markdown
45
45
  */
46
- export const chartModelSmd = `\
46
+ const chartSmd = `\
47
47
  # Base struct for all chart types
48
48
  struct ChartBase
49
49
 
@@ -288,11 +288,5 @@ struct AxisTicks
288
288
 
289
289
  /**
290
290
  * The chart model
291
- *
292
- * @property {string} title - The model's title
293
- * @property {Object} types - The model's referenced types dictionary
294
291
  */
295
- export const chartModel = {
296
- 'title': 'The Chart Model',
297
- 'types': (new SchemaMarkdownParser(chartModelSmd)).types
298
- };
292
+ export const chartTypes = (new SchemaMarkdownParser(chartSmd)).types;