markdown-up 1.7.1 → 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 +105 -45
- package/lib/script.js +38 -23
- package/package.json +2 -2
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 {ElementApplication, encodeHashID, getHashID} from 'element-app/lib/app.js';
|
|
7
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';
|
|
10
|
+
import {SchemaMarkdownParser} from 'schema-markdown/lib/parser.js';
|
|
11
11
|
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
15
|
import {parseExpression} from 'calc-script/lib/parser.js';
|
|
16
|
+
import {renderElements} from 'element-model/lib/elementModel.js';
|
|
16
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,19 +53,6 @@ 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
|
|
|
@@ -73,35 +62,34 @@ union Command
|
|
|
73
62
|
# Show the URL's Markdown text
|
|
74
63
|
int(== 1) markdown
|
|
75
64
|
`;
|
|
65
|
+
const markdownUpTypes = new SchemaMarkdownParser(markdownUpTypesSmd).types;
|
|
76
66
|
|
|
77
67
|
|
|
78
68
|
/**
|
|
79
69
|
* A markdown-up application options object
|
|
80
70
|
*
|
|
81
71
|
* @typedef {Object} MarkdownUpOptions
|
|
82
|
-
* @param {number} [
|
|
83
|
-
* @param {number} [
|
|
84
|
-
* @param {
|
|
85
|
-
* @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
|
|
86
77
|
*/
|
|
87
78
|
|
|
88
79
|
|
|
89
80
|
/**
|
|
90
|
-
* MarkdownUp, a
|
|
81
|
+
* MarkdownUp, a Markdown viewer
|
|
91
82
|
*
|
|
92
|
-
* The
|
|
93
|
-
*
|
|
94
|
-
* class.
|
|
95
|
-
*
|
|
96
|
-
* @extends ElementApplication
|
|
83
|
+
* @property {Object} window - The web browser window object
|
|
84
|
+
* @property {?Object} params - The validated hash parameters object
|
|
97
85
|
* @property {number} fontSize - The font size, in points
|
|
98
86
|
* @property {number} lineHeight - The line height, in em
|
|
99
87
|
* @property {string} markdownText - The markdown text
|
|
100
88
|
* @property {boolean} menu - If true, show the menu
|
|
101
89
|
* @property {string} url - The resource URL
|
|
102
|
-
* @property {number}
|
|
90
|
+
* @property {?number} setNavigateTimeoutId - The navigate timeout ID
|
|
103
91
|
*/
|
|
104
|
-
export class MarkdownUp
|
|
92
|
+
export class MarkdownUp {
|
|
105
93
|
/**
|
|
106
94
|
* Create an application instance
|
|
107
95
|
*
|
|
@@ -109,7 +97,8 @@ export class MarkdownUp extends ElementApplication {
|
|
|
109
97
|
* @property {module:lib/app~MarkdownUpOptions} [options] - The markdown-up application options
|
|
110
98
|
*/
|
|
111
99
|
constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
|
|
112
|
-
|
|
100
|
+
this.window = window;
|
|
101
|
+
this.params = null;
|
|
113
102
|
this.fontSize = fontSize;
|
|
114
103
|
this.lineHeight = lineHeight;
|
|
115
104
|
this.markdownText = markdownText;
|
|
@@ -118,6 +107,15 @@ export class MarkdownUp extends ElementApplication {
|
|
|
118
107
|
this.setNavigateTimeoutId = null;
|
|
119
108
|
}
|
|
120
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
|
+
|
|
121
119
|
/**
|
|
122
120
|
* ElementApplication.render override to clear the navigate timeout
|
|
123
121
|
*/
|
|
@@ -128,18 +126,38 @@ export class MarkdownUp extends ElementApplication {
|
|
|
128
126
|
this.setNavigateTimeoutId = null;
|
|
129
127
|
}
|
|
130
128
|
|
|
131
|
-
//
|
|
132
|
-
|
|
133
|
-
|
|
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
|
+
}
|
|
134
160
|
|
|
135
|
-
/**
|
|
136
|
-
* The [Element Application pre-render callback]{@link
|
|
137
|
-
* https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#preRender}.
|
|
138
|
-
* The callback is used to set the document font size and line height.
|
|
139
|
-
*
|
|
140
|
-
* @override
|
|
141
|
-
*/
|
|
142
|
-
preRender() {
|
|
143
161
|
// Set the font size
|
|
144
162
|
const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
|
|
145
163
|
this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
|
|
@@ -147,6 +165,31 @@ export class MarkdownUp extends ElementApplication {
|
|
|
147
165
|
// Set the line height
|
|
148
166
|
const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
|
|
149
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);
|
|
150
193
|
}
|
|
151
194
|
|
|
152
195
|
/**
|
|
@@ -158,12 +201,13 @@ export class MarkdownUp extends ElementApplication {
|
|
|
158
201
|
*/
|
|
159
202
|
async main() {
|
|
160
203
|
// Load the text resource
|
|
161
|
-
const
|
|
204
|
+
const urlOverride = 'url' in this.params && this.params.url !== '';
|
|
205
|
+
const url = urlOverride ? this.params.url : this.url;
|
|
162
206
|
let markdownText = null;
|
|
163
207
|
let markdownModel = null;
|
|
164
208
|
let markdownTitle = null;
|
|
165
209
|
if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
|
|
166
|
-
if (this.markdownText !== null && !
|
|
210
|
+
if (this.markdownText !== null && !urlOverride) {
|
|
167
211
|
({markdownText} = this);
|
|
168
212
|
} else {
|
|
169
213
|
const response = await this.window.fetch(url);
|
|
@@ -186,14 +230,14 @@ export class MarkdownUp extends ElementApplication {
|
|
|
186
230
|
|
|
187
231
|
// Help
|
|
188
232
|
} else {
|
|
189
|
-
cmdElements = schemaMarkdownDoc(
|
|
233
|
+
cmdElements = schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)});
|
|
190
234
|
}
|
|
191
235
|
}
|
|
192
236
|
|
|
193
237
|
// Helper function to create a cycle link
|
|
194
238
|
const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
|
|
195
239
|
const value = paramName in this.params ? this.params[paramName] : defaultValue;
|
|
196
|
-
const {attr} =
|
|
240
|
+
const {attr} = markdownUpTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
|
|
197
241
|
let valueNew = value + delta;
|
|
198
242
|
if (valueNew > attr.lte) {
|
|
199
243
|
valueNew = attr.gte;
|
|
@@ -233,7 +277,7 @@ export class MarkdownUp extends ElementApplication {
|
|
|
233
277
|
if (rawURL.startsWith('#')) {
|
|
234
278
|
// Fixup the "url" param if its relative
|
|
235
279
|
const hashParams = decodeQueryString(rawURL.slice(1));
|
|
236
|
-
if ('url' in hashParams && isRelativeURL(hashParams.url)) {
|
|
280
|
+
if ('url' in hashParams && hashParams.url !== '' && isRelativeURL(hashParams.url)) {
|
|
237
281
|
hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
|
|
238
282
|
}
|
|
239
283
|
|
|
@@ -364,6 +408,22 @@ export class MarkdownUp extends ElementApplication {
|
|
|
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/script.js
CHANGED
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
/** @module lib/script */
|
|
5
5
|
|
|
6
6
|
import {encodeMarkdownText, getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
7
|
-
import {validateType,
|
|
7
|
+
import {validateType, validateTypeModel} from 'schema-markdown/lib/schema.js';
|
|
8
8
|
import {SchemaMarkdownParser} from 'schema-markdown/lib/parser.js';
|
|
9
9
|
import {encodeQueryString} from 'schema-markdown/lib/encode.js';
|
|
10
10
|
import {executeScriptAsync} from 'calc-script/lib/runtimeAsync.js';
|
|
11
|
-
import {
|
|
11
|
+
import {markdownElementsAsync} from 'markdown-model/lib/elements.js';
|
|
12
12
|
import {parseScript} from 'calc-script/lib/parser.js';
|
|
13
13
|
import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
|
|
14
14
|
import {typeModel} from 'schema-markdown/lib/typeModel.js';
|
|
@@ -58,7 +58,8 @@ export async function markdownScriptCodeBlock(language, lines, options = null) {
|
|
|
58
58
|
if ('drawing' in part) {
|
|
59
59
|
elements.push({'html': 'p', 'elem': part.drawing});
|
|
60
60
|
} else if ('markdown' in part) {
|
|
61
|
-
|
|
61
|
+
// eslint-disable-next-line no-await-in-loop
|
|
62
|
+
elements.push(await markdownElementsAsync(parseMarkdown(part.markdown), markdownElementsOptions));
|
|
62
63
|
} else {
|
|
63
64
|
elements.push(part.elements);
|
|
64
65
|
}
|
|
@@ -144,23 +145,16 @@ export class MarkdownScriptRuntime {
|
|
|
144
145
|
|
|
145
146
|
// Markdown functions
|
|
146
147
|
'markdownEncode': (args) => encodeMarkdownText(...args),
|
|
147
|
-
'
|
|
148
|
-
'
|
|
148
|
+
'markdownParse': (lines) => parseMarkdown(MarkdownScriptRuntime.flattenAndSplitLines(lines)),
|
|
149
|
+
'markdownPrint': (lines) => this.markdownPrint(lines),
|
|
150
|
+
'markdownTitle': ([markdownModel]) => getMarkdownTitle(markdownModel),
|
|
149
151
|
|
|
150
152
|
// Schema functions
|
|
151
|
-
'schemaParse': (
|
|
152
|
-
|
|
153
|
-
parser.parse(args);
|
|
154
|
-
return parser.types;
|
|
155
|
-
},
|
|
156
|
-
'schemaParseString': ([text]) => {
|
|
157
|
-
const parser = new SchemaMarkdownParser(text);
|
|
158
|
-
return parser.types;
|
|
159
|
-
},
|
|
160
|
-
'schemaTypeModel': () => typeModel.types,
|
|
153
|
+
'schemaParse': (lines) => MarkdownScriptRuntime.schemaParse(lines),
|
|
154
|
+
'schemaTypeModel': () => typeModel,
|
|
161
155
|
'schemaValidate': ([types, typeName, value]) => validateType(types, typeName, value),
|
|
162
|
-
'schemaValidateTypeModel': ([types]) =>
|
|
163
|
-
'schemaPrint': ([types, typeName]) => this.schemaPrint(types, typeName),
|
|
156
|
+
'schemaValidateTypeModel': ([types]) => validateTypeModel(types),
|
|
157
|
+
'schemaPrint': ([types, typeName, actionURLs]) => this.schemaPrint(types, typeName, actionURLs),
|
|
164
158
|
|
|
165
159
|
// Session storage functions
|
|
166
160
|
'sessionStorageGetItem': ([key]) => (
|
|
@@ -421,15 +415,32 @@ export class MarkdownScriptRuntime {
|
|
|
421
415
|
|
|
422
416
|
markdownPrint(lines) {
|
|
423
417
|
const markdownLines = this.setMarkdown();
|
|
424
|
-
markdownLines.push(...lines);
|
|
418
|
+
markdownLines.push(...MarkdownScriptRuntime.flattenAndSplitLines(lines));
|
|
425
419
|
}
|
|
426
420
|
|
|
427
|
-
|
|
428
|
-
const
|
|
429
|
-
|
|
421
|
+
static schemaParse(lines) {
|
|
422
|
+
const parser = new SchemaMarkdownParser();
|
|
423
|
+
parser.parse(MarkdownScriptRuntime.flattenAndSplitLines(lines));
|
|
424
|
+
return parser.types;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
static flattenAndSplitLines(lines) {
|
|
428
|
+
return lines.flat().reduce(
|
|
429
|
+
(result, line) => {
|
|
430
|
+
result.push(...line.split(rLineSplit));
|
|
431
|
+
return result;
|
|
432
|
+
},
|
|
433
|
+
[]
|
|
430
434
|
);
|
|
431
|
-
|
|
432
|
-
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
schemaPrint(types, typeName, actionURLs = null) {
|
|
438
|
+
const params = (this.options !== null && 'params' in this.options ? encodeQueryString(this.options.params) : null);
|
|
439
|
+
const options = {params};
|
|
440
|
+
if (actionURLs !== null) {
|
|
441
|
+
options.actionURLs = actionURLs;
|
|
442
|
+
}
|
|
443
|
+
this.setElements(schemaMarkdownDoc(types, typeName, {params, actionURLs}));
|
|
433
444
|
}
|
|
434
445
|
|
|
435
446
|
setDrawingSize(width, height) {
|
|
@@ -445,3 +456,7 @@ export class MarkdownScriptRuntime {
|
|
|
445
456
|
}
|
|
446
457
|
}
|
|
447
458
|
}
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
// Line-splitting regex
|
|
462
|
+
const rLineSplit = /\r?\n/;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "markdown-up",
|
|
4
|
-
"version": "1.7.
|
|
4
|
+
"version": "1.7.2",
|
|
5
5
|
"description": "JavaScript Markdown viewer application",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"markdown",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"calc-script": "~0.9",
|
|
26
|
-
"element-
|
|
26
|
+
"element-model": "~1.1",
|
|
27
27
|
"markdown-model": "~1.4",
|
|
28
28
|
"schema-markdown": "~1.1",
|
|
29
29
|
"schema-markdown-doc": "~1.4"
|