markdown-up 1.8.0 → 1.8.2
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 +19 -4
- package/lib/data.js +35 -0
- package/lib/script.js +17 -1
- package/lib/scriptLibrary.js +12 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,9 +59,9 @@ allowing you to build lightweight, client-rendered web applications with no HTML
|
|
|
59
59
|
single dependency (MarkdownUp). The "markdown-script" fenced code blocks are interpreted as the
|
|
60
60
|
[CalcScript programming language](https://craigahobbs.github.io/calc-script/language/).
|
|
61
61
|
In addition to generating Markdown, you can fetch text and JSON resources, create SVG drawings,
|
|
62
|
-
parse CSV, render data tables, draw line charts, and more. For more information, see
|
|
63
|
-
[
|
|
64
|
-
[
|
|
62
|
+
parse CSV, render data tables, draw line charts, and more. For more information, see the
|
|
63
|
+
[MarkdownUp Library](https://craigahobbs.github.io/markdown-up/library/) and the
|
|
64
|
+
[CalcScript Library](https://craigahobbs.github.io/calc-script/library/).
|
|
65
65
|
|
|
66
66
|
For example:
|
|
67
67
|
|
|
@@ -107,9 +107,24 @@ jumpif (ixHello < helloCount) helloLoop
|
|
|
107
107
|
Click here to [see the example in action](https://craigahobbs.github.io/markdown-up/#url=DynamicMarkdownExample.md).
|
|
108
108
|
|
|
109
109
|
|
|
110
|
+
### Debug Mode
|
|
111
|
+
|
|
112
|
+
Debug mode enables logging output using the
|
|
113
|
+
[debugLog function](https://craigahobbs.github.io/calc-script/library/#var.vName='debugLog').
|
|
114
|
+
For example:
|
|
115
|
+
|
|
116
|
+
~~~ calcscript
|
|
117
|
+
debugLog('Hello debug')
|
|
118
|
+
~~~
|
|
119
|
+
|
|
120
|
+
Add the "#debug=1" hash parameter to the application URL to turn on debug mode.
|
|
121
|
+
Debug mode also enables the CalcScript linter, which performs static code analysis on your code and
|
|
122
|
+
reports warnings for any issues found.
|
|
123
|
+
|
|
124
|
+
|
|
110
125
|
### Links
|
|
111
126
|
|
|
112
|
-
- [
|
|
127
|
+
- [MarkdownUp Application Examples](https://craigahobbs.github.io/#url=MarkdownUpApplications.md)
|
|
113
128
|
- [The CalcScript Language](https://craigahobbs.github.io/calc-script/language/)
|
|
114
129
|
- [The CalcScript Library](https://craigahobbs.github.io/calc-script/library/)
|
|
115
130
|
- [The MarkdownUp Library](https://craigahobbs.github.io/markdown-up/library/)
|
package/lib/data.js
CHANGED
|
@@ -447,6 +447,41 @@ export function sortData(data, sorts) {
|
|
|
447
447
|
}
|
|
448
448
|
|
|
449
449
|
|
|
450
|
+
/**
|
|
451
|
+
* Top data rows
|
|
452
|
+
*
|
|
453
|
+
* @param {Object[]} data - The data array
|
|
454
|
+
* @param {number} count - The number of rows to keep
|
|
455
|
+
* @param {?string[]} [categoryFields = null] - The category fields
|
|
456
|
+
* @returns {Object[]} The top data array
|
|
457
|
+
*/
|
|
458
|
+
export function topData(data, count, categoryFields = null) {
|
|
459
|
+
// Bucket rows by category
|
|
460
|
+
const categoryRows = {};
|
|
461
|
+
const categoryOrder = [];
|
|
462
|
+
for (const row of data) {
|
|
463
|
+
const categoryKey = categoryFields === null ? ''
|
|
464
|
+
: JSON.stringify(categoryFields.map((field) => (field in row ? row[field] : null)));
|
|
465
|
+
if (!(categoryKey in categoryRows)) {
|
|
466
|
+
categoryRows[categoryKey] = [];
|
|
467
|
+
categoryOrder.push(categoryKey);
|
|
468
|
+
}
|
|
469
|
+
categoryRows[categoryKey].push(row);
|
|
470
|
+
}
|
|
471
|
+
// Take only the top rows
|
|
472
|
+
const dataTop = [];
|
|
473
|
+
const topCount = count;
|
|
474
|
+
for (const categoryKey of categoryOrder) {
|
|
475
|
+
const categoryKeyRows = categoryRows[categoryKey];
|
|
476
|
+
const categoryKeyLength = categoryKeyRows.length;
|
|
477
|
+
for (let ixRow = 0; ixRow < topCount && ixRow < categoryKeyLength; ixRow++) {
|
|
478
|
+
dataTop.push(categoryKeyRows[ixRow]);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return dataTop;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
|
|
450
485
|
// Helper function to format labels
|
|
451
486
|
export function formatValue(value, precision = null, datetime = null) {
|
|
452
487
|
if (value instanceof Date) {
|
package/lib/script.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
|
|
3
3
|
|
|
4
4
|
import {executeScriptAsync} from 'calc-script/lib/runtimeAsync.js';
|
|
5
|
+
import {lintScript} from 'calc-script/lib/model.js';
|
|
5
6
|
import {markdownElements} from 'markdown-model/lib/elements.js';
|
|
6
7
|
import {parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
7
8
|
import {parseScript} from 'calc-script/lib/parser.js';
|
|
@@ -51,7 +52,22 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
|
|
|
51
52
|
// Execute the calculation script
|
|
52
53
|
let errorMessage = null;
|
|
53
54
|
try {
|
|
54
|
-
|
|
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);
|
|
55
71
|
} catch ({message}) {
|
|
56
72
|
errorMessage = message;
|
|
57
73
|
}
|
package/lib/scriptLibrary.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// Licensed under the MIT License
|
|
2
2
|
// https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
addCalculatedField, aggregateData, filterData, joinData, parseCSV, sortData, topData, validateAggregation, validateData
|
|
6
|
+
} from './data.js';
|
|
5
7
|
import {dataTableElements, validateDataTable} from './dataTable.js';
|
|
6
8
|
import {escapeMarkdownText, markdownHeaderId} from 'markdown-model/lib/elements.js';
|
|
7
9
|
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
@@ -121,6 +123,15 @@ export const markdownScriptFunctions = {
|
|
|
121
123
|
runtime.addElements(dataTableElements(data, dataTableValidated, options));
|
|
122
124
|
},
|
|
123
125
|
|
|
126
|
+
// $function: dataTop
|
|
127
|
+
// $group: Data
|
|
128
|
+
// $doc: Keep the top rows for each category
|
|
129
|
+
// $arg data: The data array
|
|
130
|
+
// $arg count: The number of rows to keep
|
|
131
|
+
// $arg categoryFields: Optional (default is null). The category fields.
|
|
132
|
+
// $return: The top data array
|
|
133
|
+
'dataTop': ([data, count, categoryFields = null]) => topData(data, count, categoryFields),
|
|
134
|
+
|
|
124
135
|
// $function: dataValidate
|
|
125
136
|
// $group: Data
|
|
126
137
|
// $doc: Validate a data array
|