markdown-up 1.5.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.
Files changed (2) hide show
  1. package/lib/app.js +107 -43
  2. package/package.json +6 -6
package/lib/app.js CHANGED
@@ -4,16 +4,16 @@
4
4
  /** @module lib/app */
5
5
 
6
6
  import {ElementApplication, encodeHashID, getHashID} from 'element-app/lib/app.js';
7
+ import {MarkdownScriptRuntime, markdownScriptCodeBlock} from 'markdown-charts/lib/script.js';
7
8
  import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
8
- import {getBaseURL, isRelativeURL, markdownElements} from 'markdown-model/lib/elements.js';
9
+ import {getBaseURL, isRelativeURL, markdownElementsAsync} from 'markdown-model/lib/elements.js';
10
+ import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
9
11
  import {UserTypeElements} from 'schema-markdown-doc/lib/userTypeElements.js';
10
12
  import {barChartCodeBlock} from 'markdown-charts/lib/barChart.js';
11
13
  import {chartModel} from 'markdown-charts/lib/model.js';
14
+ import {computeVariables} from 'markdown-charts/lib/data.js';
12
15
  import {dataTableCodeBlock} from 'markdown-charts/lib/dataTable.js';
13
- import {drawingCodeBlock} from 'markdown-charts/lib/drawing.js';
14
- import {getMarkdownTitle} from 'markdown-model/lib/markdownModel.js';
15
16
  import {lineChartCodeBlock} from 'markdown-charts/lib/lineChart.js';
16
- import {parseMarkdown} from 'markdown-model/lib/parser.js';
17
17
 
18
18
 
19
19
  // The application's hash parameter type model
@@ -44,8 +44,11 @@ struct MarkdownUp
44
44
  # The line height
45
45
  optional int(>= 1, <= 2) lineHeight
46
46
 
47
- # Variable collection
48
- optional FieldValue{len > 0} variables
47
+ # Variable expressions
48
+ optional string{} var
49
+
50
+ # If true, enable debug behavior
51
+ optional int(== 1) debug
49
52
 
50
53
 
51
54
  # Field value union
@@ -79,6 +82,17 @@ enum HelpType
79
82
  `;
80
83
 
81
84
 
85
+ /**
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
+
82
96
  /**
83
97
  * MarkdownUp, a web application for viewing Markdown files
84
98
  *
@@ -91,29 +105,39 @@ enum HelpType
91
105
  * @property {number} lineHeight - The line height, in em
92
106
  * @property {boolean} menu - If true, show the menu
93
107
  * @property {string} url - The resource URL
108
+ * @property {number} [setNavigateTimeoutId = null] - The navigate timeout ID
94
109
  */
95
110
  export class MarkdownUp extends ElementApplication {
96
111
  /**
97
112
  * Create an application instance
98
113
  *
99
114
  * @param {Object} window - The web browser window object
100
- * @param {Object} [options] - The application options
101
- * @param {number} [options.fontSize = 12] - The font size, in points
102
- * @param {number} [options.lineHeight = 1.2] - The line height, in em
103
- * @param {boolean} [options.menu = true] - If true, show the menu
104
- * @param {string} [options.url = 'README.md'] - The resource URL
115
+ * @property {module:lib/app~MarkdownUpOptions} [options] - The markdown-up application options
105
116
  */
106
- constructor(window, {
107
- fontSize = 12,
108
- lineHeight = 1.2,
109
- menu = true,
110
- url = 'README.md'
111
- } = {}) {
117
+ constructor(window, {fontSize = 12, lineHeight = 1.2, menu = true, url = 'README.md'} = {}) {
112
118
  super(window, 'markdown-up', 'MarkdownUp', markdownUpHashTypes);
113
119
  this.fontSize = fontSize;
114
120
  this.lineHeight = lineHeight;
115
121
  this.menu = menu;
116
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();
117
141
  }
118
142
 
119
143
  /**
@@ -143,7 +167,7 @@ export class MarkdownUp extends ElementApplication {
143
167
  async main() {
144
168
  // Load the text resource
145
169
  const url = 'url' in this.params ? this.params.url : this.url;
146
- let text = null;
170
+ let markdownText = null;
147
171
  let markdownModel = null;
148
172
  let markdownTitle = null;
149
173
  if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
@@ -152,8 +176,8 @@ export class MarkdownUp extends ElementApplication {
152
176
  const status = response.statusText;
153
177
  throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
154
178
  }
155
- text = await response.text();
156
- markdownModel = parseMarkdown(text);
179
+ markdownText = await response.text();
180
+ markdownModel = parseMarkdown(markdownText);
157
181
  markdownTitle = getMarkdownTitle(markdownModel);
158
182
  }
159
183
 
@@ -162,7 +186,7 @@ export class MarkdownUp extends ElementApplication {
162
186
  if ('cmd' in this.params) {
163
187
  // Display markdown?
164
188
  if ('markdown' in this.params.cmd) {
165
- cmdElements = {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}};
189
+ cmdElements = {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}};
166
190
 
167
191
  // Help
168
192
  } else if (this.params.cmd.help === 'Bar') {
@@ -213,14 +237,64 @@ export class MarkdownUp extends ElementApplication {
213
237
  return `#${encodeQueryString(params)}`;
214
238
  };
215
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
+
216
284
  // Render the text as Markdown
285
+ const fontSize = 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
217
286
  const chartOptions = {
218
- 'fontSize': 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize),
219
- url,
220
- 'variables': 'variables' in this.params ? this.params.variables : {},
221
- 'window': this.window
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) : {}
222
295
  };
223
- return {
296
+ chartOptions.runtime = new MarkdownScriptRuntime(chartOptions);
297
+ const result = {
224
298
  'title': markdownTitle,
225
299
  'elements': [
226
300
  // Command?
@@ -231,27 +305,15 @@ export class MarkdownUp extends ElementApplication {
231
305
  ? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
232
306
 
233
307
  // Render the markdown
234
- cmdElements !== null ? null : markdownElements(markdownModel, {
308
+ cmdElements !== null ? null : await markdownElementsAsync(markdownModel, {
235
309
  'codeBlocks': {
236
310
  'bar-chart': (language, lines) => barChartCodeBlock(language, lines, chartOptions),
237
311
  'data-table': (language, lines) => dataTableCodeBlock(language, lines, chartOptions),
238
- 'drawing': (language, lines) => drawingCodeBlock(language, lines, chartOptions),
239
- 'line-chart': (language, lines) => lineChartCodeBlock(language, lines, chartOptions)
312
+ 'line-chart': (language, lines) => lineChartCodeBlock(language, lines, chartOptions),
313
+ 'markdown-script': (language, lines) => markdownScriptCodeBlock(language, lines, chartOptions)
240
314
  },
241
- 'hashFn': (hashURL) => {
242
- // Decode the hash params
243
- const hashParams = decodeQueryString(hashURL.slice(1));
244
-
245
- // Fixup the "url" param if its relative
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(hashURL));
252
- },
253
- 'headerIds': true,
254
- url
315
+ urlFn,
316
+ 'headerIds': true
255
317
  }),
256
318
 
257
319
  // Popup menu burger
@@ -296,6 +358,8 @@ export class MarkdownUp extends ElementApplication {
296
358
  }
297
359
  ]
298
360
  };
361
+
362
+ return result;
299
363
  }
300
364
  }
301
365
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.5.1",
4
+ "version": "1.6.0",
5
5
  "description": "JavaScript Markdown viewer application",
6
6
  "keywords": [
7
7
  "markdown",
@@ -24,14 +24,14 @@
24
24
  "dependencies": {
25
25
  "element-app": "~1.1",
26
26
  "markdown-charts": "~0.9",
27
- "markdown-model": "~1.3",
27
+ "markdown-model": "~1.4",
28
28
  "schema-markdown": "~1.1",
29
- "schema-markdown-doc": "~1.2"
29
+ "schema-markdown-doc": "~1.3"
30
30
  },
31
31
  "devDependencies": {
32
- "ava": "^4.0.0-rc.1",
33
- "c8": "~7.10",
34
- "eslint": "~8.4",
32
+ "ava": "~4.0",
33
+ "c8": "~7.11",
34
+ "eslint": "~8.8",
35
35
  "jsdoc": "~3.6",
36
36
  "jsdom": "~19.0"
37
37
  }