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/README.md +46 -27
- package/lib/app.js +333 -227
- package/lib/barChart.js +8 -6
- package/lib/csv.js +9 -0
- package/lib/data.js +44 -41
- package/lib/dataTable.js +8 -6
- package/lib/lineChart.js +10 -7
- package/lib/model.js +5 -11
- package/lib/script.js +329 -251
- package/lib/scriptLibrary.js +441 -0
- package/lib/util.js +8 -39
- package/package.json +9 -9
package/lib/app.js
CHANGED
|
@@ -12,6 +12,7 @@ import {barChartCodeBlock} from './barChart.js';
|
|
|
12
12
|
import {dataTableCodeBlock} from './dataTable.js';
|
|
13
13
|
import {evaluateExpression} from 'calc-script/lib/runtime.js';
|
|
14
14
|
import {lineChartCodeBlock} from './lineChart.js';
|
|
15
|
+
import {markdownScriptFunctions} from './scriptLibrary.js';
|
|
15
16
|
import {parseExpression} from 'calc-script/lib/parser.js';
|
|
16
17
|
import {renderElements} from 'element-model/lib/elementModel.js';
|
|
17
18
|
import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
|
|
@@ -21,13 +22,11 @@ import {validateType} from 'schema-markdown/lib/schema.js';
|
|
|
21
22
|
// The application's hash parameter type model
|
|
22
23
|
const markdownUpTypesSmd = `\
|
|
23
24
|
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
# [markdown-up](https://github.com/craigahobbs/markdown-up#readme)
|
|
25
|
+
# [MarkdownUp](https://craigahobbs.github.io/markdown-up/) is a Markdown viewer.
|
|
27
26
|
#
|
|
28
27
|
# ## Hash Parameters
|
|
29
28
|
#
|
|
30
|
-
#
|
|
29
|
+
# MarkdownUp recognizes the following hash parameters:
|
|
31
30
|
#
|
|
32
31
|
struct MarkdownUp
|
|
33
32
|
|
|
@@ -37,7 +36,7 @@ struct MarkdownUp
|
|
|
37
36
|
# Optional command
|
|
38
37
|
optional Command cmd
|
|
39
38
|
|
|
40
|
-
# The menu visibility (default is
|
|
39
|
+
# The menu visibility (default is collapsed)
|
|
41
40
|
optional int(== 1) menu
|
|
42
41
|
|
|
43
42
|
# The font size
|
|
@@ -53,48 +52,49 @@ struct MarkdownUp
|
|
|
53
52
|
optional int(== 1) debug
|
|
54
53
|
|
|
55
54
|
|
|
56
|
-
#
|
|
55
|
+
# A MarkdownUp application command
|
|
57
56
|
union Command
|
|
58
57
|
|
|
59
|
-
# Display the
|
|
58
|
+
# Display the hash parameters documentation
|
|
60
59
|
int(== 1) help
|
|
61
60
|
|
|
62
|
-
# Show the
|
|
61
|
+
# Show the resource's Markdown text
|
|
63
62
|
int(== 1) markdown
|
|
64
63
|
`;
|
|
65
64
|
const markdownUpTypes = new SchemaMarkdownParser(markdownUpTypesSmd).types;
|
|
66
65
|
|
|
67
66
|
|
|
68
67
|
/**
|
|
69
|
-
*
|
|
68
|
+
* The MarkdownUp application options
|
|
70
69
|
*
|
|
71
70
|
* @typedef {Object} MarkdownUpOptions
|
|
72
|
-
* @
|
|
73
|
-
* @
|
|
74
|
-
* @
|
|
75
|
-
* @
|
|
76
|
-
* @
|
|
71
|
+
* @property {number} [fontSize = 12] - The font size, in points
|
|
72
|
+
* @property {number} [lineHeight = 1.2] - The line height, in em
|
|
73
|
+
* @property {?string} [markdownText = null] - The default Markdown text
|
|
74
|
+
* @property {boolean} [menu = true] - If true, show the menu
|
|
75
|
+
* @property {string} [url = 'README.md'] - The default resource URL
|
|
77
76
|
*/
|
|
78
77
|
|
|
79
78
|
|
|
80
79
|
/**
|
|
81
|
-
* MarkdownUp
|
|
80
|
+
* The MarkdownUp application
|
|
82
81
|
*
|
|
83
82
|
* @property {Object} window - The web browser window object
|
|
84
83
|
* @property {?Object} params - The validated hash parameters object
|
|
85
84
|
* @property {number} fontSize - The font size, in points
|
|
86
85
|
* @property {number} lineHeight - The line height, in em
|
|
87
|
-
* @property {string} markdownText - The
|
|
86
|
+
* @property {?string} markdownText - The default Markdown text
|
|
88
87
|
* @property {boolean} menu - If true, show the menu
|
|
89
|
-
* @property {string} url - The resource URL
|
|
90
|
-
* @property {?
|
|
88
|
+
* @property {string} url - The default resource URL
|
|
89
|
+
* @property {?function} runtimeWindowResize - The runtime-set window resize event listener function
|
|
90
|
+
* @property {?number} runtimeTimeoutId - The runtime-set timeout ID
|
|
91
91
|
*/
|
|
92
92
|
export class MarkdownUp {
|
|
93
93
|
/**
|
|
94
94
|
* Create an application instance
|
|
95
95
|
*
|
|
96
96
|
* @param {Object} window - The web browser window object
|
|
97
|
-
* @
|
|
97
|
+
* @param {Object} [options] - The [application options]{@link module:lib/app~MarkdownUpOptions}
|
|
98
98
|
*/
|
|
99
99
|
constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
|
|
100
100
|
this.window = window;
|
|
@@ -104,28 +104,25 @@ export class MarkdownUp {
|
|
|
104
104
|
this.markdownText = markdownText;
|
|
105
105
|
this.menu = menu;
|
|
106
106
|
this.url = url;
|
|
107
|
-
this.
|
|
107
|
+
this.runtimeWindowResize = null;
|
|
108
|
+
this.runtimeTimeoutId = null;
|
|
108
109
|
}
|
|
109
110
|
|
|
111
|
+
|
|
110
112
|
/**
|
|
111
|
-
* Run the application. This method calls render and subscribes to
|
|
112
|
-
* re-render
|
|
113
|
+
* Run the application. This method calls the render method and subscribes to hash parameter
|
|
114
|
+
* changes to re-render.
|
|
113
115
|
*/
|
|
114
116
|
async run() {
|
|
115
117
|
await this.render();
|
|
116
118
|
this.window.addEventListener('hashchange', () => this.render(), false);
|
|
117
119
|
}
|
|
118
120
|
|
|
121
|
+
|
|
119
122
|
/**
|
|
120
|
-
*
|
|
123
|
+
* Render the application
|
|
121
124
|
*/
|
|
122
125
|
async render() {
|
|
123
|
-
// Clear the navigate timeout ID, if one is set
|
|
124
|
-
if (this.setNavigateTimeoutId !== null) {
|
|
125
|
-
this.window.clearTimeout(this.setNavigateTimeoutId);
|
|
126
|
-
this.setNavigateTimeoutId = null;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
126
|
// Parse the hash parameters and render the application element model
|
|
130
127
|
let result;
|
|
131
128
|
let isError = false;
|
|
@@ -138,37 +135,60 @@ export class MarkdownUp {
|
|
|
138
135
|
if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
|
|
139
136
|
return;
|
|
140
137
|
}
|
|
141
|
-
|
|
142
|
-
// Call the application main and validate the result
|
|
143
|
-
result = await this.main();
|
|
144
138
|
} catch ({message}) {
|
|
145
|
-
result = {
|
|
139
|
+
result = {
|
|
140
|
+
'title': 'MarkdownUp',
|
|
141
|
+
'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}
|
|
142
|
+
};
|
|
146
143
|
isError = true;
|
|
147
144
|
}
|
|
148
145
|
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
146
|
+
// Call the application main and validate the result
|
|
147
|
+
this.clearRuntimeCallbacks();
|
|
148
|
+
if (!isError) {
|
|
149
|
+
result = await this.main();
|
|
153
150
|
}
|
|
154
151
|
|
|
152
|
+
// Set the font size
|
|
153
|
+
const fontSize = (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
|
|
154
|
+
this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
|
|
155
|
+
|
|
156
|
+
// Set the line height
|
|
157
|
+
const lineHeight = (this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight);
|
|
158
|
+
this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
|
|
159
|
+
|
|
155
160
|
// Set the window title
|
|
156
161
|
const title = result.title ?? null;
|
|
157
162
|
if (title !== null) {
|
|
158
163
|
this.window.document.title = title;
|
|
159
164
|
}
|
|
160
165
|
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
|
|
166
|
+
// Timeout?
|
|
167
|
+
if ('timeout' in result) {
|
|
168
|
+
this.runtimeTimeoutId = this.window.setTimeout(...result.timeout);
|
|
169
|
+
}
|
|
164
170
|
|
|
165
|
-
//
|
|
166
|
-
|
|
167
|
-
|
|
171
|
+
// Window resize event handler?
|
|
172
|
+
if ('resize' in result) {
|
|
173
|
+
this.runtimeWindowResize = result.resize;
|
|
174
|
+
this.window.addEventListener('resize', result.resize);
|
|
175
|
+
}
|
|
168
176
|
|
|
169
177
|
// Render the element model
|
|
170
178
|
renderElements(this.window.document.body, result.elements);
|
|
171
179
|
|
|
180
|
+
// Focus?
|
|
181
|
+
if ('focus' in result) {
|
|
182
|
+
this.setDocumentFocus(result.focus);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Navigate?
|
|
186
|
+
// Note: This is done after render since it may have no effect (in which case we need to render)
|
|
187
|
+
if ('location' in result && result.location !== null) {
|
|
188
|
+
this.window.location.href = result.location;
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
172
192
|
// If there is a URL hash ID, re-navigate to go there since it was just rendered. After the
|
|
173
193
|
// first render, re-render is short-circuited by the unchanged hash param check above.
|
|
174
194
|
if (!isError && getHashID(this.window.location.hash) !== null) {
|
|
@@ -176,10 +196,11 @@ export class MarkdownUp {
|
|
|
176
196
|
}
|
|
177
197
|
}
|
|
178
198
|
|
|
199
|
+
|
|
179
200
|
/**
|
|
180
201
|
* Parse and validate the hash parameters
|
|
181
202
|
*
|
|
182
|
-
* @param {?string} paramString - Optional parameter string for testing
|
|
203
|
+
* @param {?string} [paramString = null] - Optional parameter string for testing
|
|
183
204
|
*/
|
|
184
205
|
updateParams(paramString = null) {
|
|
185
206
|
// Clear, then validate the hash parameters (may throw)
|
|
@@ -192,139 +213,156 @@ export class MarkdownUp {
|
|
|
192
213
|
this.params = validateType(markdownUpTypes, 'MarkdownUp', params);
|
|
193
214
|
}
|
|
194
215
|
|
|
216
|
+
|
|
217
|
+
clearRuntimeCallbacks(clearResize = true) {
|
|
218
|
+
// Clear the runtime timeout ID, if one is set
|
|
219
|
+
if (this.runtimeTimeoutId !== null) {
|
|
220
|
+
this.window.clearTimeout(this.runtimeTimeoutId);
|
|
221
|
+
this.runtimeTimeoutId = null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Clear the window resize event handler
|
|
225
|
+
if (clearResize && this.runtimeWindowResize !== null) {
|
|
226
|
+
this.window.removeEventListener('resize', this.runtimeWindowResize);
|
|
227
|
+
this.runtimeWindowResize = null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
setDocumentFocus(focusId) {
|
|
233
|
+
const element = this.window.document.getElementById(focusId);
|
|
234
|
+
if (element !== null) {
|
|
235
|
+
element.focus();
|
|
236
|
+
if ('selectionStart' in element) {
|
|
237
|
+
element.selectionStart = element.value.length;
|
|
238
|
+
element.selectionEnd = element.value.length;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
195
244
|
/**
|
|
196
|
-
* The
|
|
197
|
-
* https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#main}.
|
|
245
|
+
* The application's main entry point
|
|
198
246
|
*
|
|
199
|
-
* @
|
|
200
|
-
* @returns {Object} [MainResult]{@link https://craigahobbs.github.io/element-app/module-lib_app.html#~MainResult}
|
|
247
|
+
* @returns {Object}
|
|
201
248
|
*/
|
|
202
249
|
async main() {
|
|
203
|
-
//
|
|
250
|
+
// Help?
|
|
251
|
+
if ('cmd' in this.params && 'help' in this.params.cmd) {
|
|
252
|
+
return {
|
|
253
|
+
'title': 'MarkdownUp',
|
|
254
|
+
'elements': [
|
|
255
|
+
schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)}),
|
|
256
|
+
this.burgerElements()
|
|
257
|
+
]
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Get the Markdown text
|
|
204
262
|
const urlOverride = 'url' in this.params && this.params.url !== '';
|
|
205
263
|
const url = urlOverride ? this.params.url : this.url;
|
|
206
264
|
let markdownText = null;
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
265
|
+
if (this.markdownText !== null && !urlOverride) {
|
|
266
|
+
({markdownText} = this);
|
|
267
|
+
} else {
|
|
268
|
+
// Fetch the Markdown text resource URL
|
|
269
|
+
const response = await this.window.fetch(url);
|
|
270
|
+
if (!response.ok) {
|
|
271
|
+
const status = response.statusText;
|
|
272
|
+
return {
|
|
273
|
+
'title': 'MarkdownUp',
|
|
274
|
+
'elements': {
|
|
275
|
+
'html': 'p',
|
|
276
|
+
'elem': {'text': `Error: Could not fetch "${url}"${status === '' ? '' : ` - ${JSON.stringify(status)}`}`}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
219
279
|
}
|
|
220
|
-
|
|
221
|
-
markdownTitle = getMarkdownTitle(markdownModel);
|
|
280
|
+
markdownText = await response.text();
|
|
222
281
|
}
|
|
223
282
|
|
|
224
|
-
//
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
283
|
+
// Parse the Markdown and get the title
|
|
284
|
+
const markdownModel = parseMarkdown(markdownText);
|
|
285
|
+
const markdownTitle = getMarkdownTitle(markdownModel);
|
|
286
|
+
|
|
287
|
+
// Display the Markdown?
|
|
288
|
+
if ('cmd' in this.params && 'markdown' in this.params.cmd) {
|
|
289
|
+
return {
|
|
290
|
+
'title': markdownTitle,
|
|
291
|
+
'elements': [
|
|
292
|
+
{'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}},
|
|
293
|
+
this.burgerElements()
|
|
294
|
+
]
|
|
295
|
+
};
|
|
235
296
|
}
|
|
236
297
|
|
|
237
|
-
//
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
valueNew = Math.max(attr.gte, Math.min(attr.lte, valueNew));
|
|
246
|
-
const params = {...this.params};
|
|
247
|
-
params[paramName] = fixed === null ? `${valueNew}` : `${valueNew.toFixed(fixed)}`;
|
|
248
|
-
return `#${encodeQueryString(params)}`;
|
|
249
|
-
};
|
|
298
|
+
// Render the Markdown
|
|
299
|
+
const scriptOptions = this.createScriptOptions(url);
|
|
300
|
+
const result = {
|
|
301
|
+
'title': markdownTitle,
|
|
302
|
+
'elements': [
|
|
303
|
+
// Add a top hash ID, if necessary
|
|
304
|
+
Object.keys(this.params).length === 0
|
|
305
|
+
? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
|
|
250
306
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
307
|
+
// Render the markdown
|
|
308
|
+
await markdownElementsAsync(markdownModel, {
|
|
309
|
+
'codeBlocks': {
|
|
310
|
+
'bar-chart': (codeBlock) => barChartCodeBlock(codeBlock, scriptOptions),
|
|
311
|
+
'data-table': (codeBlock) => dataTableCodeBlock(codeBlock, scriptOptions),
|
|
312
|
+
'line-chart': (codeBlock) => lineChartCodeBlock(codeBlock, scriptOptions),
|
|
313
|
+
'markdown-script': (codeBlock) => markdownScriptCodeBlock(codeBlock, scriptOptions)
|
|
314
|
+
},
|
|
315
|
+
'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
|
|
316
|
+
'headerIds': true
|
|
317
|
+
}),
|
|
262
318
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (!(name in this.params)) {
|
|
267
|
-
params[name] = value;
|
|
268
|
-
} else {
|
|
269
|
-
delete params[name];
|
|
270
|
-
}
|
|
271
|
-
return `#${encodeQueryString(params)}`;
|
|
319
|
+
// Popup menu burger
|
|
320
|
+
this.burgerElements()
|
|
321
|
+
]
|
|
272
322
|
};
|
|
273
323
|
|
|
274
|
-
//
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
return `${getBaseURL(url)}${rawURL}`;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return rawURL;
|
|
294
|
-
};
|
|
324
|
+
// Set any runtime side-effects
|
|
325
|
+
if (scriptOptions.runtime.documentFocus !== null) {
|
|
326
|
+
result.focus = scriptOptions.runtime.documentFocus;
|
|
327
|
+
}
|
|
328
|
+
if (scriptOptions.runtime.documentTitle !== null) {
|
|
329
|
+
result.title = scriptOptions.runtime.documentTitle;
|
|
330
|
+
}
|
|
331
|
+
if (scriptOptions.runtime.windowLocation !== null) {
|
|
332
|
+
result.location = scriptOptions.runtime.windowLocation;
|
|
333
|
+
}
|
|
334
|
+
if (scriptOptions.runtime.windowResize !== null) {
|
|
335
|
+
result.resize = scriptOptions.runtime.windowResize;
|
|
336
|
+
}
|
|
337
|
+
if (scriptOptions.runtime.windowTimeout !== null) {
|
|
338
|
+
result.timeout = scriptOptions.runtime.windowTimeout;
|
|
339
|
+
}
|
|
295
340
|
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
if ('debug' in this.params) {
|
|
299
|
-
this.window.console.log(text);
|
|
300
|
-
}
|
|
301
|
-
};
|
|
341
|
+
// Reset the runtime
|
|
342
|
+
scriptOptions.runtime.reset();
|
|
302
343
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const fetchFn = async (fetchURL, init) => this.window.fetch(urlFn(fetchURL), init);
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
306
346
|
|
|
307
|
-
// The navigate timeout function
|
|
308
|
-
const navigateTimeoutFn = (navigateURL, delay) => {
|
|
309
|
-
const timeoutCallback = () => {
|
|
310
|
-
this.window.location.href = urlFn(navigateURL);
|
|
311
|
-
};
|
|
312
|
-
this.setNavigateTimeoutId = this.window.setTimeout(timeoutCallback, delay);
|
|
313
|
-
};
|
|
314
|
-
// eslint-disable-next-line lines-around-comment
|
|
315
347
|
|
|
316
|
-
|
|
317
|
-
const fontSize = 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
|
|
348
|
+
createScriptOptions(resourceURL) {
|
|
318
349
|
const scriptOptions = {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
'
|
|
322
|
-
logFn,
|
|
323
|
-
navigateTimeoutFn,
|
|
350
|
+
// eslint-disable-next-line require-await
|
|
351
|
+
'fetchFn': async (fetchURL, options) => this.window.fetch(fetchURL, options),
|
|
352
|
+
'fontSize': (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize),
|
|
324
353
|
'params': this.params,
|
|
325
|
-
'
|
|
326
|
-
|
|
354
|
+
'urlFn': (url) => this.modifyURL(url, resourceURL),
|
|
355
|
+
'window': this.window
|
|
327
356
|
};
|
|
357
|
+
|
|
358
|
+
// Add log function, if debugging
|
|
359
|
+
if ('debug' in this.params) {
|
|
360
|
+
scriptOptions.logFn = (text) => {
|
|
361
|
+
this.window.console.log(text);
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Add hash parameter variables, if any
|
|
328
366
|
if ('var' in this.params) {
|
|
329
367
|
scriptOptions.variables = {};
|
|
330
368
|
for (const varName of Object.keys(this.params.var)) {
|
|
@@ -332,84 +370,159 @@ export class MarkdownUp {
|
|
|
332
370
|
scriptOptions.variables[varName] = evaluateExpression(varExpr, scriptOptions.variables);
|
|
333
371
|
}
|
|
334
372
|
}
|
|
335
|
-
scriptOptions.runtime = new MarkdownScriptRuntime(scriptOptions);
|
|
336
|
-
const result = {
|
|
337
|
-
'title': markdownTitle,
|
|
338
|
-
'elements': [
|
|
339
|
-
// Command?
|
|
340
|
-
cmdElements !== null ? cmdElements : null,
|
|
341
373
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
374
|
+
// Create the markdown-script runtime
|
|
375
|
+
const runtime = new MarkdownScriptRuntime(scriptOptions);
|
|
376
|
+
scriptOptions.globals = {...markdownScriptFunctions};
|
|
377
|
+
scriptOptions.runtime = runtime;
|
|
378
|
+
scriptOptions.runtimeUpdateFn = () => {
|
|
379
|
+
// Did script set the document title?
|
|
380
|
+
if (runtime.documentTitle !== null) {
|
|
381
|
+
this.window.document.title = runtime.documentTitle;
|
|
382
|
+
}
|
|
345
383
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
'line-chart': (language, lines) => lineChartCodeBlock(language, lines, scriptOptions),
|
|
352
|
-
'markdown-script': (language, lines) => markdownScriptCodeBlock(language, lines, scriptOptions)
|
|
353
|
-
},
|
|
354
|
-
urlFn,
|
|
355
|
-
'headerIds': true
|
|
356
|
-
}),
|
|
384
|
+
// Did the script set a timeout?
|
|
385
|
+
this.clearRuntimeCallbacks(false);
|
|
386
|
+
if (runtime.windowTimeout !== null) {
|
|
387
|
+
this.runtimeTimeoutId = this.window.setTimeout(...runtime.windowTimeout);
|
|
388
|
+
}
|
|
357
389
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
'elem': markdownSVGElements
|
|
380
|
-
},
|
|
381
|
-
{
|
|
382
|
-
'html': 'a',
|
|
383
|
-
'attr': {'href': linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
|
|
384
|
-
'elem': fontSizeSVGElements
|
|
385
|
-
},
|
|
386
|
-
{
|
|
387
|
-
'html': 'a',
|
|
388
|
-
'attr': {'href': linkCycle('lineHeight', this.lineHeight, 0.2, 1), 'aria-label': 'Line height'},
|
|
389
|
-
'elem': lineHeightSVGElements
|
|
390
|
-
},
|
|
391
|
-
{
|
|
392
|
-
'html': 'a',
|
|
393
|
-
'attr': {'href': linkCommandToggle('help', '1'), 'aria-label': 'Help'},
|
|
394
|
-
'elem': helpSVGElements
|
|
395
|
-
}
|
|
396
|
-
]
|
|
397
|
-
}
|
|
398
|
-
]
|
|
390
|
+
// Reset the runtime and render
|
|
391
|
+
const {isDocumentReset, windowLocation} = runtime;
|
|
392
|
+
const documentElements = [
|
|
393
|
+
runtime.resetElements(),
|
|
394
|
+
isDocumentReset ? this.burgerElements() : null
|
|
395
|
+
];
|
|
396
|
+
renderElements(this.window.document.body, documentElements, isDocumentReset);
|
|
397
|
+
|
|
398
|
+
// Focus?
|
|
399
|
+
if (runtime.documentFocus !== null) {
|
|
400
|
+
this.setDocumentFocus(runtime.documentFocus);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Navigate?
|
|
404
|
+
// Note: This is done after render since it may have no effect (in which case we want to render).
|
|
405
|
+
if (windowLocation !== null) {
|
|
406
|
+
this.window.location.href = windowLocation;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// Reset the runtime
|
|
410
|
+
runtime.reset();
|
|
399
411
|
};
|
|
400
412
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
413
|
+
return scriptOptions;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
modifyURL(url, resourceURL) {
|
|
418
|
+
// Hash URL?
|
|
419
|
+
if (url.startsWith('#')) {
|
|
420
|
+
// Fixup the "url" param if its relative
|
|
421
|
+
const hashParams = decodeQueryString(url.slice(1));
|
|
422
|
+
if ('url' in hashParams && hashParams.url !== '' && isRelativeURL(hashParams.url)) {
|
|
423
|
+
hashParams.url = `${getBaseURL(resourceURL)}${hashParams.url}`;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Encode hash parameters with a hash ID
|
|
427
|
+
const paramString = encodeQueryString({...this.params, ...hashParams});
|
|
428
|
+
const hashID = getHashID(url);
|
|
429
|
+
return `#${paramString}${hashID !== null && paramString !== '' ? '&' : ''}${hashID !== null ? hashID : ''}`;
|
|
404
430
|
}
|
|
405
431
|
|
|
406
|
-
|
|
432
|
+
// Relative URL?
|
|
433
|
+
if (isRelativeURL(url)) {
|
|
434
|
+
return `${getBaseURL(resourceURL)}${url}`;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return url;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
burgerElements() {
|
|
442
|
+
return !this.menu ? null : [
|
|
443
|
+
{
|
|
444
|
+
'html': 'div',
|
|
445
|
+
'attr': {'class': 'menu-burger'},
|
|
446
|
+
'elem': [
|
|
447
|
+
{
|
|
448
|
+
'html': 'a',
|
|
449
|
+
'attr': {'href': this.linkValueToggle('menu', '1'), 'aria-label': 'Menu'},
|
|
450
|
+
'elem': menuSVGElements
|
|
451
|
+
}
|
|
452
|
+
]
|
|
453
|
+
},
|
|
454
|
+
|
|
455
|
+
// Popup menu
|
|
456
|
+
!('menu' in this.params) ? null : {
|
|
457
|
+
'html': 'div',
|
|
458
|
+
'attr': {'class': 'menu'},
|
|
459
|
+
'elem': [
|
|
460
|
+
{
|
|
461
|
+
'html': 'a',
|
|
462
|
+
'attr': {'href': this.linkCommandToggle('markdown', '1'), 'aria-label': 'Show Markdown'},
|
|
463
|
+
'elem': markdownSVGElements
|
|
464
|
+
},
|
|
465
|
+
{
|
|
466
|
+
'html': 'a',
|
|
467
|
+
'attr': {'href': this.linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
|
|
468
|
+
'elem': fontSizeSVGElements
|
|
469
|
+
},
|
|
470
|
+
{
|
|
471
|
+
'html': 'a',
|
|
472
|
+
'attr': {'href': this.linkCycle('lineHeight', this.lineHeight, 0.2, 1), 'aria-label': 'Line height'},
|
|
473
|
+
'elem': lineHeightSVGElements
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
'html': 'a',
|
|
477
|
+
'attr': {'href': this.linkCommandToggle('help', '1'), 'aria-label': 'Help'},
|
|
478
|
+
'elem': helpSVGElements
|
|
479
|
+
}
|
|
480
|
+
]
|
|
481
|
+
}
|
|
482
|
+
];
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
linkCycle(paramName, defaultValue, delta, fixed = null) {
|
|
487
|
+
const value = paramName in this.params ? this.params[paramName] : defaultValue;
|
|
488
|
+
const {attr} = markdownUpTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
|
|
489
|
+
let valueNew = value + delta;
|
|
490
|
+
if (valueNew > attr.lte) {
|
|
491
|
+
valueNew = attr.gte;
|
|
492
|
+
}
|
|
493
|
+
valueNew = Math.max(attr.gte, Math.min(attr.lte, valueNew));
|
|
494
|
+
const params = {...this.params};
|
|
495
|
+
params[paramName] = fixed === null ? `${valueNew}` : `${valueNew.toFixed(fixed)}`;
|
|
496
|
+
return `#${encodeQueryString(params)}`;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
linkCommandToggle(command, value) {
|
|
501
|
+
const params = {...this.params};
|
|
502
|
+
if ('cmd' in this.params && command in this.params.cmd) {
|
|
503
|
+
delete params.cmd;
|
|
504
|
+
} else {
|
|
505
|
+
params.cmd = {};
|
|
506
|
+
params.cmd[command] = `${value}`;
|
|
507
|
+
}
|
|
508
|
+
return `#${encodeQueryString(params)}`;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
linkValueToggle(name, value) {
|
|
513
|
+
const params = {...this.params};
|
|
514
|
+
if (!(name in this.params)) {
|
|
515
|
+
params[name] = value;
|
|
516
|
+
} else {
|
|
517
|
+
delete params[name];
|
|
518
|
+
}
|
|
519
|
+
return `#${encodeQueryString(params)}`;
|
|
407
520
|
}
|
|
408
521
|
}
|
|
409
522
|
|
|
410
523
|
|
|
411
524
|
// Get a URL's hash ID
|
|
412
|
-
|
|
525
|
+
function getHashID(url) {
|
|
413
526
|
const matchId = url.match(rHashId);
|
|
414
527
|
return matchId !== null ? matchId[1] : null;
|
|
415
528
|
}
|
|
@@ -417,13 +530,6 @@ export function getHashID(url) {
|
|
|
417
530
|
const rHashId = /[#&]([^=]+)$/;
|
|
418
531
|
|
|
419
532
|
|
|
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
|
-
|
|
427
533
|
// Icon sizes
|
|
428
534
|
const burgerSize = '24';
|
|
429
535
|
const iconSize = '36';
|