markdown-up 4.2.0 → 5.1.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 +21 -54
- package/lib/appImports.js +2 -9
- package/lib/script.js +5 -8
- package/package.json +7 -8
package/lib/app.js
CHANGED
|
@@ -4,18 +4,17 @@
|
|
|
4
4
|
/** @module lib/app */
|
|
5
5
|
|
|
6
6
|
import {MarkdownScriptRuntime, markdownScriptCodeBlock} from './script.js';
|
|
7
|
-
import {
|
|
7
|
+
import {barescriptParseExpression, evaluateExpression} from 'bare-script/lib/runtime.js';
|
|
8
8
|
import {elementModelWrapCallbacks, markdownScriptFunctions} from './scriptLibrary.js';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
|
|
9
|
+
import {markdownElementsAsync, markdownParse, markdownTitle, schemaParse, schemaValidate} from 'bare-script/lib/include.js';
|
|
10
|
+
// eslint-disable-next-line no-duplicate-imports
|
|
11
|
+
import {urlDecodeQueryString, urlEncodeQueryString} from 'bare-script/lib/include.js';
|
|
13
12
|
import {renderElements} from 'element-model/lib/elementModel.js';
|
|
14
|
-
import {
|
|
13
|
+
import {valueJSON} from 'bare-script/lib/value.js';
|
|
15
14
|
|
|
16
15
|
|
|
17
16
|
// The application's hash parameter type model
|
|
18
|
-
const markdownUpTypes =
|
|
17
|
+
const markdownUpTypes = schemaParse(`\
|
|
19
18
|
# The MarkdownUp hash parameters
|
|
20
19
|
struct MarkdownUp
|
|
21
20
|
|
|
@@ -74,15 +73,10 @@ enum MarkdownUpView
|
|
|
74
73
|
* @property {?number} [lineHeight = 1.3] - The line height, in em
|
|
75
74
|
* @property {?string} [markdownText = null] - The default Markdown text
|
|
76
75
|
* @property {?boolean} [menu = true] - If true, show the menu
|
|
77
|
-
* @property {?string} [systemPrefix] - The markdown-script system include prefix
|
|
78
76
|
* @property {?string} [url = 'README.md'] - The default resource URL
|
|
79
77
|
*/
|
|
80
78
|
|
|
81
79
|
|
|
82
|
-
// The default system include path prefix
|
|
83
|
-
const defaultSystemPrefix = 'https://craigahobbs.github.io/markdown-up/include/';
|
|
84
|
-
|
|
85
|
-
|
|
86
80
|
// The default application help URL
|
|
87
81
|
const defaultHelpURL = 'https://craigahobbs.github.io/markdown-up-py/';
|
|
88
82
|
|
|
@@ -111,7 +105,6 @@ export class MarkdownUp {
|
|
|
111
105
|
this.lineHeight = (options !== null ? options.lineHeight : null) ?? 1.3;
|
|
112
106
|
this.markdownText = (options !== null ? options.markdownText : null) ?? null;
|
|
113
107
|
this.menu = (options !== null ? options.menu : null) ?? true;
|
|
114
|
-
this.systemPrefix = (options !== null ? options.systemPrefix : null) ?? defaultSystemPrefix;
|
|
115
108
|
this.url = (options !== null ? options.url : null) ?? 'README.md';
|
|
116
109
|
this.runtimeDocumentKeyDown = null;
|
|
117
110
|
this.runtimeWindowResize = null;
|
|
@@ -189,7 +182,7 @@ export class MarkdownUp {
|
|
|
189
182
|
this.updateParams();
|
|
190
183
|
|
|
191
184
|
// Skip the render if the page params haven't changed
|
|
192
|
-
if (!forceRender && paramsPrev !== null &&
|
|
185
|
+
if (!forceRender && paramsPrev !== null && valueJSON(paramsPrev) === valueJSON(this.params)) {
|
|
193
186
|
return;
|
|
194
187
|
}
|
|
195
188
|
} catch ({message}) {
|
|
@@ -288,17 +281,17 @@ export class MarkdownUp {
|
|
|
288
281
|
this.paramsSession = {};
|
|
289
282
|
|
|
290
283
|
// Decode and validate the hash parameters
|
|
291
|
-
this.params =
|
|
284
|
+
this.params = schemaValidate(
|
|
292
285
|
markdownUpTypes,
|
|
293
286
|
'MarkdownUp',
|
|
294
|
-
|
|
287
|
+
urlDecodeQueryString(paramString ?? this.window.location.hash.slice(1))
|
|
295
288
|
);
|
|
296
289
|
|
|
297
290
|
// Decode and validate the local storage paramters
|
|
298
291
|
const localJSON = localJSONString ?? this.window.localStorage.getItem('MarkdownUp');
|
|
299
292
|
if (localJSON !== null) {
|
|
300
293
|
try {
|
|
301
|
-
this.paramsLocal =
|
|
294
|
+
this.paramsLocal = schemaValidate(markdownUpTypes, 'MarkdownUpLocal', JSON.parse(localJSON));
|
|
302
295
|
} catch {
|
|
303
296
|
// Do nothing
|
|
304
297
|
}
|
|
@@ -308,7 +301,7 @@ export class MarkdownUp {
|
|
|
308
301
|
const sessionJSON = sessionJSONString ?? this.window.sessionStorage.getItem('MarkdownUp');
|
|
309
302
|
if (sessionJSON !== null) {
|
|
310
303
|
try {
|
|
311
|
-
this.paramsSession =
|
|
304
|
+
this.paramsSession = schemaValidate(markdownUpTypes, 'MarkdownUpSession', JSON.parse(sessionJSON));
|
|
312
305
|
} catch {
|
|
313
306
|
// Do nothing
|
|
314
307
|
}
|
|
@@ -353,30 +346,6 @@ export class MarkdownUp {
|
|
|
353
346
|
const url = this.getResourceURL();
|
|
354
347
|
const scriptOptions = this.createScriptOptions();
|
|
355
348
|
|
|
356
|
-
// Load BareScript
|
|
357
|
-
const includeScript = {
|
|
358
|
-
'statements': [
|
|
359
|
-
{
|
|
360
|
-
'include': {
|
|
361
|
-
'includes': [
|
|
362
|
-
{'url': 'markdown.bare', 'system': true},
|
|
363
|
-
{'url': 'markdownHighlight.bare', 'system': true},
|
|
364
|
-
{'url': 'markdownElements.bare', 'system': true},
|
|
365
|
-
{'url': 'markdownParser.bare', 'system': true},
|
|
366
|
-
|
|
367
|
-
// Additional common includes
|
|
368
|
-
{'url': 'args.bare', 'system': true}
|
|
369
|
-
]
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
]
|
|
373
|
-
};
|
|
374
|
-
await executeScriptAsync(includeScript, scriptOptions);
|
|
375
|
-
const getMarkdownTitle = (markdown) => scriptOptions.globals.markdownTitle([markdown], scriptOptions);
|
|
376
|
-
const markdownElementsAsync =
|
|
377
|
-
(markdown, markdownOptions) => scriptOptions.globals.markdownElementsAsync([markdown, markdownOptions], scriptOptions);
|
|
378
|
-
const parseMarkdown = (text) => scriptOptions.globals.markdownParse([text], scriptOptions);
|
|
379
|
-
|
|
380
349
|
// Get the Markdown text
|
|
381
350
|
let markdownText;
|
|
382
351
|
let timeBegin;
|
|
@@ -424,13 +393,13 @@ export class MarkdownUp {
|
|
|
424
393
|
}
|
|
425
394
|
|
|
426
395
|
// Parse the Markdown and get the title
|
|
427
|
-
const markdownModel =
|
|
428
|
-
const
|
|
396
|
+
const markdownModel = markdownParse(markdownText);
|
|
397
|
+
const title = markdownTitle(markdownModel);
|
|
429
398
|
|
|
430
399
|
// Display the Markdown?
|
|
431
400
|
if (this.params.view === 'markdown') {
|
|
432
401
|
return {
|
|
433
|
-
'title':
|
|
402
|
+
'title': title,
|
|
434
403
|
'elements': [
|
|
435
404
|
this.burgerElements(),
|
|
436
405
|
{'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}}
|
|
@@ -441,14 +410,13 @@ export class MarkdownUp {
|
|
|
441
410
|
// Copy the markdown options and override the markdown-script code block renderer
|
|
442
411
|
const scriptMarkdownOptions = {...scriptOptions.markdownOptions};
|
|
443
412
|
scriptMarkdownOptions.codeBlocks = {...scriptMarkdownOptions.codeBlocks};
|
|
444
|
-
scriptMarkdownOptions.codeBlocks['markdown-script'] =
|
|
445
|
-
([codeBlock], fnScriptOptions) => markdownScriptCodeBlock(codeBlock, fnScriptOptions);
|
|
413
|
+
scriptMarkdownOptions.codeBlocks['markdown-script'] = ([codeBlock]) => markdownScriptCodeBlock(codeBlock, scriptOptions);
|
|
446
414
|
|
|
447
415
|
// Render the Markdown
|
|
448
416
|
const contentElements = await markdownElementsAsync(markdownModel, scriptMarkdownOptions);
|
|
449
417
|
elementModelWrapCallbacks(contentElements, scriptOptions);
|
|
450
418
|
const result = {
|
|
451
|
-
'title':
|
|
419
|
+
'title': title,
|
|
452
420
|
'elements': [
|
|
453
421
|
// Menu "burger"
|
|
454
422
|
this.burgerElements(),
|
|
@@ -524,7 +492,6 @@ export class MarkdownUp {
|
|
|
524
492
|
logFn,
|
|
525
493
|
markdownOptions,
|
|
526
494
|
'params': this.params,
|
|
527
|
-
'systemPrefix': this.systemPrefix,
|
|
528
495
|
'urlFn': (url) => this.modifyURL(url),
|
|
529
496
|
'window': this.window
|
|
530
497
|
};
|
|
@@ -535,7 +502,7 @@ export class MarkdownUp {
|
|
|
535
502
|
for (const varName of Object.keys(this.params.var)) {
|
|
536
503
|
const varExprStr = this.params.var[varName];
|
|
537
504
|
try {
|
|
538
|
-
scriptOptions.variables[varName] = evaluateExpression(
|
|
505
|
+
scriptOptions.variables[varName] = evaluateExpression(barescriptParseExpression(varExprStr), scriptOptions.variables);
|
|
539
506
|
} catch ({message}) {
|
|
540
507
|
if (scriptOptions.debug) {
|
|
541
508
|
logFn(`MarkdownUp: Error evaluating variable "${varName}" expression "${varExprStr}": ${message}`);
|
|
@@ -609,13 +576,13 @@ export class MarkdownUp {
|
|
|
609
576
|
// Hash URL?
|
|
610
577
|
if (url.startsWith('#')) {
|
|
611
578
|
// Fixup the "url" param if its relative
|
|
612
|
-
const hashParams =
|
|
579
|
+
const hashParams = urlDecodeQueryString(url.slice(1)) ?? {};
|
|
613
580
|
if ('url' in hashParams && hashParams.url !== '' && isRelativeURL(hashParams.url)) {
|
|
614
581
|
hashParams.url = `${getBaseURL(resourceURL)}${hashParams.url}`;
|
|
615
582
|
}
|
|
616
583
|
|
|
617
584
|
// Encode hash parameters with a header ID
|
|
618
|
-
const paramString =
|
|
585
|
+
const paramString = urlEncodeQueryString({...this.params, ...hashParams});
|
|
619
586
|
const headerId = getHeaderId(url);
|
|
620
587
|
return `#${paramString}${headerId !== null && paramString !== '' ? '&' : ''}${headerId !== null ? headerId : ''}`;
|
|
621
588
|
}
|
|
@@ -630,7 +597,7 @@ export class MarkdownUp {
|
|
|
630
597
|
|
|
631
598
|
|
|
632
599
|
burgerElements() {
|
|
633
|
-
const paramString =
|
|
600
|
+
const paramString = urlEncodeQueryString(this.params);
|
|
634
601
|
const topHeaderId = `${paramString}${paramString !== '' ? '&' : ''}_top`;
|
|
635
602
|
return [
|
|
636
603
|
!this.menu ? null : [
|
|
@@ -717,7 +684,7 @@ export class MarkdownUp {
|
|
|
717
684
|
} else {
|
|
718
685
|
params.view = viewValue;
|
|
719
686
|
}
|
|
720
|
-
this.window.location.href = `#${
|
|
687
|
+
this.window.location.href = `#${urlEncodeQueryString(params)}`;
|
|
721
688
|
},
|
|
722
689
|
icon
|
|
723
690
|
);
|
package/lib/appImports.js
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
// Bulk-import JavaScript
|
|
6
6
|
|
|
7
7
|
// bare-script
|
|
8
|
+
import 'bare-script/lib/include.js';
|
|
9
|
+
import 'bare-script/lib/includeSource.js';
|
|
8
10
|
import 'bare-script/lib/library.js';
|
|
9
|
-
import 'bare-script/lib/model.js';
|
|
10
11
|
import 'bare-script/lib/options.js';
|
|
11
|
-
import 'bare-script/lib/parser.js';
|
|
12
12
|
import 'bare-script/lib/runtime.js';
|
|
13
13
|
import 'bare-script/lib/runtimeAsync.js';
|
|
14
14
|
import 'bare-script/lib/value.js';
|
|
@@ -20,10 +20,3 @@ import 'element-model/lib/elementModel.js';
|
|
|
20
20
|
export {MarkdownUp} from './app.js';
|
|
21
21
|
import './script.js';
|
|
22
22
|
import './scriptLibrary.js';
|
|
23
|
-
|
|
24
|
-
// schema-markdown
|
|
25
|
-
import 'schema-markdown/lib/encode.js';
|
|
26
|
-
import 'schema-markdown/lib/parser.js';
|
|
27
|
-
import 'schema-markdown/lib/schema.js';
|
|
28
|
-
import 'schema-markdown/lib/schemaUtil.js';
|
|
29
|
-
import 'schema-markdown/lib/typeModel.js';
|
package/lib/script.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Licensed under the MIT License
|
|
2
2
|
// https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
|
|
3
3
|
|
|
4
|
+
import {barescriptLintScript, barescriptParseScript} from 'bare-script/lib/runtime.js';
|
|
5
|
+
import {markdownElements, markdownParse} from 'bare-script/lib/include.js';
|
|
4
6
|
import {elementModelWrapCallbacks} from './scriptLibrary.js';
|
|
5
7
|
import {executeScriptAsync} from 'bare-script/lib/runtimeAsync.js';
|
|
6
|
-
import {lintScript} from 'bare-script/lib/model.js';
|
|
7
|
-
import {parseScript} from 'bare-script/lib/parser.js';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -58,14 +58,14 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
|
|
|
58
58
|
let errorMessage = null;
|
|
59
59
|
try {
|
|
60
60
|
// Parse the script
|
|
61
|
-
const script =
|
|
61
|
+
const script = barescriptParseScript(codeBlock.lines, codeBlock.startLineNumber + 1);
|
|
62
62
|
|
|
63
63
|
// Execute the script
|
|
64
64
|
await executeScriptAsync(script, options);
|
|
65
65
|
|
|
66
66
|
// Run the bare-script linter?
|
|
67
67
|
if ('logFn' in options && options.debug) {
|
|
68
|
-
const warnings =
|
|
68
|
+
const warnings = barescriptLintScript(script, options.globals);
|
|
69
69
|
const warningPrefix = `MarkdownUp: Script static analysis...`;
|
|
70
70
|
if (warnings.length === 0) {
|
|
71
71
|
options.logFn(`${warningPrefix} OK`);
|
|
@@ -197,10 +197,7 @@ export class MarkdownScriptRuntime {
|
|
|
197
197
|
// Helper method to finish the current Markdown block and add its elements
|
|
198
198
|
finishMarkdown() {
|
|
199
199
|
if (this.markdown !== null) {
|
|
200
|
-
const markdownElements
|
|
201
|
-
(markdown, markdownOptions) => this.options.globals.markdownElements([markdown, markdownOptions], this.options);
|
|
202
|
-
const parseMarkdown = (text) => this.options.globals.markdownParse([text], this.options);
|
|
203
|
-
const contentElements = markdownElements(parseMarkdown(this.markdown), this.options.markdownOptions);
|
|
200
|
+
const contentElements = markdownElements(markdownParse(this.markdown), this.options.markdownOptions);
|
|
204
201
|
elementModelWrapCallbacks(contentElements, this.options);
|
|
205
202
|
this.addElements(contentElements);
|
|
206
203
|
this.markdown = null;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "markdown-up",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"description": "MarkdownUp is a Markdown viewer",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"markdown",
|
|
@@ -22,16 +22,15 @@
|
|
|
22
22
|
"test": "node --test test/"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"bare-script": "~
|
|
26
|
-
"element-model": "~1.2"
|
|
27
|
-
"schema-markdown": "~1.3"
|
|
25
|
+
"bare-script": "~5.1",
|
|
26
|
+
"element-model": "~1.2"
|
|
28
27
|
},
|
|
29
28
|
"devDependencies": {
|
|
30
29
|
"@eslint/js": "~10.0",
|
|
31
|
-
"c8": "~
|
|
32
|
-
"eslint": "~10.
|
|
33
|
-
"globals": "~17.
|
|
30
|
+
"c8": "~12.0",
|
|
31
|
+
"eslint": "~10.8",
|
|
32
|
+
"globals": "~17.8",
|
|
34
33
|
"jsdoc": "~4.0",
|
|
35
|
-
"jsdom": "~
|
|
34
|
+
"jsdom": "~30.0"
|
|
36
35
|
}
|
|
37
36
|
}
|