markdown-up 1.6.0 → 1.7.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 +1 -2
- package/lib/app.js +146 -86
- package/lib/barChart.js +34 -0
- package/lib/csv.js +59 -0
- package/lib/data.js +518 -0
- package/lib/dataTable.js +94 -0
- package/lib/lineChart.js +632 -0
- package/lib/model.js +298 -0
- package/lib/script.js +462 -0
- package/lib/util.js +207 -0
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -50,8 +50,7 @@ To view local Markdown files using markdown-up, try the
|
|
|
50
50
|
|
|
51
51
|
## Development
|
|
52
52
|
|
|
53
|
-
markdown-up is
|
|
54
|
-
It is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme)
|
|
53
|
+
markdown-up is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme)
|
|
55
54
|
and it was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme):
|
|
56
55
|
|
|
57
56
|
```
|
package/lib/app.js
CHANGED
|
@@ -3,21 +3,23 @@
|
|
|
3
3
|
|
|
4
4
|
/** @module lib/app */
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import {MarkdownScriptRuntime, markdownScriptCodeBlock} from 'markdown-charts/lib/script.js';
|
|
6
|
+
import {MarkdownScriptRuntime, markdownScriptCodeBlock} from './script.js';
|
|
8
7
|
import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
|
|
9
8
|
import {getBaseURL, isRelativeURL, markdownElementsAsync} from 'markdown-model/lib/elements.js';
|
|
10
9
|
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
11
|
-
import {
|
|
12
|
-
import {barChartCodeBlock} from '
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
10
|
+
import {SchemaMarkdownParser} from 'schema-markdown/lib/parser.js';
|
|
11
|
+
import {barChartCodeBlock} from './barChart.js';
|
|
12
|
+
import {dataTableCodeBlock} from './dataTable.js';
|
|
13
|
+
import {evaluateExpression} from 'calc-script/lib/runtime.js';
|
|
14
|
+
import {lineChartCodeBlock} from './lineChart.js';
|
|
15
|
+
import {parseExpression} from 'calc-script/lib/parser.js';
|
|
16
|
+
import {renderElements} from 'element-model/lib/elementModel.js';
|
|
17
|
+
import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
|
|
18
|
+
import {validateType} from 'schema-markdown/lib/schema.js';
|
|
17
19
|
|
|
18
20
|
|
|
19
21
|
// The application's hash parameter type model
|
|
20
|
-
const
|
|
22
|
+
const markdownUpTypesSmd = `\
|
|
21
23
|
#
|
|
22
24
|
# **markdown-up** is a Markdown viewer application. Click the following link to learn more.
|
|
23
25
|
#
|
|
@@ -30,7 +32,7 @@ const markdownUpHashTypes = `\
|
|
|
30
32
|
struct MarkdownUp
|
|
31
33
|
|
|
32
34
|
# The resource URL
|
|
33
|
-
optional string
|
|
35
|
+
optional string url
|
|
34
36
|
|
|
35
37
|
# Optional command
|
|
36
38
|
optional Command cmd
|
|
@@ -51,103 +53,111 @@ struct MarkdownUp
|
|
|
51
53
|
optional int(== 1) debug
|
|
52
54
|
|
|
53
55
|
|
|
54
|
-
# Field value union
|
|
55
|
-
union FieldValue
|
|
56
|
-
|
|
57
|
-
# A datetime value
|
|
58
|
-
datetime(nullable) datetime
|
|
59
|
-
|
|
60
|
-
# A number value
|
|
61
|
-
float(nullable) number
|
|
62
|
-
|
|
63
|
-
# A string value
|
|
64
|
-
string(nullable) string
|
|
65
|
-
|
|
66
|
-
|
|
67
56
|
# Application command union
|
|
68
57
|
union Command
|
|
69
58
|
|
|
70
59
|
# Display the application's hash parameter documentation
|
|
71
|
-
|
|
60
|
+
int(== 1) help
|
|
72
61
|
|
|
73
62
|
# Show the URL's Markdown text
|
|
74
63
|
int(== 1) markdown
|
|
75
|
-
|
|
76
|
-
# Help type enum
|
|
77
|
-
enum HelpType
|
|
78
|
-
Help
|
|
79
|
-
Bar
|
|
80
|
-
Line
|
|
81
|
-
Table
|
|
82
64
|
`;
|
|
65
|
+
const markdownUpTypes = new SchemaMarkdownParser(markdownUpTypesSmd).types;
|
|
83
66
|
|
|
84
67
|
|
|
85
68
|
/**
|
|
86
69
|
* A markdown-up application options object
|
|
87
70
|
*
|
|
88
71
|
* @typedef {Object} MarkdownUpOptions
|
|
89
|
-
* @param {number} [
|
|
90
|
-
* @param {number} [
|
|
91
|
-
* @param {
|
|
92
|
-
* @param {
|
|
72
|
+
* @param {number} [fontSize = 12] - The font size, in points
|
|
73
|
+
* @param {number} [lineHeight = 1.2] - The line height, in em
|
|
74
|
+
* @param {string} [markdownText = null] - The markdown text
|
|
75
|
+
* @param {boolean} [menu = true] - If true, show the menu
|
|
76
|
+
* @param {string} [url = 'README.md'] - The resource URL
|
|
93
77
|
*/
|
|
94
78
|
|
|
95
79
|
|
|
96
80
|
/**
|
|
97
|
-
* MarkdownUp, a
|
|
81
|
+
* MarkdownUp, a Markdown viewer
|
|
98
82
|
*
|
|
99
|
-
* The
|
|
100
|
-
*
|
|
101
|
-
* class.
|
|
102
|
-
*
|
|
103
|
-
* @extends ElementApplication
|
|
83
|
+
* @property {Object} window - The web browser window object
|
|
84
|
+
* @property {?Object} params - The validated hash parameters object
|
|
104
85
|
* @property {number} fontSize - The font size, in points
|
|
105
86
|
* @property {number} lineHeight - The line height, in em
|
|
87
|
+
* @property {string} markdownText - The markdown text
|
|
106
88
|
* @property {boolean} menu - If true, show the menu
|
|
107
89
|
* @property {string} url - The resource URL
|
|
108
|
-
* @property {number}
|
|
90
|
+
* @property {?number} setNavigateTimeoutId - The navigate timeout ID
|
|
109
91
|
*/
|
|
110
|
-
export class MarkdownUp
|
|
92
|
+
export class MarkdownUp {
|
|
111
93
|
/**
|
|
112
94
|
* Create an application instance
|
|
113
95
|
*
|
|
114
96
|
* @param {Object} window - The web browser window object
|
|
115
97
|
* @property {module:lib/app~MarkdownUpOptions} [options] - The markdown-up application options
|
|
116
98
|
*/
|
|
117
|
-
constructor(window, {fontSize = 12, lineHeight = 1.2, menu = true, url = 'README.md'} = {}) {
|
|
118
|
-
|
|
99
|
+
constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
|
|
100
|
+
this.window = window;
|
|
101
|
+
this.params = null;
|
|
119
102
|
this.fontSize = fontSize;
|
|
120
103
|
this.lineHeight = lineHeight;
|
|
104
|
+
this.markdownText = markdownText;
|
|
121
105
|
this.menu = menu;
|
|
122
106
|
this.url = url;
|
|
123
107
|
this.setNavigateTimeoutId = null;
|
|
124
108
|
}
|
|
125
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Run the application. This method calls render and subscribes to any hash parameter changes to
|
|
112
|
+
* re-render on any hash parameter change.
|
|
113
|
+
*/
|
|
114
|
+
async run() {
|
|
115
|
+
await this.render();
|
|
116
|
+
this.window.addEventListener('hashchange', () => this.render(), false);
|
|
117
|
+
}
|
|
118
|
+
|
|
126
119
|
/**
|
|
127
120
|
* ElementApplication.render override to clear the navigate timeout
|
|
128
121
|
*/
|
|
129
122
|
async render() {
|
|
130
123
|
// Clear the navigate timeout ID, if one is set
|
|
131
|
-
/* c8 ignore start */
|
|
132
124
|
if (this.setNavigateTimeoutId !== null) {
|
|
133
125
|
this.window.clearTimeout(this.setNavigateTimeoutId);
|
|
134
126
|
this.setNavigateTimeoutId = null;
|
|
135
127
|
}
|
|
136
|
-
// eslint-disable-next lines-around-comment
|
|
137
|
-
/* c8 ignore stop */
|
|
138
128
|
|
|
139
|
-
//
|
|
140
|
-
|
|
141
|
-
|
|
129
|
+
// Parse the hash parameters and render the application element model
|
|
130
|
+
let result;
|
|
131
|
+
let isError = false;
|
|
132
|
+
try {
|
|
133
|
+
// Validate hash parameters
|
|
134
|
+
const paramsPrev = this.params;
|
|
135
|
+
this.updateParams();
|
|
136
|
+
|
|
137
|
+
// Skip the render if the page params haven't changed
|
|
138
|
+
if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Call the application main and validate the result
|
|
143
|
+
result = await this.main();
|
|
144
|
+
} catch ({message}) {
|
|
145
|
+
result = {'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}};
|
|
146
|
+
isError = true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Navigate?
|
|
150
|
+
if ('location' in result && result.location !== null) {
|
|
151
|
+
this.window.location.href = result.location;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Set the window title
|
|
156
|
+
const title = result.title ?? null;
|
|
157
|
+
if (title !== null) {
|
|
158
|
+
this.window.document.title = title;
|
|
159
|
+
}
|
|
142
160
|
|
|
143
|
-
/**
|
|
144
|
-
* The [Element Application pre-render callback]{@link
|
|
145
|
-
* https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#preRender}.
|
|
146
|
-
* The callback is used to set the document font size and line height.
|
|
147
|
-
*
|
|
148
|
-
* @override
|
|
149
|
-
*/
|
|
150
|
-
preRender() {
|
|
151
161
|
// Set the font size
|
|
152
162
|
const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
|
|
153
163
|
this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
|
|
@@ -155,6 +165,31 @@ export class MarkdownUp extends ElementApplication {
|
|
|
155
165
|
// Set the line height
|
|
156
166
|
const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
|
|
157
167
|
this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
|
|
168
|
+
|
|
169
|
+
// Render the element model
|
|
170
|
+
renderElements(this.window.document.body, result.elements);
|
|
171
|
+
|
|
172
|
+
// If there is a URL hash ID, re-navigate to go there since it was just rendered. After the
|
|
173
|
+
// first render, re-render is short-circuited by the unchanged hash param check above.
|
|
174
|
+
if (!isError && getHashID(this.window.location.hash) !== null) {
|
|
175
|
+
this.window.location.href = this.window.location.hash;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Parse and validate the hash parameters
|
|
181
|
+
*
|
|
182
|
+
* @param {?string} paramString - Optional parameter string for testing
|
|
183
|
+
*/
|
|
184
|
+
updateParams(paramString = null) {
|
|
185
|
+
// Clear, then validate the hash parameters (may throw)
|
|
186
|
+
this.params = null;
|
|
187
|
+
|
|
188
|
+
// Decode the params string
|
|
189
|
+
const params = decodeQueryString(paramString !== null ? paramString : this.window.location.hash.slice(1));
|
|
190
|
+
|
|
191
|
+
// Validate the params
|
|
192
|
+
this.params = validateType(markdownUpTypes, 'MarkdownUp', params);
|
|
158
193
|
}
|
|
159
194
|
|
|
160
195
|
/**
|
|
@@ -166,17 +201,22 @@ export class MarkdownUp extends ElementApplication {
|
|
|
166
201
|
*/
|
|
167
202
|
async main() {
|
|
168
203
|
// Load the text resource
|
|
169
|
-
const
|
|
204
|
+
const urlOverride = 'url' in this.params && this.params.url !== '';
|
|
205
|
+
const url = urlOverride ? this.params.url : this.url;
|
|
170
206
|
let markdownText = null;
|
|
171
207
|
let markdownModel = null;
|
|
172
208
|
let markdownTitle = null;
|
|
173
209
|
if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
210
|
+
if (this.markdownText !== null && !urlOverride) {
|
|
211
|
+
({markdownText} = this);
|
|
212
|
+
} else {
|
|
213
|
+
const response = await this.window.fetch(url);
|
|
214
|
+
if (!response.ok) {
|
|
215
|
+
const status = response.statusText;
|
|
216
|
+
throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
|
|
217
|
+
}
|
|
218
|
+
markdownText = await response.text();
|
|
178
219
|
}
|
|
179
|
-
markdownText = await response.text();
|
|
180
220
|
markdownModel = parseMarkdown(markdownText);
|
|
181
221
|
markdownTitle = getMarkdownTitle(markdownModel);
|
|
182
222
|
}
|
|
@@ -189,21 +229,15 @@ export class MarkdownUp extends ElementApplication {
|
|
|
189
229
|
cmdElements = {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}};
|
|
190
230
|
|
|
191
231
|
// Help
|
|
192
|
-
} else if (this.params.cmd.help === 'Bar') {
|
|
193
|
-
cmdElements = new UserTypeElements(this.params).getElements(chartModel.types, `BarChart`);
|
|
194
|
-
} else if (this.params.cmd.help === 'Line') {
|
|
195
|
-
cmdElements = new UserTypeElements(this.params).getElements(chartModel.types, `LineChart`);
|
|
196
|
-
} else if (this.params.cmd.help === 'Table') {
|
|
197
|
-
cmdElements = new UserTypeElements(this.params).getElements(chartModel.types, `DataTable`);
|
|
198
232
|
} else {
|
|
199
|
-
cmdElements =
|
|
233
|
+
cmdElements = schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)});
|
|
200
234
|
}
|
|
201
235
|
}
|
|
202
236
|
|
|
203
237
|
// Helper function to create a cycle link
|
|
204
238
|
const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
|
|
205
239
|
const value = paramName in this.params ? this.params[paramName] : defaultValue;
|
|
206
|
-
const {attr} =
|
|
240
|
+
const {attr} = markdownUpTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
|
|
207
241
|
let valueNew = value + delta;
|
|
208
242
|
if (valueNew > attr.lte) {
|
|
209
243
|
valueNew = attr.gte;
|
|
@@ -243,7 +277,7 @@ export class MarkdownUp extends ElementApplication {
|
|
|
243
277
|
if (rawURL.startsWith('#')) {
|
|
244
278
|
// Fixup the "url" param if its relative
|
|
245
279
|
const hashParams = decodeQueryString(rawURL.slice(1));
|
|
246
|
-
if ('url' in hashParams && isRelativeURL(hashParams.url)) {
|
|
280
|
+
if ('url' in hashParams && hashParams.url !== '' && isRelativeURL(hashParams.url)) {
|
|
247
281
|
hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
|
|
248
282
|
}
|
|
249
283
|
|
|
@@ -266,7 +300,6 @@ export class MarkdownUp extends ElementApplication {
|
|
|
266
300
|
}
|
|
267
301
|
};
|
|
268
302
|
|
|
269
|
-
/* c8 ignore start */
|
|
270
303
|
// The fetch function
|
|
271
304
|
// eslint-disable-next-line require-await
|
|
272
305
|
const fetchFn = async (fetchURL, init) => this.window.fetch(urlFn(fetchURL), init);
|
|
@@ -279,21 +312,27 @@ export class MarkdownUp extends ElementApplication {
|
|
|
279
312
|
this.setNavigateTimeoutId = this.window.setTimeout(timeoutCallback, delay);
|
|
280
313
|
};
|
|
281
314
|
// eslint-disable-next-line lines-around-comment
|
|
282
|
-
/* c8 ignore stop */
|
|
283
315
|
|
|
284
316
|
// Render the text as Markdown
|
|
285
317
|
const fontSize = 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
|
|
286
|
-
const
|
|
318
|
+
const scriptOptions = {
|
|
287
319
|
fetchFn,
|
|
288
320
|
fontSize,
|
|
289
|
-
urlFn,
|
|
290
321
|
'localStorage': this.window.localStorage,
|
|
291
322
|
logFn,
|
|
292
323
|
navigateTimeoutFn,
|
|
324
|
+
'params': this.params,
|
|
293
325
|
'sessionStorage': this.window.sessionStorage,
|
|
294
|
-
|
|
326
|
+
urlFn
|
|
295
327
|
};
|
|
296
|
-
|
|
328
|
+
if ('var' in this.params) {
|
|
329
|
+
scriptOptions.variables = {};
|
|
330
|
+
for (const varName of Object.keys(this.params.var)) {
|
|
331
|
+
const varExpr = parseExpression(this.params.var[varName]);
|
|
332
|
+
scriptOptions.variables[varName] = evaluateExpression(varExpr, scriptOptions.variables);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
scriptOptions.runtime = new MarkdownScriptRuntime(scriptOptions);
|
|
297
336
|
const result = {
|
|
298
337
|
'title': markdownTitle,
|
|
299
338
|
'elements': [
|
|
@@ -307,10 +346,10 @@ export class MarkdownUp extends ElementApplication {
|
|
|
307
346
|
// Render the markdown
|
|
308
347
|
cmdElements !== null ? null : await markdownElementsAsync(markdownModel, {
|
|
309
348
|
'codeBlocks': {
|
|
310
|
-
'bar-chart': (language, lines) => barChartCodeBlock(language, lines,
|
|
311
|
-
'data-table': (language, lines) => dataTableCodeBlock(language, lines,
|
|
312
|
-
'line-chart': (language, lines) => lineChartCodeBlock(language, lines,
|
|
313
|
-
'markdown-script': (language, lines) => markdownScriptCodeBlock(language, lines,
|
|
349
|
+
'bar-chart': (language, lines) => barChartCodeBlock(language, lines, scriptOptions),
|
|
350
|
+
'data-table': (language, lines) => dataTableCodeBlock(language, lines, scriptOptions),
|
|
351
|
+
'line-chart': (language, lines) => lineChartCodeBlock(language, lines, scriptOptions),
|
|
352
|
+
'markdown-script': (language, lines) => markdownScriptCodeBlock(language, lines, scriptOptions)
|
|
314
353
|
},
|
|
315
354
|
urlFn,
|
|
316
355
|
'headerIds': true
|
|
@@ -336,7 +375,7 @@ export class MarkdownUp extends ElementApplication {
|
|
|
336
375
|
'elem': [
|
|
337
376
|
{
|
|
338
377
|
'html': 'a',
|
|
339
|
-
'attr': {'href': linkCommandToggle('markdown', 1), 'aria-label': 'Show Markdown'},
|
|
378
|
+
'attr': {'href': linkCommandToggle('markdown', '1'), 'aria-label': 'Show Markdown'},
|
|
340
379
|
'elem': markdownSVGElements
|
|
341
380
|
},
|
|
342
381
|
{
|
|
@@ -351,7 +390,7 @@ export class MarkdownUp extends ElementApplication {
|
|
|
351
390
|
},
|
|
352
391
|
{
|
|
353
392
|
'html': 'a',
|
|
354
|
-
'attr': {'href': linkCommandToggle('help', '
|
|
393
|
+
'attr': {'href': linkCommandToggle('help', '1'), 'aria-label': 'Help'},
|
|
355
394
|
'elem': helpSVGElements
|
|
356
395
|
}
|
|
357
396
|
]
|
|
@@ -359,11 +398,32 @@ export class MarkdownUp extends ElementApplication {
|
|
|
359
398
|
]
|
|
360
399
|
};
|
|
361
400
|
|
|
401
|
+
// Set document title?
|
|
402
|
+
if (scriptOptions.runtime.documentTitle !== null) {
|
|
403
|
+
result.title = scriptOptions.runtime.documentTitle;
|
|
404
|
+
}
|
|
405
|
+
|
|
362
406
|
return result;
|
|
363
407
|
}
|
|
364
408
|
}
|
|
365
409
|
|
|
366
410
|
|
|
411
|
+
// Get a URL's hash ID
|
|
412
|
+
export function getHashID(url) {
|
|
413
|
+
const matchId = url.match(rHashId);
|
|
414
|
+
return matchId !== null ? matchId[1] : null;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const rHashId = /[#&]([^=]+)$/;
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
// Encode hash parameters with a hash ID
|
|
421
|
+
export function encodeHashID(params, hashID) {
|
|
422
|
+
const paramString = encodeQueryString(params);
|
|
423
|
+
return `#${paramString}${hashID !== null && paramString !== '' ? '&' : ''}${hashID !== null ? hashID : ''}`;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
|
|
367
427
|
// Icon sizes
|
|
368
428
|
const burgerSize = '24';
|
|
369
429
|
const iconSize = '36';
|
package/lib/barChart.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Licensed under the MIT License
|
|
2
|
+
// https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
|
|
3
|
+
|
|
4
|
+
/** @module lib/barChart */
|
|
5
|
+
|
|
6
|
+
import {chartCodeBlock} from './util.js';
|
|
7
|
+
import {loadChartData} from './data.js';
|
|
8
|
+
import {validateBarChart} from './model.js';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Bar chart code block function
|
|
13
|
+
*
|
|
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
|
+
* @returns {Object} The bar chart element model
|
|
18
|
+
*/
|
|
19
|
+
export function barChartCodeBlock(language, lines, options = null) {
|
|
20
|
+
return chartCodeBlock(language, lines, options, validateBarChart, barChartElements);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Render a bar chart
|
|
26
|
+
*
|
|
27
|
+
* @param {Object} barChart - The bar chart model
|
|
28
|
+
* @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
|
|
29
|
+
* @returns {Object} The bar chart element model
|
|
30
|
+
*/
|
|
31
|
+
export async function barChartElements(barChart, options = null) {
|
|
32
|
+
const {data} = await loadChartData(barChart, options);
|
|
33
|
+
return {'html': 'pre', 'elem': {'text': JSON.stringify(data, null, 4)}};
|
|
34
|
+
}
|
package/lib/csv.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Licensed under the MIT License
|
|
2
|
+
// https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
|
|
3
|
+
|
|
4
|
+
/** @module lib/csv */
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Parse CSV text to a JSON data array
|
|
9
|
+
*
|
|
10
|
+
* @param {string} text - The CSV text
|
|
11
|
+
* @returns {Object[]}
|
|
12
|
+
*/
|
|
13
|
+
export function parseCSV(text) {
|
|
14
|
+
const data = [];
|
|
15
|
+
const splitLines = text.split(rCSVLineSplit).map((line) => line.replace(rCSVLineScrub, '$1').split(rCSVFieldSplit));
|
|
16
|
+
const [fields] = splitLines;
|
|
17
|
+
for (let ixLine = 1; ixLine < splitLines.length; ixLine += 1) {
|
|
18
|
+
const splitLine = splitLines[ixLine];
|
|
19
|
+
if (splitLine.length > 1 || splitLine[0] !== '') {
|
|
20
|
+
data.push(Object.fromEntries(fields.map(
|
|
21
|
+
(field, ixField) => [field, ixField < splitLine.length ? splitLine[ixField] : 'null']
|
|
22
|
+
)));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const rCSVLineSplit = /\r?\n/;
|
|
29
|
+
const rCSVLineScrub = /^"?(.*?)"?$/;
|
|
30
|
+
const rCSVFieldSplit = /"?,"?/;
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Parse a CSV number
|
|
35
|
+
*/
|
|
36
|
+
export function parseCSVNumber(text) {
|
|
37
|
+
const value = Number.parseFloat(text);
|
|
38
|
+
if (Number.isNaN(value) || !Number.isFinite(value)) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parse a CSV datetime
|
|
47
|
+
*/
|
|
48
|
+
export function parseCSVDatetime(text) {
|
|
49
|
+
if (rCSVDate.test(text)) {
|
|
50
|
+
const localDate = new Date(text);
|
|
51
|
+
return new Date(localDate.getUTCFullYear(), localDate.getUTCMonth(), localDate.getUTCDate());
|
|
52
|
+
} else if (rCSVDatetime.test(text)) {
|
|
53
|
+
return new Date(text);
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const rCSVDate = /^\d{4}-\d{2}-\d{2}$/;
|
|
59
|
+
const rCSVDatetime = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$/;
|