markdown-up 1.2.0 → 1.3.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.
@@ -1,14 +1,13 @@
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';
4
+ import {ElementApplication, encodeHashID, getHashID} from 'element-app/lib/app.js';
5
+ import {decodeQueryString, encodeQueryString} from 'schema-markdown/index.js';
5
6
  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';
8
7
 
9
8
 
10
9
  // The application's hash parameter type model
11
- const appHashTypes = (new SchemaMarkdownParser(`\
10
+ const markdownUpHashTypes = `\
12
11
  #
13
12
  # **markdown-up** is a Markdown viewer application. Click the following link to learn more.
14
13
  #
@@ -43,20 +42,22 @@ union Command
43
42
 
44
43
  # Show the URL's Markdown text
45
44
  int(== 1) markdown
46
- `).types);
45
+ `;
47
46
 
48
47
 
49
48
  /**
50
- * The MarkdownUp application
49
+ * MarkdownUp, a web application for viewing Markdown files.
51
50
  *
52
- * @property {Object} window - The web browser window object
51
+ * Markdown up is an
52
+ * [Element Application]{@link https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html}.
53
+ *
54
+ * @extends ElementApplication
53
55
  * @property {number} fontSize - The font size, in points
54
56
  * @property {number} lineHeight - The line height, in em
55
57
  * @property {boolean} menu - If true, show the menu
56
58
  * @property {string} url - The resource URL
57
- * @property {Object} params - The validated hash parameters object
58
59
  */
59
- export class MarkdownUp {
60
+ export class MarkdownUp extends ElementApplication {
60
61
  /**
61
62
  * Create an application instance
62
63
  *
@@ -73,61 +74,21 @@ export class MarkdownUp {
73
74
  menu = true,
74
75
  url = 'README.md'
75
76
  } = {}) {
76
- this.window = window;
77
+ super(window, 'markdown-up', 'MarkdownUp', markdownUpHashTypes);
77
78
  this.fontSize = fontSize;
78
79
  this.lineHeight = lineHeight;
79
80
  this.menu = menu;
80
81
  this.url = url;
81
- this.params = null;
82
82
  }
83
83
 
84
84
  /**
85
- * Run the application
85
+ * The [Element Application pre-render callback]{@link
86
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#preRender}.
87
+ * The callback is used to set the document font size and line height.
86
88
  *
87
- * @param {Object} window - The web browser window object
88
- * @param {Object} [options = {}] - The application options
89
- * @returns {MarkdownUp}
89
+ * @override
90
90
  */
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
-
91
+ preRender() {
131
92
  // Set the font size
132
93
  const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
133
94
  this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
@@ -135,19 +96,15 @@ export class MarkdownUp {
135
96
  // Set the line height
136
97
  const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
137
98
  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
99
  }
149
100
 
150
- // Generate the application's element model
101
+ /**
102
+ * The [Element Application main entry point]{@link
103
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#helpElements}.
104
+ *
105
+ * @override
106
+ * @returns {Object} [MainResult]{@link https://craigahobbs.github.io/element-app/module-lib_app.html#~MainResult}
107
+ */
151
108
  async main() {
152
109
  // Load the text resource
153
110
  const url = 'url' in this.params ? this.params.url : this.url;
@@ -168,7 +125,7 @@ export class MarkdownUp {
168
125
  // Helper function to create a cycle link
169
126
  const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
170
127
  const value = paramName in this.params ? this.params[paramName] : defaultValue;
171
- const {attr} = appHashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
128
+ const {attr} = this.hashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
172
129
  let valueNew = value + delta;
173
130
  if (valueNew > attr.lte) {
174
131
  valueNew = attr.gte;
@@ -203,10 +160,14 @@ export class MarkdownUp {
203
160
  };
204
161
 
205
162
  // Render the text as Markdown
206
- const result = {
163
+ return {
164
+ 'title': markdownTitle,
207
165
  'elements': [
166
+ // Application hash parameter help?
208
167
  !('cmd' in this.params && 'help' in this.params.cmd)
209
- ? null : (new UserTypeElements(this.params)).getElements(appHashTypes, 'MarkdownUp'),
168
+ ? null : this.helpElements(),
169
+
170
+ // Render markdown?
210
171
  !('cmd' in this.params && 'markdown' in this.params.cmd)
211
172
  ? null : {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}},
212
173
 
@@ -225,12 +186,8 @@ export class MarkdownUp {
225
186
  hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
226
187
  }
227
188
 
228
- // Combine the application params and the hash URL params
229
- const hashParamStr = encodeQueryString({...this.params, ...hashParams});
230
-
231
189
  // Return the combined hash URL
232
- const hashId = getUrlHashID(hashURL);
233
- return `#${hashParamStr}${hashId !== null && hashParamStr !== '' ? '&' : ''}${hashId !== null ? hashId : ''}`;
190
+ return encodeHashID({...this.params, ...hashParams}, getHashID(hashURL));
234
191
  },
235
192
  'headerIds': true,
236
193
  url
@@ -278,26 +235,10 @@ export class MarkdownUp {
278
235
  }
279
236
  ]
280
237
  };
281
-
282
- // Set the page title
283
- if (markdownTitle !== null) {
284
- result.title = markdownTitle;
285
- }
286
-
287
- return result;
288
238
  }
289
239
  }
290
240
 
291
241
 
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
242
  // Icon sizes
302
243
  const burgerSize = '24';
303
244
  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.3.0",
5
5
  "description": "JavaScript Markdown viewer application",
6
6
  "keywords": [
7
7
  "markdown",
@@ -16,17 +16,16 @@
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": {
25
+ "element-app": "~1.0",
26
26
  "element-model": "~1.0",
27
27
  "markdown-model": "~1.2",
28
- "schema-markdown": "~1.0",
29
- "schema-markdown-doc": "~1.0"
28
+ "schema-markdown": "~1.0"
30
29
  },
31
30
  "devDependencies": {
32
31
  "ava": "^4.0.0-rc.1",
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';