markdown-up 1.7.4 → 1.8.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 +64 -7
- package/lib/app.js +83 -108
- package/lib/data.js +348 -334
- package/lib/dataTable.js +95 -40
- package/lib/lineChart.js +194 -127
- package/lib/script.js +71 -681
- package/lib/scriptLibrary.js +756 -0
- package/package.json +7 -7
- package/lib/barChart.js +0 -33
- package/lib/csv.js +0 -59
- package/lib/model.js +0 -292
- package/lib/util.js +0 -203
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ To host a Markdown resource, download the MarkdownUp application stub to the dir
|
|
|
25
25
|
your Markdown files:
|
|
26
26
|
|
|
27
27
|
~~~
|
|
28
|
-
curl -O https://craigahobbs.github.io/markdown-up/
|
|
28
|
+
curl -O https://craigahobbs.github.io/markdown-up/extra/index.html
|
|
29
29
|
~~~
|
|
30
30
|
|
|
31
31
|
To test your Markdown page, start a local static web server:
|
|
@@ -52,17 +52,74 @@ To view a different Markdown resource, set the application's
|
|
|
52
52
|
(i.e., "http://127.0.0.1:8000#url=other.md").
|
|
53
53
|
|
|
54
54
|
|
|
55
|
-
##
|
|
55
|
+
## Dynamic Markdown Applications
|
|
56
|
+
|
|
57
|
+
Using MarkdownUp's "markdown-script" fenced code blocks, you can dynamically generate Markdown,
|
|
58
|
+
allowing you to build lightweight, client-rendered web applications with no HTML, no CSS, and a
|
|
59
|
+
single dependency (MarkdownUp). The "markdown-script" fenced code blocks are interpreted as the
|
|
60
|
+
[CalcScript programming language](https://craigahobbs.github.io/calc-script/language/).
|
|
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
|
+
[the MarkdownUp Library](https://craigahobbs.github.io/markdown-up/library/) and
|
|
64
|
+
[the CalcScript Library](https://craigahobbs.github.io/calc-script/library/).
|
|
65
|
+
|
|
66
|
+
For example:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
# MarkdownUp Dynamic Markdown Example
|
|
70
|
+
|
|
71
|
+
~~~ markdown-script
|
|
72
|
+
# Variable arguments
|
|
73
|
+
helloCount = if(vCount != null, vCount, 5)
|
|
74
|
+
|
|
75
|
+
# Constants
|
|
76
|
+
width = 140
|
|
77
|
+
height = 40
|
|
78
|
+
borderSize = 5
|
|
79
|
+
borderColor = 'blue'
|
|
80
|
+
|
|
81
|
+
# Render the more/less menu
|
|
82
|
+
markdownPrint( \
|
|
83
|
+
'', \
|
|
84
|
+
if(helloCount <= 1, 'Less', '[Less](#var.vCount=' + (helloCount - 1) + ')') + ' | ', \
|
|
85
|
+
'[More](#var.vCount=' + (helloCount + 1) + ')' \
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
# Render many hellos
|
|
89
|
+
ixHello = 0
|
|
90
|
+
helloLoop:
|
|
91
|
+
# Render the hello title
|
|
92
|
+
helloTitle = 'Hello #' + (ixHello + 1)
|
|
93
|
+
markdownPrint('', '## ' + helloTitle)
|
|
94
|
+
|
|
95
|
+
# Render the hello drawing
|
|
96
|
+
setDrawingSize(width, height)
|
|
97
|
+
drawStyle(borderColor, borderSize)
|
|
98
|
+
drawRect(0.5 * borderSize, 0.5 * borderSize, width - borderSize, height - borderSize)
|
|
99
|
+
drawTextStyle(0.67 * height, null, true)
|
|
100
|
+
drawText(helloTitle, 0.5 * width, 0.55 * height)
|
|
101
|
+
|
|
102
|
+
ixHello = ixHello + 1
|
|
103
|
+
jumpif (ixHello < helloCount) helloLoop
|
|
104
|
+
~~~
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Click here to [see the example in action](https://craigahobbs.github.io/markdown-up/#url=DynamicMarkdownExample.md).
|
|
108
|
+
|
|
56
109
|
|
|
57
|
-
|
|
110
|
+
### Links
|
|
58
111
|
|
|
59
|
-
-
|
|
112
|
+
- [Dynamic Markdown Examples](https://craigahobbs.github.io/#url=MarkdownUpApplications.md)
|
|
113
|
+
- [The CalcScript Language](https://craigahobbs.github.io/calc-script/language/)
|
|
114
|
+
- [The CalcScript Library](https://craigahobbs.github.io/calc-script/library/)
|
|
115
|
+
- [The MarkdownUp Library](https://craigahobbs.github.io/markdown-up/library/)
|
|
60
116
|
|
|
61
|
-
- **lineHeight** - The default line height, in em
|
|
62
117
|
|
|
63
|
-
|
|
118
|
+
## The MarkdownUp Package
|
|
64
119
|
|
|
65
|
-
-
|
|
120
|
+
The [markdown-up package](https://www.npmjs.com/package/markdown-up) contains functions for parsing
|
|
121
|
+
CSV, manipulating data, and rendering tables and charts. For more information, refer to the
|
|
122
|
+
[MarkdownUp package documentation](https://craigahobbs.github.io/markdown-up/doc/).
|
|
66
123
|
|
|
67
124
|
|
|
68
125
|
## Development
|
package/lib/app.js
CHANGED
|
@@ -7,21 +7,19 @@ import {MarkdownScriptRuntime, markdownScriptCodeBlock} from './script.js';
|
|
|
7
7
|
import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
|
|
8
8
|
import {getBaseURL, isRelativeURL, markdownElementsAsync} from 'markdown-model/lib/elements.js';
|
|
9
9
|
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
10
|
-
import {SchemaMarkdownParser} from 'schema-markdown/lib/parser.js';
|
|
11
|
-
import {barChartCodeBlock} from './barChart.js';
|
|
12
|
-
import {dataTableCodeBlock} from './dataTable.js';
|
|
13
10
|
import {evaluateExpression} from 'calc-script/lib/runtime.js';
|
|
14
|
-
import {
|
|
11
|
+
import {markdownScriptFunctions} from './scriptLibrary.js';
|
|
15
12
|
import {parseExpression} from 'calc-script/lib/parser.js';
|
|
13
|
+
import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
|
|
16
14
|
import {renderElements} from 'element-model/lib/elementModel.js';
|
|
17
15
|
import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
|
|
18
16
|
import {validateType} from 'schema-markdown/lib/schema.js';
|
|
19
17
|
|
|
20
18
|
|
|
21
19
|
// The application's hash parameter type model
|
|
22
|
-
const
|
|
20
|
+
const markdownUpTypes = parseSchemaMarkdown(`\
|
|
23
21
|
#
|
|
24
|
-
# [MarkdownUp](https://craigahobbs.github.io/markdown-up/) is a Markdown viewer
|
|
22
|
+
# [MarkdownUp](https://craigahobbs.github.io/markdown-up/) is a Markdown viewer.
|
|
25
23
|
#
|
|
26
24
|
# ## Hash Parameters
|
|
27
25
|
#
|
|
@@ -59,40 +57,30 @@ union Command
|
|
|
59
57
|
|
|
60
58
|
# Show the resource's Markdown text
|
|
61
59
|
int(== 1) markdown
|
|
62
|
-
|
|
63
|
-
const markdownUpTypes = new SchemaMarkdownParser(markdownUpTypesSmd).types;
|
|
60
|
+
`);
|
|
64
61
|
|
|
65
62
|
|
|
66
63
|
/**
|
|
67
64
|
* The MarkdownUp application options
|
|
68
65
|
*
|
|
69
66
|
* @typedef {Object} MarkdownUpOptions
|
|
70
|
-
* @
|
|
71
|
-
* @
|
|
72
|
-
* @
|
|
73
|
-
* @
|
|
74
|
-
* @
|
|
67
|
+
* @property {number} [fontSize = 12] - The font size, in points
|
|
68
|
+
* @property {number} [lineHeight = 1.2] - The line height, in em
|
|
69
|
+
* @property {?string} [markdownText = null] - The default Markdown text
|
|
70
|
+
* @property {boolean} [menu = true] - If true, show the menu
|
|
71
|
+
* @property {string} [url = 'README.md'] - The default resource URL
|
|
75
72
|
*/
|
|
76
73
|
|
|
77
74
|
|
|
78
75
|
/**
|
|
79
|
-
* MarkdownUp
|
|
80
|
-
*
|
|
81
|
-
* @property {Object} window - The web browser window object
|
|
82
|
-
* @property {?Object} params - The validated hash parameters object
|
|
83
|
-
* @property {number} fontSize - The font size, in points
|
|
84
|
-
* @property {number} lineHeight - The line height, in em
|
|
85
|
-
* @property {string} markdownText - The markdown text
|
|
86
|
-
* @property {boolean} menu - If true, show the menu
|
|
87
|
-
* @property {string} url - The resource URL
|
|
88
|
-
* @property {?number} runtimeTimeoutId - The runtime-set timeout ID
|
|
76
|
+
* The MarkdownUp application
|
|
89
77
|
*/
|
|
90
78
|
export class MarkdownUp {
|
|
91
79
|
/**
|
|
92
80
|
* Create an application instance
|
|
93
81
|
*
|
|
94
82
|
* @param {Object} window - The web browser window object
|
|
95
|
-
* @
|
|
83
|
+
* @param {Object} [options] - The [application options]{@link module:lib/app~MarkdownUpOptions}
|
|
96
84
|
*/
|
|
97
85
|
constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
|
|
98
86
|
this.window = window;
|
|
@@ -108,8 +96,7 @@ export class MarkdownUp {
|
|
|
108
96
|
|
|
109
97
|
|
|
110
98
|
/**
|
|
111
|
-
* Run the application
|
|
112
|
-
* re-render on any hash parameter change.
|
|
99
|
+
* Run the application
|
|
113
100
|
*/
|
|
114
101
|
async run() {
|
|
115
102
|
await this.render();
|
|
@@ -117,22 +104,7 @@ export class MarkdownUp {
|
|
|
117
104
|
}
|
|
118
105
|
|
|
119
106
|
|
|
120
|
-
/**
|
|
121
|
-
* Render the application
|
|
122
|
-
*/
|
|
123
107
|
async render() {
|
|
124
|
-
// Clear the runtime timeout ID, if one is set
|
|
125
|
-
if (this.runtimeTimeoutId !== null) {
|
|
126
|
-
this.window.clearTimeout(this.runtimeTimeoutId);
|
|
127
|
-
this.runtimeTimeoutId = null;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Clear the window resize event handler
|
|
131
|
-
if (this.runtimeWindowResize !== null) {
|
|
132
|
-
this.window.removeEventListener('resize', this.runtimeWindowResize);
|
|
133
|
-
this.runtimeWindowResize = null;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
108
|
// Parse the hash parameters and render the application element model
|
|
137
109
|
let result;
|
|
138
110
|
let isError = false;
|
|
@@ -145,14 +117,28 @@ export class MarkdownUp {
|
|
|
145
117
|
if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
|
|
146
118
|
return;
|
|
147
119
|
}
|
|
148
|
-
|
|
149
|
-
// Call the application main and validate the result
|
|
150
|
-
result = await this.main();
|
|
151
120
|
} catch ({message}) {
|
|
152
|
-
result = {
|
|
121
|
+
result = {
|
|
122
|
+
'title': 'MarkdownUp',
|
|
123
|
+
'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}
|
|
124
|
+
};
|
|
153
125
|
isError = true;
|
|
154
126
|
}
|
|
155
127
|
|
|
128
|
+
// Call the application main and validate the result
|
|
129
|
+
this.clearRuntimeCallbacks();
|
|
130
|
+
if (!isError) {
|
|
131
|
+
result = await this.main();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Set the font size
|
|
135
|
+
const fontSize = (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
|
|
136
|
+
this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
|
|
137
|
+
|
|
138
|
+
// Set the line height
|
|
139
|
+
const lineHeight = (this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight);
|
|
140
|
+
this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
|
|
141
|
+
|
|
156
142
|
// Set the window title
|
|
157
143
|
const title = result.title ?? null;
|
|
158
144
|
if (title !== null) {
|
|
@@ -170,31 +156,16 @@ export class MarkdownUp {
|
|
|
170
156
|
this.window.addEventListener('resize', result.resize);
|
|
171
157
|
}
|
|
172
158
|
|
|
173
|
-
// Set the font size
|
|
174
|
-
const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
|
|
175
|
-
this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
|
|
176
|
-
|
|
177
|
-
// Set the line height
|
|
178
|
-
const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
|
|
179
|
-
this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
|
|
180
|
-
|
|
181
159
|
// Render the element model
|
|
182
160
|
renderElements(this.window.document.body, result.elements);
|
|
183
161
|
|
|
184
162
|
// Focus?
|
|
185
163
|
if ('focus' in result) {
|
|
186
|
-
|
|
187
|
-
if (element !== null) {
|
|
188
|
-
element.focus();
|
|
189
|
-
if ('selectionStart' in element) {
|
|
190
|
-
element.selectionStart = element.value.length;
|
|
191
|
-
element.selectionEnd = element.value.length;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
164
|
+
this.setDocumentFocus(result.focus);
|
|
194
165
|
}
|
|
195
166
|
|
|
196
167
|
// Navigate?
|
|
197
|
-
// Note: This is done after render since it may have no effect (in which case we
|
|
168
|
+
// Note: This is done after render since it may have no effect (in which case we need to render)
|
|
198
169
|
if ('location' in result && result.location !== null) {
|
|
199
170
|
this.window.location.href = result.location;
|
|
200
171
|
return;
|
|
@@ -208,11 +179,6 @@ export class MarkdownUp {
|
|
|
208
179
|
}
|
|
209
180
|
|
|
210
181
|
|
|
211
|
-
/**
|
|
212
|
-
* Parse and validate the hash parameters
|
|
213
|
-
*
|
|
214
|
-
* @param {?string} paramString - Optional parameter string for testing
|
|
215
|
-
*/
|
|
216
182
|
updateParams(paramString = null) {
|
|
217
183
|
// Clear, then validate the hash parameters (may throw)
|
|
218
184
|
this.params = null;
|
|
@@ -225,11 +191,33 @@ export class MarkdownUp {
|
|
|
225
191
|
}
|
|
226
192
|
|
|
227
193
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
194
|
+
clearRuntimeCallbacks(clearResize = true) {
|
|
195
|
+
// Clear the runtime timeout ID, if one is set
|
|
196
|
+
if (this.runtimeTimeoutId !== null) {
|
|
197
|
+
this.window.clearTimeout(this.runtimeTimeoutId);
|
|
198
|
+
this.runtimeTimeoutId = null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Clear the window resize event handler
|
|
202
|
+
if (clearResize && this.runtimeWindowResize !== null) {
|
|
203
|
+
this.window.removeEventListener('resize', this.runtimeWindowResize);
|
|
204
|
+
this.runtimeWindowResize = null;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
setDocumentFocus(focusId) {
|
|
210
|
+
const element = this.window.document.getElementById(focusId);
|
|
211
|
+
if (element !== null) {
|
|
212
|
+
element.focus();
|
|
213
|
+
if ('selectionStart' in element) {
|
|
214
|
+
element.selectionStart = element.value.length;
|
|
215
|
+
element.selectionEnd = element.value.length;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
233
221
|
async main() {
|
|
234
222
|
// Help?
|
|
235
223
|
if ('cmd' in this.params && 'help' in this.params.cmd) {
|
|
@@ -291,9 +279,6 @@ export class MarkdownUp {
|
|
|
291
279
|
// Render the markdown
|
|
292
280
|
await markdownElementsAsync(markdownModel, {
|
|
293
281
|
'codeBlocks': {
|
|
294
|
-
'bar-chart': (codeBlock) => barChartCodeBlock(codeBlock, scriptOptions),
|
|
295
|
-
'data-table': (codeBlock) => dataTableCodeBlock(codeBlock, scriptOptions),
|
|
296
|
-
'line-chart': (codeBlock) => lineChartCodeBlock(codeBlock, scriptOptions),
|
|
297
282
|
'markdown-script': (codeBlock) => markdownScriptCodeBlock(codeBlock, scriptOptions)
|
|
298
283
|
},
|
|
299
284
|
'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
|
|
@@ -306,32 +291,34 @@ export class MarkdownUp {
|
|
|
306
291
|
};
|
|
307
292
|
|
|
308
293
|
// Set any runtime side-effects
|
|
309
|
-
if (scriptOptions.runtime.windowLocation !== null) {
|
|
310
|
-
result.location = scriptOptions.runtime.windowLocation;
|
|
311
|
-
}
|
|
312
294
|
if (scriptOptions.runtime.documentFocus !== null) {
|
|
313
295
|
result.focus = scriptOptions.runtime.documentFocus;
|
|
314
296
|
}
|
|
315
297
|
if (scriptOptions.runtime.documentTitle !== null) {
|
|
316
298
|
result.title = scriptOptions.runtime.documentTitle;
|
|
317
299
|
}
|
|
318
|
-
if (scriptOptions.runtime.
|
|
319
|
-
result.
|
|
300
|
+
if (scriptOptions.runtime.windowLocation !== null) {
|
|
301
|
+
result.location = scriptOptions.runtime.windowLocation;
|
|
320
302
|
}
|
|
321
303
|
if (scriptOptions.runtime.windowResize !== null) {
|
|
322
304
|
result.resize = scriptOptions.runtime.windowResize;
|
|
323
305
|
}
|
|
306
|
+
if (scriptOptions.runtime.windowTimeout !== null) {
|
|
307
|
+
result.timeout = scriptOptions.runtime.windowTimeout;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Reset the runtime
|
|
311
|
+
scriptOptions.runtime.reset();
|
|
324
312
|
|
|
325
313
|
return result;
|
|
326
314
|
}
|
|
327
315
|
|
|
328
316
|
|
|
329
317
|
createScriptOptions(resourceURL) {
|
|
330
|
-
// Create the script options object
|
|
331
318
|
const scriptOptions = {
|
|
332
319
|
// eslint-disable-next-line require-await
|
|
333
|
-
'fetchFn': async (fetchURL,
|
|
334
|
-
'fontSize': this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize,
|
|
320
|
+
'fetchFn': async (fetchURL, options) => this.window.fetch(fetchURL, options),
|
|
321
|
+
'fontSize': (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize),
|
|
335
322
|
'params': this.params,
|
|
336
323
|
'urlFn': (url) => this.modifyURL(url, resourceURL),
|
|
337
324
|
'window': this.window
|
|
@@ -355,6 +342,7 @@ export class MarkdownUp {
|
|
|
355
342
|
|
|
356
343
|
// Create the markdown-script runtime
|
|
357
344
|
const runtime = new MarkdownScriptRuntime(scriptOptions);
|
|
345
|
+
scriptOptions.globals = {...markdownScriptFunctions};
|
|
358
346
|
scriptOptions.runtime = runtime;
|
|
359
347
|
scriptOptions.runtimeUpdateFn = () => {
|
|
360
348
|
// Did script set the document title?
|
|
@@ -363,38 +351,22 @@ export class MarkdownUp {
|
|
|
363
351
|
}
|
|
364
352
|
|
|
365
353
|
// Did the script set a timeout?
|
|
354
|
+
this.clearRuntimeCallbacks(false);
|
|
366
355
|
if (runtime.windowTimeout !== null) {
|
|
367
|
-
if (this.runtimeTimeoutId !== null) {
|
|
368
|
-
this.window.clearTimeout(this.runtimeTimeoutId);
|
|
369
|
-
}
|
|
370
356
|
this.runtimeTimeoutId = this.window.setTimeout(...runtime.windowTimeout);
|
|
371
357
|
}
|
|
372
358
|
|
|
373
|
-
// Reset the runtime
|
|
374
|
-
const {isDocumentReset} = runtime;
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
renderElements(
|
|
380
|
-
this.window.document.body,
|
|
381
|
-
[
|
|
382
|
-
documentElements,
|
|
383
|
-
isDocumentReset ? this.burgerElements() : null
|
|
384
|
-
],
|
|
385
|
-
isDocumentReset
|
|
386
|
-
);
|
|
359
|
+
// Reset the runtime and render
|
|
360
|
+
const {isDocumentReset, windowLocation} = runtime;
|
|
361
|
+
const documentElements = [
|
|
362
|
+
runtime.resetElements(),
|
|
363
|
+
isDocumentReset ? this.burgerElements() : null
|
|
364
|
+
];
|
|
365
|
+
renderElements(this.window.document.body, documentElements, isDocumentReset);
|
|
387
366
|
|
|
388
367
|
// Focus?
|
|
389
368
|
if (runtime.documentFocus !== null) {
|
|
390
|
-
|
|
391
|
-
if (element !== null) {
|
|
392
|
-
element.focus();
|
|
393
|
-
if ('selectionStart' in element) {
|
|
394
|
-
element.selectionStart = element.value.length;
|
|
395
|
-
element.selectionEnd = element.value.length;
|
|
396
|
-
}
|
|
397
|
-
}
|
|
369
|
+
this.setDocumentFocus(runtime.documentFocus);
|
|
398
370
|
}
|
|
399
371
|
|
|
400
372
|
// Navigate?
|
|
@@ -402,6 +374,9 @@ export class MarkdownUp {
|
|
|
402
374
|
if (windowLocation !== null) {
|
|
403
375
|
this.window.location.href = windowLocation;
|
|
404
376
|
}
|
|
377
|
+
|
|
378
|
+
// Reset the runtime
|
|
379
|
+
runtime.reset();
|
|
405
380
|
};
|
|
406
381
|
|
|
407
382
|
return scriptOptions;
|