markdown-up 1.2.0 → 1.5.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/README.md CHANGED
@@ -25,8 +25,9 @@ Alternatively, you can change the default Markdown file by updating the markdown
25
25
 
26
26
  ```
27
27
  <script type="module">
28
- import {MarkdownUp} from 'https://craigahobbs.github.io/markdown-up/markdown-up/index.js';
29
- MarkdownUp.run(window, {'url': 'other.md'});
28
+ import {MarkdownUp} from 'https://craigahobbs.github.io/markdown-up/lib/app.js';
29
+ const app = new MarkdownUp(window, {'url': 'other.md'});
30
+ app.run();
30
31
  </script>
31
32
  ```
32
33
 
@@ -49,8 +50,9 @@ To view local Markdown files using markdown-up, try the
49
50
 
50
51
  ## Development
51
52
 
52
- This project is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme). It was started
53
- using [javascript-template](https://github.com/craigahobbs/javascript-template#readme) as follows:
53
+ markdown-up is an [Element Application](https://github.com/craigahobbs/element-app#readme).
54
+ It is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme)
55
+ and it was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme):
54
56
 
55
57
  ```
56
58
  template-specialize javascript-template/template/ markdown-up/ -k package markdown-up -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs'
@@ -1,14 +1,20 @@
1
1
  // Licensed under the MIT License
2
2
  // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
3
 
4
- import {SchemaMarkdownParser, decodeQueryString, encodeQueryString, validateType} from 'schema-markdown/index.js';
5
- import {getBaseURL, getMarkdownTitle, isRelativeURL, markdownElements, parseMarkdown} from 'markdown-model/index.js';
6
- import {UserTypeElements} from 'schema-markdown-doc/index.js';
7
- import {renderElements} from 'element-model/index.js';
4
+ /** @module lib/app */
5
+
6
+ import {ElementApplication, encodeHashID, getHashID} from 'element-app/lib/app.js';
7
+ import {barChartCodeBlock, dataTableCodeBlock, lineChartCodeBlock} from 'markdown-charts/lib/codeBlock.js';
8
+ import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
9
+ import {getBaseURL, isRelativeURL, markdownElements} from 'markdown-model/lib/elements.js';
10
+ import {UserTypeElements} from 'schema-markdown-doc/lib/userTypeElements.js';
11
+ import {chartModel} from 'markdown-charts/lib/model.js';
12
+ import {getMarkdownTitle} from 'markdown-model/lib/markdownModel.js';
13
+ import {parseMarkdown} from 'markdown-model/lib/parser.js';
8
14
 
9
15
 
10
16
  // The application's hash parameter type model
11
- const appHashTypes = (new SchemaMarkdownParser(`\
17
+ const markdownUpHashTypes = `\
12
18
  #
13
19
  # **markdown-up** is a Markdown viewer application. Click the following link to learn more.
14
20
  #
@@ -35,28 +41,55 @@ struct MarkdownUp
35
41
  # The line height
36
42
  optional int(>= 1, <= 2) lineHeight
37
43
 
44
+ # Variable collection
45
+ optional FieldValue{len > 0} variables
46
+
47
+
48
+ # Field value union
49
+ union FieldValue
50
+
51
+ # A datetime value
52
+ datetime(nullable) datetime
53
+
54
+ # A number value
55
+ float(nullable) number
56
+
57
+ # A string value
58
+ string(nullable) string
59
+
60
+
38
61
  # Application command union
39
62
  union Command
40
63
 
41
- # Render the application's hash parameter documentation
42
- int(== 1) help
64
+ # Display the application's hash parameter documentation
65
+ HelpType help
43
66
 
44
67
  # Show the URL's Markdown text
45
68
  int(== 1) markdown
46
- `).types);
69
+
70
+ # Help type enum
71
+ enum HelpType
72
+ Help
73
+ Bar
74
+ Line
75
+ Table
76
+ `;
47
77
 
48
78
 
49
79
  /**
50
- * The MarkdownUp application
80
+ * MarkdownUp, a web application for viewing Markdown files
51
81
  *
52
- * @property {Object} window - The web browser window object
82
+ * The MarkdownUp class extends the element-app
83
+ * [ElementApplication]{@link https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html}
84
+ * class.
85
+ *
86
+ * @extends ElementApplication
53
87
  * @property {number} fontSize - The font size, in points
54
88
  * @property {number} lineHeight - The line height, in em
55
89
  * @property {boolean} menu - If true, show the menu
56
90
  * @property {string} url - The resource URL
57
- * @property {Object} params - The validated hash parameters object
58
91
  */
59
- export class MarkdownUp {
92
+ export class MarkdownUp extends ElementApplication {
60
93
  /**
61
94
  * Create an application instance
62
95
  *
@@ -73,61 +106,21 @@ export class MarkdownUp {
73
106
  menu = true,
74
107
  url = 'README.md'
75
108
  } = {}) {
76
- this.window = window;
109
+ super(window, 'markdown-up', 'MarkdownUp', markdownUpHashTypes);
77
110
  this.fontSize = fontSize;
78
111
  this.lineHeight = lineHeight;
79
112
  this.menu = menu;
80
113
  this.url = url;
81
- this.params = null;
82
114
  }
83
115
 
84
116
  /**
85
- * Run the application
117
+ * The [Element Application pre-render callback]{@link
118
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#preRender}.
119
+ * The callback is used to set the document font size and line height.
86
120
  *
87
- * @param {Object} window - The web browser window object
88
- * @param {Object} [options = {}] - The application options
89
- * @returns {MarkdownUp}
121
+ * @override
90
122
  */
91
- static async run(window, options = {}) {
92
- const app = new MarkdownUp(window, options);
93
- await app.render();
94
- window.addEventListener('hashchange', () => app.render(), false);
95
- return app;
96
- }
97
-
98
- // Helper function to parse and validate the hash parameters
99
- updateParams(paramStr = null) {
100
- // Clear, then validate the hash parameters (may throw)
101
- this.params = null;
102
-
103
- // Decode the params string
104
- const params = decodeQueryString(paramStr !== null ? paramStr : this.window.location.hash.slice(1));
105
-
106
- // Validate the params
107
- this.params = validateType(appHashTypes, 'MarkdownUp', params);
108
- }
109
-
110
- // Render the application
111
- async render() {
112
- let result;
113
- let isError = false;
114
- try {
115
- // Validate hash parameters
116
- const paramsPrev = this.params;
117
- this.updateParams();
118
-
119
- // Skip the render if the page params haven't changed
120
- if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
121
- return;
122
- }
123
-
124
- // Render the application elements
125
- result = await this.main();
126
- } catch ({message}) {
127
- result = {'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}};
128
- isError = true;
129
- }
130
-
123
+ preRender() {
131
124
  // Set the font size
132
125
  const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
133
126
  this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
@@ -135,19 +128,15 @@ export class MarkdownUp {
135
128
  // Set the line height
136
129
  const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
137
130
  this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
138
-
139
- // Render the application
140
- this.window.document.title = 'title' in result ? result.title : 'MarkdownUp';
141
- renderElements(this.window.document.body, result.elements);
142
-
143
- // If there is a URL hash ID, re-navigate to go there since it was just rendered. After the
144
- // first render, re-render is short-circuited by the unchanged hash param check above.
145
- if (!isError && getUrlHashID(this.window.location.hash) !== null) {
146
- this.window.location.href = this.window.location.hash;
147
- }
148
131
  }
149
132
 
150
- // Generate the application's element model
133
+ /**
134
+ * The [Element Application main entry point]{@link
135
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#main}.
136
+ *
137
+ * @override
138
+ * @returns {Object} [MainResult]{@link https://craigahobbs.github.io/element-app/module-lib_app.html#~MainResult}
139
+ */
151
140
  async main() {
152
141
  // Load the text resource
153
142
  const url = 'url' in this.params ? this.params.url : this.url;
@@ -165,10 +154,29 @@ export class MarkdownUp {
165
154
  markdownTitle = getMarkdownTitle(markdownModel);
166
155
  }
167
156
 
157
+ // Command?
158
+ let cmdElements = null;
159
+ if ('cmd' in this.params) {
160
+ // Display markdown?
161
+ if ('markdown' in this.params.cmd) {
162
+ cmdElements = {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}};
163
+
164
+ // Help
165
+ } else if (this.params.cmd.help === 'Bar') {
166
+ cmdElements = new UserTypeElements(this.params).getElements(chartModel.types, `BarChart`);
167
+ } else if (this.params.cmd.help === 'Line') {
168
+ cmdElements = new UserTypeElements(this.params).getElements(chartModel.types, `LineChart`);
169
+ } else if (this.params.cmd.help === 'Table') {
170
+ cmdElements = new UserTypeElements(this.params).getElements(chartModel.types, `DataTable`);
171
+ } else {
172
+ cmdElements = new UserTypeElements(this.params).getElements(this.hashTypes, this.hashType);
173
+ }
174
+ }
175
+
168
176
  // Helper function to create a cycle link
169
177
  const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
170
178
  const value = paramName in this.params ? this.params[paramName] : defaultValue;
171
- const {attr} = appHashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
179
+ const {attr} = this.hashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
172
180
  let valueNew = value + delta;
173
181
  if (valueNew > attr.lte) {
174
182
  valueNew = attr.gte;
@@ -203,19 +211,29 @@ export class MarkdownUp {
203
211
  };
204
212
 
205
213
  // Render the text as Markdown
206
- const result = {
214
+ const chartOptions = {
215
+ 'fontSize': 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize),
216
+ url,
217
+ 'variables': 'variables' in this.params ? this.params.variables : {},
218
+ 'window': this.window
219
+ };
220
+ return {
221
+ 'title': markdownTitle,
207
222
  'elements': [
208
- !('cmd' in this.params && 'help' in this.params.cmd)
209
- ? null : (new UserTypeElements(this.params)).getElements(appHashTypes, 'MarkdownUp'),
210
- !('cmd' in this.params && 'markdown' in this.params.cmd)
211
- ? null : {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}},
223
+ // Command?
224
+ cmdElements !== null ? cmdElements : null,
212
225
 
213
226
  // Add a top hash ID, if necessary
214
- 'cmd' in this.params || Object.keys(this.params).length === 0
227
+ cmdElements !== null || Object.keys(this.params).length === 0
215
228
  ? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
216
229
 
217
230
  // Render the markdown
218
- 'cmd' in this.params ? null : markdownElements(markdownModel, {
231
+ cmdElements !== null ? null : markdownElements(markdownModel, {
232
+ 'codeBlocks': {
233
+ 'bar-chart': (language, lines) => barChartCodeBlock(language, lines, chartOptions),
234
+ 'data-table': (language, lines) => dataTableCodeBlock(language, lines, chartOptions),
235
+ 'line-chart': (language, lines) => lineChartCodeBlock(language, lines, chartOptions)
236
+ },
219
237
  'hashFn': (hashURL) => {
220
238
  // Decode the hash params
221
239
  const hashParams = decodeQueryString(hashURL.slice(1));
@@ -225,12 +243,8 @@ export class MarkdownUp {
225
243
  hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
226
244
  }
227
245
 
228
- // Combine the application params and the hash URL params
229
- const hashParamStr = encodeQueryString({...this.params, ...hashParams});
230
-
231
246
  // Return the combined hash URL
232
- const hashId = getUrlHashID(hashURL);
233
- return `#${hashParamStr}${hashId !== null && hashParamStr !== '' ? '&' : ''}${hashId !== null ? hashId : ''}`;
247
+ return encodeHashID({...this.params, ...hashParams}, getHashID(hashURL));
234
248
  },
235
249
  'headerIds': true,
236
250
  url
@@ -271,33 +285,17 @@ export class MarkdownUp {
271
285
  },
272
286
  {
273
287
  'html': 'a',
274
- 'attr': {'href': linkCommandToggle('help', 1), 'aria-label': 'Help'},
288
+ 'attr': {'href': linkCommandToggle('help', 'Help'), 'aria-label': 'Help'},
275
289
  'elem': helpSVGElements
276
290
  }
277
291
  ]
278
292
  }
279
293
  ]
280
294
  };
281
-
282
- // Set the page title
283
- if (markdownTitle !== null) {
284
- result.title = markdownTitle;
285
- }
286
-
287
- return result;
288
295
  }
289
296
  }
290
297
 
291
298
 
292
- // Helper function to get a URL's hash ID
293
- function getUrlHashID(url) {
294
- const matchId = url.match(rUrlHashId);
295
- return matchId !== null ? matchId[1] : null;
296
- }
297
-
298
- const rUrlHashId = /[#&]([^=]+)$/;
299
-
300
-
301
299
  // Icon sizes
302
300
  const burgerSize = '24';
303
301
  const iconSize = '36';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.2.0",
4
+ "version": "1.5.0",
5
5
  "description": "JavaScript Markdown viewer application",
6
6
  "keywords": [
7
7
  "markdown",
@@ -16,23 +16,23 @@
16
16
  "url": "https://github.com/craigahobbs"
17
17
  },
18
18
  "files": [
19
- "index.js",
20
- "src/markdown-up/**/*"
19
+ "lib/**/*"
21
20
  ],
22
21
  "scripts": {
23
- "test": "ava -v 'src/tests/**/*.js'"
22
+ "test": "ava -v test/"
24
23
  },
25
24
  "dependencies": {
26
- "element-model": "~1.0",
27
- "markdown-model": "~1.2",
28
- "schema-markdown": "~1.0",
29
- "schema-markdown-doc": "~1.0"
25
+ "element-app": "~1.1",
26
+ "markdown-charts": "~0.9",
27
+ "markdown-model": "~1.3",
28
+ "schema-markdown": "~1.1",
29
+ "schema-markdown-doc": "~1.2"
30
30
  },
31
31
  "devDependencies": {
32
32
  "ava": "^4.0.0-rc.1",
33
33
  "c8": "~7.10",
34
- "eslint": "~8.3",
34
+ "eslint": "~8.4",
35
35
  "jsdoc": "~3.6",
36
- "jsdom": "~18.1"
36
+ "jsdom": "~19.0"
37
37
  }
38
38
  }
package/index.js DELETED
@@ -1,4 +0,0 @@
1
- // Licensed under the MIT License
2
- // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
-
4
- export * from './src/markdown-up/index.js';
@@ -1,4 +0,0 @@
1
- // Licensed under the MIT License
2
- // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
-
4
- export {MarkdownUp} from './markdownUp.js';