markdown-up 1.1.0 → 1.4.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,15 +1,18 @@
1
1
  // Licensed under the MIT License
2
2
  // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
3
 
4
- import * as smd from 'schema-markdown/index.js';
5
- import {getMarkdownTitle, markdownElements, parseMarkdown} from 'markdown-model/index.js';
6
- import {UserTypeElements} from 'schema-markdown-doc/index.js';
7
- import {encodeQueryString} from 'schema-markdown/index.js';
8
- 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 {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
8
+ import {getBaseURL, isRelativeURL, markdownElements} from 'markdown-model/lib/elements.js';
9
+ import {UserTypeElements} from 'schema-markdown-doc/lib/userTypeElements.js';
10
+ import {getMarkdownTitle} from 'markdown-model/lib/markdownModel.js';
11
+ import {parseMarkdown} from 'markdown-model/lib/parser.js';
9
12
 
10
13
 
11
14
  // The application's hash parameter type model
12
- const appHashTypes = (new smd.SchemaMarkdownParser(`\
15
+ const markdownUpHashTypes = `\
13
16
  #
14
17
  # **markdown-up** is a Markdown viewer application. Click the following link to learn more.
15
18
  #
@@ -39,25 +42,28 @@ struct MarkdownUp
39
42
  # Application command union
40
43
  union Command
41
44
 
42
- # Render the application's hash parameter documentation
45
+ # Display the application's hash parameter documentation
43
46
  int(== 1) help
44
47
 
45
48
  # Show the URL's Markdown text
46
49
  int(== 1) markdown
47
- `).types);
50
+ `;
48
51
 
49
52
 
50
53
  /**
51
- * The MarkdownUp application
54
+ * MarkdownUp, a web application for viewing Markdown files
55
+ *
56
+ * The MarkdownUp class extends the element-app
57
+ * [ElementApplication]{@link https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html}
58
+ * class.
52
59
  *
53
- * @property {Object} window - The web browser window object
60
+ * @extends ElementApplication
54
61
  * @property {number} fontSize - The font size, in points
55
62
  * @property {number} lineHeight - The line height, in em
56
63
  * @property {boolean} menu - If true, show the menu
57
64
  * @property {string} url - The resource URL
58
- * @property {Object} params - The validated hash parameters object
59
65
  */
60
- export class MarkdownUp {
66
+ export class MarkdownUp extends ElementApplication {
61
67
  /**
62
68
  * Create an application instance
63
69
  *
@@ -74,60 +80,21 @@ export class MarkdownUp {
74
80
  menu = true,
75
81
  url = 'README.md'
76
82
  } = {}) {
77
- this.window = window;
83
+ super(window, 'markdown-up', 'MarkdownUp', markdownUpHashTypes);
78
84
  this.fontSize = fontSize;
79
85
  this.lineHeight = lineHeight;
80
86
  this.menu = menu;
81
87
  this.url = url;
82
- this.params = null;
83
88
  }
84
89
 
85
90
  /**
86
- * Run the application
91
+ * The [Element Application pre-render callback]{@link
92
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#preRender}.
93
+ * The callback is used to set the document font size and line height.
87
94
  *
88
- * @param {Object} window - The web browser window object
89
- * @param {Object} [options = {}] - The application options
90
- * @returns {MarkdownUp}
95
+ * @override
91
96
  */
92
- static async run(window, options = {}) {
93
- const app = new MarkdownUp(window, options);
94
- await app.render();
95
- window.addEventListener('hashchange', () => app.render(), false);
96
- return app;
97
- }
98
-
99
- // Helper function to parse and validate the hash parameters
100
- updateParams(paramStr = null) {
101
- // Clear, then validate the hash parameters (may throw)
102
- this.params = null;
103
-
104
- // Decode the params string
105
- const paramStrActual = paramStr !== null ? paramStr : this.window.location.hash.substring(1);
106
- const params = smd.decodeQueryString(paramStrActual);
107
-
108
- // Validate the params
109
- this.params = smd.validateType(appHashTypes, 'MarkdownUp', params);
110
- }
111
-
112
- // Render the application
113
- async render() {
114
- let result;
115
- try {
116
- // Validate hash parameters
117
- const paramsPrev = this.params;
118
- this.updateParams();
119
-
120
- // Skip the render if the page params haven't changed
121
- if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
122
- return;
123
- }
124
-
125
- // Render the application elements
126
- result = await this.main();
127
- } catch ({message}) {
128
- result = {'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}};
129
- }
130
-
97
+ preRender() {
131
98
  // Set the font size
132
99
  const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
133
100
  this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
@@ -135,20 +102,29 @@ export class MarkdownUp {
135
102
  // Set the line height
136
103
  const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
137
104
  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
105
  }
143
106
 
144
- // Generate the application's element model
107
+ /**
108
+ * The [Element Application main entry point]{@link
109
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#main}.
110
+ *
111
+ * @override
112
+ * @returns {Object} [MainResult]{@link https://craigahobbs.github.io/element-app/module-lib_app.html#~MainResult}
113
+ */
145
114
  async main() {
115
+ // Help?
116
+ if ('cmd' in this.params && 'help' in this.params.cmd) {
117
+ return {
118
+ 'elements': new UserTypeElements(this.params).getElements(this.hashTypes, this.hashType)
119
+ };
120
+ }
121
+
146
122
  // Load the text resource
147
123
  const url = 'url' in this.params ? this.params.url : this.url;
148
124
  let text = null;
149
125
  let markdownModel = null;
150
126
  let markdownTitle = null;
151
- if (!('cmd' in this.params) || !('help' in this.params.cmd)) {
127
+ if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
152
128
  const response = await this.window.fetch(url);
153
129
  if (!response.ok) {
154
130
  const status = response.statusText;
@@ -162,7 +138,7 @@ export class MarkdownUp {
162
138
  // Helper function to create a cycle link
163
139
  const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
164
140
  const value = paramName in this.params ? this.params[paramName] : defaultValue;
165
- const {attr} = appHashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
141
+ const {attr} = this.hashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
166
142
  let valueNew = value + delta;
167
143
  if (valueNew > attr.lte) {
168
144
  valueNew = attr.gte;
@@ -197,19 +173,36 @@ export class MarkdownUp {
197
173
  };
198
174
 
199
175
  // Render the text as Markdown
200
- const result = {
176
+ return {
177
+ 'title': markdownTitle,
201
178
  'elements': [
202
- 'cmd' in this.params
203
- ? (
204
- 'markdown' in this.params.cmd
205
- ? {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}}
206
- : (new UserTypeElements(this.params)).getElements(appHashTypes, 'MarkdownUp')
207
- )
208
- : markdownElements(markdownModel, {
209
- 'hashPrefix': smd.encodeQueryString(this.params),
210
- 'headerIds': true,
211
- url
212
- }),
179
+ // Render markdown?
180
+ !('cmd' in this.params && 'markdown' in this.params.cmd)
181
+ ? null : {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}},
182
+
183
+ // Add a top hash ID, if necessary
184
+ 'cmd' in this.params || Object.keys(this.params).length === 0
185
+ ? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
186
+
187
+ // Render the markdown
188
+ 'cmd' in this.params ? null : markdownElements(markdownModel, {
189
+ 'hashFn': (hashURL) => {
190
+ // Decode the hash params
191
+ const hashParams = decodeQueryString(hashURL.slice(1));
192
+
193
+ // Fixup the "url" param if its relative
194
+ if ('url' in hashParams && isRelativeURL(hashParams.url)) {
195
+ hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
196
+ }
197
+
198
+ // Return the combined hash URL
199
+ return encodeHashID({...this.params, ...hashParams}, getHashID(hashURL));
200
+ },
201
+ 'headerIds': true,
202
+ url
203
+ }),
204
+
205
+ // Popup menu burger
213
206
  !this.menu ? null : {
214
207
  'html': 'div',
215
208
  'attr': {'class': 'menu-burger'},
@@ -221,6 +214,8 @@ export class MarkdownUp {
221
214
  }
222
215
  ]
223
216
  },
217
+
218
+ // Popup menu
224
219
  !this.menu || !('menu' in this.params) ? null : {
225
220
  'html': 'div',
226
221
  'attr': {'class': 'menu'},
@@ -249,10 +244,6 @@ export class MarkdownUp {
249
244
  }
250
245
  ]
251
246
  };
252
- if (markdownTitle !== null) {
253
- result.title = markdownTitle;
254
- }
255
- return result;
256
247
  }
257
248
  }
258
249
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.1.0",
4
+ "version": "1.4.0",
5
5
  "description": "JavaScript Markdown viewer application",
6
6
  "keywords": [
7
7
  "markdown",
@@ -16,23 +16,22 @@
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.1",
28
- "schema-markdown": "~1.0",
29
- "schema-markdown-doc": "~1.0"
25
+ "element-app": "~1.1",
26
+ "markdown-model": "~1.3",
27
+ "schema-markdown": "~1.1",
28
+ "schema-markdown-doc": "~1.2"
30
29
  },
31
30
  "devDependencies": {
32
- "ava": "4.0.0-alpha.2",
33
- "c8": "7.9.0",
34
- "eslint": "7.32.0",
35
- "jsdoc": "3.6.7",
36
- "jsdom": "14.1.0"
31
+ "ava": "^4.0.0-rc.1",
32
+ "c8": "~7.10",
33
+ "eslint": "~8.3",
34
+ "jsdoc": "~3.6",
35
+ "jsdom": "~18.1"
37
36
  }
38
37
  }
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';