markdown-up 1.6.0 → 1.7.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/lib/app.js CHANGED
@@ -4,16 +4,16 @@
4
4
  /** @module lib/app */
5
5
 
6
6
  import {ElementApplication, encodeHashID, getHashID} from 'element-app/lib/app.js';
7
- import {MarkdownScriptRuntime, markdownScriptCodeBlock} from 'markdown-charts/lib/script.js';
7
+ import {MarkdownScriptRuntime, markdownScriptCodeBlock} from './script.js';
8
8
  import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
9
9
  import {getBaseURL, isRelativeURL, markdownElementsAsync} from 'markdown-model/lib/elements.js';
10
10
  import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
11
11
  import {UserTypeElements} from 'schema-markdown-doc/lib/userTypeElements.js';
12
- import {barChartCodeBlock} from 'markdown-charts/lib/barChart.js';
13
- import {chartModel} from 'markdown-charts/lib/model.js';
14
- import {computeVariables} from 'markdown-charts/lib/data.js';
15
- import {dataTableCodeBlock} from 'markdown-charts/lib/dataTable.js';
16
- import {lineChartCodeBlock} from 'markdown-charts/lib/lineChart.js';
12
+ import {barChartCodeBlock} from './barChart.js';
13
+ import {dataTableCodeBlock} from './dataTable.js';
14
+ import {evaluateExpression} from 'calc-script/lib/runtime.js';
15
+ import {lineChartCodeBlock} from './lineChart.js';
16
+ import {parseExpression} from 'calc-script/lib/parser.js';
17
17
 
18
18
 
19
19
  // The application's hash parameter type model
@@ -68,17 +68,10 @@ union FieldValue
68
68
  union Command
69
69
 
70
70
  # Display the application's hash parameter documentation
71
- HelpType help
71
+ int(== 1) help
72
72
 
73
73
  # Show the URL's Markdown text
74
74
  int(== 1) markdown
75
-
76
- # Help type enum
77
- enum HelpType
78
- Help
79
- Bar
80
- Line
81
- Table
82
75
  `;
83
76
 
84
77
 
@@ -103,6 +96,7 @@ enum HelpType
103
96
  * @extends ElementApplication
104
97
  * @property {number} fontSize - The font size, in points
105
98
  * @property {number} lineHeight - The line height, in em
99
+ * @property {string} markdownText - The markdown text
106
100
  * @property {boolean} menu - If true, show the menu
107
101
  * @property {string} url - The resource URL
108
102
  * @property {number} [setNavigateTimeoutId = null] - The navigate timeout ID
@@ -114,10 +108,11 @@ export class MarkdownUp extends ElementApplication {
114
108
  * @param {Object} window - The web browser window object
115
109
  * @property {module:lib/app~MarkdownUpOptions} [options] - The markdown-up application options
116
110
  */
117
- constructor(window, {fontSize = 12, lineHeight = 1.2, menu = true, url = 'README.md'} = {}) {
118
- super(window, 'markdown-up', 'MarkdownUp', markdownUpHashTypes);
111
+ constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
112
+ super(window, null, 'MarkdownUp', markdownUpHashTypes);
119
113
  this.fontSize = fontSize;
120
114
  this.lineHeight = lineHeight;
115
+ this.markdownText = markdownText;
121
116
  this.menu = menu;
122
117
  this.url = url;
123
118
  this.setNavigateTimeoutId = null;
@@ -128,13 +123,10 @@ export class MarkdownUp extends ElementApplication {
128
123
  */
129
124
  async render() {
130
125
  // Clear the navigate timeout ID, if one is set
131
- /* c8 ignore start */
132
126
  if (this.setNavigateTimeoutId !== null) {
133
127
  this.window.clearTimeout(this.setNavigateTimeoutId);
134
128
  this.setNavigateTimeoutId = null;
135
129
  }
136
- // eslint-disable-next lines-around-comment
137
- /* c8 ignore stop */
138
130
 
139
131
  // Call the base class implementation
140
132
  await super.render();
@@ -171,12 +163,16 @@ export class MarkdownUp extends ElementApplication {
171
163
  let markdownModel = null;
172
164
  let markdownTitle = null;
173
165
  if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
174
- const response = await this.window.fetch(url);
175
- if (!response.ok) {
176
- const status = response.statusText;
177
- throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
166
+ if (this.markdownText !== null && !('url' in this.params)) {
167
+ ({markdownText} = this);
168
+ } else {
169
+ const response = await this.window.fetch(url);
170
+ if (!response.ok) {
171
+ const status = response.statusText;
172
+ throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
173
+ }
174
+ markdownText = await response.text();
178
175
  }
179
- markdownText = await response.text();
180
176
  markdownModel = parseMarkdown(markdownText);
181
177
  markdownTitle = getMarkdownTitle(markdownModel);
182
178
  }
@@ -189,12 +185,6 @@ export class MarkdownUp extends ElementApplication {
189
185
  cmdElements = {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}};
190
186
 
191
187
  // 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
188
  } else {
199
189
  cmdElements = new UserTypeElements(this.params).getElements(this.hashTypes, this.hashType);
200
190
  }
@@ -266,7 +256,6 @@ export class MarkdownUp extends ElementApplication {
266
256
  }
267
257
  };
268
258
 
269
- /* c8 ignore start */
270
259
  // The fetch function
271
260
  // eslint-disable-next-line require-await
272
261
  const fetchFn = async (fetchURL, init) => this.window.fetch(urlFn(fetchURL), init);
@@ -279,20 +268,26 @@ export class MarkdownUp extends ElementApplication {
279
268
  this.setNavigateTimeoutId = this.window.setTimeout(timeoutCallback, delay);
280
269
  };
281
270
  // eslint-disable-next-line lines-around-comment
282
- /* c8 ignore stop */
283
271
 
284
272
  // Render the text as Markdown
285
273
  const fontSize = 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
286
274
  const chartOptions = {
287
275
  fetchFn,
288
276
  fontSize,
289
- urlFn,
290
277
  'localStorage': this.window.localStorage,
291
278
  logFn,
292
279
  navigateTimeoutFn,
280
+ 'params': this.params,
293
281
  'sessionStorage': this.window.sessionStorage,
294
- 'variables': 'var' in this.params ? computeVariables(this.params.var) : {}
282
+ urlFn
295
283
  };
284
+ if ('var' in this.params) {
285
+ chartOptions.variables = {};
286
+ for (const varName of Object.keys(this.params.var)) {
287
+ const varExpr = parseExpression(this.params.var[varName]);
288
+ chartOptions.variables[varName] = evaluateExpression(varExpr, chartOptions.variables);
289
+ }
290
+ }
296
291
  chartOptions.runtime = new MarkdownScriptRuntime(chartOptions);
297
292
  const result = {
298
293
  'title': markdownTitle,
@@ -336,7 +331,7 @@ export class MarkdownUp extends ElementApplication {
336
331
  'elem': [
337
332
  {
338
333
  'html': 'a',
339
- 'attr': {'href': linkCommandToggle('markdown', 1), 'aria-label': 'Show Markdown'},
334
+ 'attr': {'href': linkCommandToggle('markdown', '1'), 'aria-label': 'Show Markdown'},
340
335
  'elem': markdownSVGElements
341
336
  },
342
337
  {
@@ -351,7 +346,7 @@ export class MarkdownUp extends ElementApplication {
351
346
  },
352
347
  {
353
348
  'html': 'a',
354
- 'attr': {'href': linkCommandToggle('help', 'Help'), 'aria-label': 'Help'},
349
+ 'attr': {'href': linkCommandToggle('help', '1'), 'aria-label': 'Help'},
355
350
  'elem': helpSVGElements
356
351
  }
357
352
  ]
@@ -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})$/;