markdown-up 1.3.1 → 1.6.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 +173 -50
- package/package.json +10 -9
package/lib/app.js
CHANGED
|
@@ -4,8 +4,16 @@
|
|
|
4
4
|
/** @module lib/app */
|
|
5
5
|
|
|
6
6
|
import {ElementApplication, encodeHashID, getHashID} from 'element-app/lib/app.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import {MarkdownScriptRuntime, markdownScriptCodeBlock} from 'markdown-charts/lib/script.js';
|
|
8
|
+
import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
|
|
9
|
+
import {getBaseURL, isRelativeURL, markdownElementsAsync} from 'markdown-model/lib/elements.js';
|
|
10
|
+
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
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';
|
|
9
17
|
|
|
10
18
|
|
|
11
19
|
// The application's hash parameter type model
|
|
@@ -36,51 +44,100 @@ struct MarkdownUp
|
|
|
36
44
|
# The line height
|
|
37
45
|
optional int(>= 1, <= 2) lineHeight
|
|
38
46
|
|
|
47
|
+
# Variable expressions
|
|
48
|
+
optional string{} var
|
|
49
|
+
|
|
50
|
+
# If true, enable debug behavior
|
|
51
|
+
optional int(== 1) debug
|
|
52
|
+
|
|
53
|
+
|
|
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
|
+
|
|
39
67
|
# Application command union
|
|
40
68
|
union Command
|
|
41
69
|
|
|
42
70
|
# Display the application's hash parameter documentation
|
|
43
|
-
|
|
71
|
+
HelpType help
|
|
44
72
|
|
|
45
73
|
# Show the URL's Markdown text
|
|
46
74
|
int(== 1) markdown
|
|
75
|
+
|
|
76
|
+
# Help type enum
|
|
77
|
+
enum HelpType
|
|
78
|
+
Help
|
|
79
|
+
Bar
|
|
80
|
+
Line
|
|
81
|
+
Table
|
|
47
82
|
`;
|
|
48
83
|
|
|
49
84
|
|
|
50
85
|
/**
|
|
51
|
-
*
|
|
86
|
+
* A markdown-up application options object
|
|
87
|
+
*
|
|
88
|
+
* @typedef {Object} MarkdownUpOptions
|
|
89
|
+
* @param {number} [options.fontSize = 12] - The font size, in points
|
|
90
|
+
* @param {number} [options.lineHeight = 1.2] - The line height, in em
|
|
91
|
+
* @param {boolean} [options.menu = true] - If true, show the menu
|
|
92
|
+
* @param {string} [options.url = 'README.md'] - The resource URL
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* MarkdownUp, a web application for viewing Markdown files
|
|
52
98
|
*
|
|
53
|
-
* MarkdownUp
|
|
54
|
-
* [
|
|
99
|
+
* The MarkdownUp class extends the element-app
|
|
100
|
+
* [ElementApplication]{@link https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html}
|
|
101
|
+
* class.
|
|
55
102
|
*
|
|
56
103
|
* @extends ElementApplication
|
|
57
104
|
* @property {number} fontSize - The font size, in points
|
|
58
105
|
* @property {number} lineHeight - The line height, in em
|
|
59
106
|
* @property {boolean} menu - If true, show the menu
|
|
60
107
|
* @property {string} url - The resource URL
|
|
108
|
+
* @property {number} [setNavigateTimeoutId = null] - The navigate timeout ID
|
|
61
109
|
*/
|
|
62
110
|
export class MarkdownUp extends ElementApplication {
|
|
63
111
|
/**
|
|
64
112
|
* Create an application instance
|
|
65
113
|
*
|
|
66
114
|
* @param {Object} window - The web browser window object
|
|
67
|
-
* @
|
|
68
|
-
* @param {number} [options.fontSize = 12] - The font size, in points
|
|
69
|
-
* @param {number} [options.lineHeight = 1.2] - The line height, in em
|
|
70
|
-
* @param {boolean} [options.menu = true] - If true, show the menu
|
|
71
|
-
* @param {string} [options.url = 'README.md'] - The resource URL
|
|
115
|
+
* @property {module:lib/app~MarkdownUpOptions} [options] - The markdown-up application options
|
|
72
116
|
*/
|
|
73
|
-
constructor(window, {
|
|
74
|
-
fontSize = 12,
|
|
75
|
-
lineHeight = 1.2,
|
|
76
|
-
menu = true,
|
|
77
|
-
url = 'README.md'
|
|
78
|
-
} = {}) {
|
|
117
|
+
constructor(window, {fontSize = 12, lineHeight = 1.2, menu = true, url = 'README.md'} = {}) {
|
|
79
118
|
super(window, 'markdown-up', 'MarkdownUp', markdownUpHashTypes);
|
|
80
119
|
this.fontSize = fontSize;
|
|
81
120
|
this.lineHeight = lineHeight;
|
|
82
121
|
this.menu = menu;
|
|
83
122
|
this.url = url;
|
|
123
|
+
this.setNavigateTimeoutId = null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* ElementApplication.render override to clear the navigate timeout
|
|
128
|
+
*/
|
|
129
|
+
async render() {
|
|
130
|
+
// Clear the navigate timeout ID, if one is set
|
|
131
|
+
/* c8 ignore start */
|
|
132
|
+
if (this.setNavigateTimeoutId !== null) {
|
|
133
|
+
this.window.clearTimeout(this.setNavigateTimeoutId);
|
|
134
|
+
this.setNavigateTimeoutId = null;
|
|
135
|
+
}
|
|
136
|
+
// eslint-disable-next lines-around-comment
|
|
137
|
+
/* c8 ignore stop */
|
|
138
|
+
|
|
139
|
+
// Call the base class implementation
|
|
140
|
+
await super.render();
|
|
84
141
|
}
|
|
85
142
|
|
|
86
143
|
/**
|
|
@@ -102,7 +159,7 @@ export class MarkdownUp extends ElementApplication {
|
|
|
102
159
|
|
|
103
160
|
/**
|
|
104
161
|
* The [Element Application main entry point]{@link
|
|
105
|
-
* https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#
|
|
162
|
+
* https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#main}.
|
|
106
163
|
*
|
|
107
164
|
* @override
|
|
108
165
|
* @returns {Object} [MainResult]{@link https://craigahobbs.github.io/element-app/module-lib_app.html#~MainResult}
|
|
@@ -110,7 +167,7 @@ export class MarkdownUp extends ElementApplication {
|
|
|
110
167
|
async main() {
|
|
111
168
|
// Load the text resource
|
|
112
169
|
const url = 'url' in this.params ? this.params.url : this.url;
|
|
113
|
-
let
|
|
170
|
+
let markdownText = null;
|
|
114
171
|
let markdownModel = null;
|
|
115
172
|
let markdownTitle = null;
|
|
116
173
|
if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
|
|
@@ -119,11 +176,30 @@ export class MarkdownUp extends ElementApplication {
|
|
|
119
176
|
const status = response.statusText;
|
|
120
177
|
throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
|
|
121
178
|
}
|
|
122
|
-
|
|
123
|
-
markdownModel = parseMarkdown(
|
|
179
|
+
markdownText = await response.text();
|
|
180
|
+
markdownModel = parseMarkdown(markdownText);
|
|
124
181
|
markdownTitle = getMarkdownTitle(markdownModel);
|
|
125
182
|
}
|
|
126
183
|
|
|
184
|
+
// Command?
|
|
185
|
+
let cmdElements = null;
|
|
186
|
+
if ('cmd' in this.params) {
|
|
187
|
+
// Display markdown?
|
|
188
|
+
if ('markdown' in this.params.cmd) {
|
|
189
|
+
cmdElements = {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}};
|
|
190
|
+
|
|
191
|
+
// 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
|
+
} else {
|
|
199
|
+
cmdElements = new UserTypeElements(this.params).getElements(this.hashTypes, this.hashType);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
127
203
|
// Helper function to create a cycle link
|
|
128
204
|
const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
|
|
129
205
|
const value = paramName in this.params ? this.params[paramName] : defaultValue;
|
|
@@ -161,38 +237,83 @@ export class MarkdownUp extends ElementApplication {
|
|
|
161
237
|
return `#${encodeQueryString(params)}`;
|
|
162
238
|
};
|
|
163
239
|
|
|
240
|
+
// The URL modifier function
|
|
241
|
+
const urlFn = (rawURL) => {
|
|
242
|
+
// Hash URL?
|
|
243
|
+
if (rawURL.startsWith('#')) {
|
|
244
|
+
// Fixup the "url" param if its relative
|
|
245
|
+
const hashParams = decodeQueryString(rawURL.slice(1));
|
|
246
|
+
if ('url' in hashParams && isRelativeURL(hashParams.url)) {
|
|
247
|
+
hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Return the combined hash URL
|
|
251
|
+
return encodeHashID({...this.params, ...hashParams}, getHashID(rawURL));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Relative URL?
|
|
255
|
+
if (isRelativeURL(rawURL)) {
|
|
256
|
+
return `${getBaseURL(url)}${rawURL}`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return rawURL;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// The log function
|
|
263
|
+
const logFn = (text) => {
|
|
264
|
+
if ('debug' in this.params) {
|
|
265
|
+
this.window.console.log(text);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/* c8 ignore start */
|
|
270
|
+
// The fetch function
|
|
271
|
+
// eslint-disable-next-line require-await
|
|
272
|
+
const fetchFn = async (fetchURL, init) => this.window.fetch(urlFn(fetchURL), init);
|
|
273
|
+
|
|
274
|
+
// The navigate timeout function
|
|
275
|
+
const navigateTimeoutFn = (navigateURL, delay) => {
|
|
276
|
+
const timeoutCallback = () => {
|
|
277
|
+
this.window.location.href = urlFn(navigateURL);
|
|
278
|
+
};
|
|
279
|
+
this.setNavigateTimeoutId = this.window.setTimeout(timeoutCallback, delay);
|
|
280
|
+
};
|
|
281
|
+
// eslint-disable-next-line lines-around-comment
|
|
282
|
+
/* c8 ignore stop */
|
|
283
|
+
|
|
164
284
|
// Render the text as Markdown
|
|
165
|
-
|
|
285
|
+
const fontSize = 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
|
|
286
|
+
const chartOptions = {
|
|
287
|
+
fetchFn,
|
|
288
|
+
fontSize,
|
|
289
|
+
urlFn,
|
|
290
|
+
'localStorage': this.window.localStorage,
|
|
291
|
+
logFn,
|
|
292
|
+
navigateTimeoutFn,
|
|
293
|
+
'sessionStorage': this.window.sessionStorage,
|
|
294
|
+
'variables': 'var' in this.params ? computeVariables(this.params.var) : {}
|
|
295
|
+
};
|
|
296
|
+
chartOptions.runtime = new MarkdownScriptRuntime(chartOptions);
|
|
297
|
+
const result = {
|
|
166
298
|
'title': markdownTitle,
|
|
167
299
|
'elements': [
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
? null : this.helpElements(),
|
|
171
|
-
|
|
172
|
-
// Render markdown?
|
|
173
|
-
!('cmd' in this.params && 'markdown' in this.params.cmd)
|
|
174
|
-
? null : {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}},
|
|
300
|
+
// Command?
|
|
301
|
+
cmdElements !== null ? cmdElements : null,
|
|
175
302
|
|
|
176
303
|
// Add a top hash ID, if necessary
|
|
177
|
-
|
|
304
|
+
cmdElements !== null || Object.keys(this.params).length === 0
|
|
178
305
|
? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
|
|
179
306
|
|
|
180
307
|
// Render the markdown
|
|
181
|
-
|
|
182
|
-
'
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if ('url' in hashParams && isRelativeURL(hashParams.url)) {
|
|
188
|
-
hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// Return the combined hash URL
|
|
192
|
-
return encodeHashID({...this.params, ...hashParams}, getHashID(hashURL));
|
|
308
|
+
cmdElements !== null ? null : await markdownElementsAsync(markdownModel, {
|
|
309
|
+
'codeBlocks': {
|
|
310
|
+
'bar-chart': (language, lines) => barChartCodeBlock(language, lines, chartOptions),
|
|
311
|
+
'data-table': (language, lines) => dataTableCodeBlock(language, lines, chartOptions),
|
|
312
|
+
'line-chart': (language, lines) => lineChartCodeBlock(language, lines, chartOptions),
|
|
313
|
+
'markdown-script': (language, lines) => markdownScriptCodeBlock(language, lines, chartOptions)
|
|
193
314
|
},
|
|
194
|
-
|
|
195
|
-
|
|
315
|
+
urlFn,
|
|
316
|
+
'headerIds': true
|
|
196
317
|
}),
|
|
197
318
|
|
|
198
319
|
// Popup menu burger
|
|
@@ -213,6 +334,11 @@ export class MarkdownUp extends ElementApplication {
|
|
|
213
334
|
'html': 'div',
|
|
214
335
|
'attr': {'class': 'menu'},
|
|
215
336
|
'elem': [
|
|
337
|
+
{
|
|
338
|
+
'html': 'a',
|
|
339
|
+
'attr': {'href': linkCommandToggle('markdown', 1), 'aria-label': 'Show Markdown'},
|
|
340
|
+
'elem': markdownSVGElements
|
|
341
|
+
},
|
|
216
342
|
{
|
|
217
343
|
'html': 'a',
|
|
218
344
|
'attr': {'href': linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
|
|
@@ -225,18 +351,15 @@ export class MarkdownUp extends ElementApplication {
|
|
|
225
351
|
},
|
|
226
352
|
{
|
|
227
353
|
'html': 'a',
|
|
228
|
-
'attr': {'href': linkCommandToggle('
|
|
229
|
-
'elem': markdownSVGElements
|
|
230
|
-
},
|
|
231
|
-
{
|
|
232
|
-
'html': 'a',
|
|
233
|
-
'attr': {'href': linkCommandToggle('help', 1), 'aria-label': 'Help'},
|
|
354
|
+
'attr': {'href': linkCommandToggle('help', 'Help'), 'aria-label': 'Help'},
|
|
234
355
|
'elem': helpSVGElements
|
|
235
356
|
}
|
|
236
357
|
]
|
|
237
358
|
}
|
|
238
359
|
]
|
|
239
360
|
};
|
|
361
|
+
|
|
362
|
+
return result;
|
|
240
363
|
}
|
|
241
364
|
}
|
|
242
365
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "markdown-up",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.6.0",
|
|
5
5
|
"description": "JavaScript Markdown viewer application",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"markdown",
|
|
@@ -22,16 +22,17 @@
|
|
|
22
22
|
"test": "ava -v test/"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"element-app": "~1.
|
|
26
|
-
"
|
|
27
|
-
"markdown-model": "~1.
|
|
28
|
-
"schema-markdown": "~1.
|
|
25
|
+
"element-app": "~1.1",
|
|
26
|
+
"markdown-charts": "~0.9",
|
|
27
|
+
"markdown-model": "~1.4",
|
|
28
|
+
"schema-markdown": "~1.1",
|
|
29
|
+
"schema-markdown-doc": "~1.3"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"ava": "
|
|
32
|
-
"c8": "~7.
|
|
33
|
-
"eslint": "~8.
|
|
32
|
+
"ava": "~4.0",
|
|
33
|
+
"c8": "~7.11",
|
|
34
|
+
"eslint": "~8.8",
|
|
34
35
|
"jsdoc": "~3.6",
|
|
35
|
-
"jsdom": "~
|
|
36
|
+
"jsdom": "~19.0"
|
|
36
37
|
}
|
|
37
38
|
}
|