markdown-up 1.0.10 → 1.3.1

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,11 +25,22 @@ 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, '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
 
34
+ The following application options are defined:
35
+
36
+ - **fontSize** - The default font size, in points
37
+
38
+ - **lineHeight** - The default line height, in em
39
+
40
+ - **menu** - If false, the application menu is hidden
41
+
42
+ - **url** - The default Markdown text resource URL
43
+
33
44
 
34
45
  ## Viewing local Markdown files
35
46
 
@@ -39,8 +50,9 @@ To view local Markdown files using markdown-up, try the
39
50
 
40
51
  ## Development
41
52
 
42
- This project is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme). It was started
43
- 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):
44
56
 
45
57
  ```
46
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'
package/lib/app.js ADDED
@@ -0,0 +1,310 @@
1
+ // Licensed under the MIT License
2
+ // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
+
4
+ /** @module lib/app */
5
+
6
+ import {ElementApplication, encodeHashID, getHashID} from 'element-app/lib/app.js';
7
+ import {decodeQueryString, encodeQueryString} from 'schema-markdown/index.js';
8
+ import {getBaseURL, getMarkdownTitle, isRelativeURL, markdownElements, parseMarkdown} from 'markdown-model/index.js';
9
+
10
+
11
+ // The application's hash parameter type model
12
+ const markdownUpHashTypes = `\
13
+ #
14
+ # **markdown-up** is a Markdown viewer application. Click the following link to learn more.
15
+ #
16
+ # [markdown-up](https://github.com/craigahobbs/markdown-up#readme)
17
+ #
18
+ # ## Hash Parameters
19
+ #
20
+ # The markdown-up application recognizes the following hash parameters:
21
+ #
22
+ struct MarkdownUp
23
+
24
+ # The resource URL
25
+ optional string(len > 0) url
26
+
27
+ # Optional command
28
+ optional Command cmd
29
+
30
+ # The menu visibility (default is hidden)
31
+ optional int(== 1) menu
32
+
33
+ # The font size
34
+ optional int(>= 8, <= 18) fontSize
35
+
36
+ # The line height
37
+ optional int(>= 1, <= 2) lineHeight
38
+
39
+ # Application command union
40
+ union Command
41
+
42
+ # Display the application's hash parameter documentation
43
+ int(== 1) help
44
+
45
+ # Show the URL's Markdown text
46
+ int(== 1) markdown
47
+ `;
48
+
49
+
50
+ /**
51
+ * MarkdownUp, a web application for viewing Markdown files.
52
+ *
53
+ * MarkdownUp up is an
54
+ * [Element Application]{@link https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html}.
55
+ *
56
+ * @extends ElementApplication
57
+ * @property {number} fontSize - The font size, in points
58
+ * @property {number} lineHeight - The line height, in em
59
+ * @property {boolean} menu - If true, show the menu
60
+ * @property {string} url - The resource URL
61
+ */
62
+ export class MarkdownUp extends ElementApplication {
63
+ /**
64
+ * Create an application instance
65
+ *
66
+ * @param {Object} window - The web browser window object
67
+ * @param {Object} [options] - The application options
68
+ * @param {number} [options.fontSize = 12] - The font size, in points
69
+ * @param {number} [options.lineHeight = 1.2] - The line height, in em
70
+ * @param {boolean} [options.menu = true] - If true, show the menu
71
+ * @param {string} [options.url = 'README.md'] - The resource URL
72
+ */
73
+ constructor(window, {
74
+ fontSize = 12,
75
+ lineHeight = 1.2,
76
+ menu = true,
77
+ url = 'README.md'
78
+ } = {}) {
79
+ super(window, 'markdown-up', 'MarkdownUp', markdownUpHashTypes);
80
+ this.fontSize = fontSize;
81
+ this.lineHeight = lineHeight;
82
+ this.menu = menu;
83
+ this.url = url;
84
+ }
85
+
86
+ /**
87
+ * The [Element Application pre-render callback]{@link
88
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#preRender}.
89
+ * The callback is used to set the document font size and line height.
90
+ *
91
+ * @override
92
+ */
93
+ preRender() {
94
+ // Set the font size
95
+ const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
96
+ this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
97
+
98
+ // Set the line height
99
+ const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
100
+ this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
101
+ }
102
+
103
+ /**
104
+ * The [Element Application main entry point]{@link
105
+ * https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#helpElements}.
106
+ *
107
+ * @override
108
+ * @returns {Object} [MainResult]{@link https://craigahobbs.github.io/element-app/module-lib_app.html#~MainResult}
109
+ */
110
+ async main() {
111
+ // Load the text resource
112
+ const url = 'url' in this.params ? this.params.url : this.url;
113
+ let text = null;
114
+ let markdownModel = null;
115
+ let markdownTitle = null;
116
+ if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
117
+ const response = await this.window.fetch(url);
118
+ if (!response.ok) {
119
+ const status = response.statusText;
120
+ throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
121
+ }
122
+ text = await response.text();
123
+ markdownModel = parseMarkdown(text);
124
+ markdownTitle = getMarkdownTitle(markdownModel);
125
+ }
126
+
127
+ // Helper function to create a cycle link
128
+ const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
129
+ const value = paramName in this.params ? this.params[paramName] : defaultValue;
130
+ const {attr} = this.hashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
131
+ let valueNew = value + delta;
132
+ if (valueNew > attr.lte) {
133
+ valueNew = attr.gte;
134
+ }
135
+ valueNew = Math.max(attr.gte, Math.min(attr.lte, valueNew));
136
+ const params = {...this.params};
137
+ params[paramName] = fixed === null ? `${valueNew}` : `${valueNew.toFixed(fixed)}`;
138
+ return `#${encodeQueryString(params)}`;
139
+ };
140
+
141
+ // Helper function to create a toggle link
142
+ const linkCommandToggle = (command, value) => {
143
+ const params = {...this.params};
144
+ if ('cmd' in this.params && command in this.params.cmd) {
145
+ delete params.cmd;
146
+ } else {
147
+ params.cmd = {};
148
+ params.cmd[command] = `${value}`;
149
+ }
150
+ return `#${encodeQueryString(params)}`;
151
+ };
152
+
153
+ // Helper function to create a value-toggle link
154
+ const linkValueToggle = (name, value) => {
155
+ const params = {...this.params};
156
+ if (!(name in this.params)) {
157
+ params[name] = value;
158
+ } else {
159
+ delete params[name];
160
+ }
161
+ return `#${encodeQueryString(params)}`;
162
+ };
163
+
164
+ // Render the text as Markdown
165
+ return {
166
+ 'title': markdownTitle,
167
+ 'elements': [
168
+ // Application hash parameter help?
169
+ !('cmd' in this.params && 'help' in this.params.cmd)
170
+ ? null : this.helpElements(),
171
+
172
+ // Render markdown?
173
+ !('cmd' in this.params && 'markdown' in this.params.cmd)
174
+ ? null : {'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': text}},
175
+
176
+ // Add a top hash ID, if necessary
177
+ 'cmd' in this.params || Object.keys(this.params).length === 0
178
+ ? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
179
+
180
+ // Render the markdown
181
+ 'cmd' in this.params ? null : markdownElements(markdownModel, {
182
+ 'hashFn': (hashURL) => {
183
+ // Decode the hash params
184
+ const hashParams = decodeQueryString(hashURL.slice(1));
185
+
186
+ // Fixup the "url" param if its relative
187
+ if ('url' in hashParams && isRelativeURL(hashParams.url)) {
188
+ hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
189
+ }
190
+
191
+ // Return the combined hash URL
192
+ return encodeHashID({...this.params, ...hashParams}, getHashID(hashURL));
193
+ },
194
+ 'headerIds': true,
195
+ url
196
+ }),
197
+
198
+ // Popup menu burger
199
+ !this.menu ? null : {
200
+ 'html': 'div',
201
+ 'attr': {'class': 'menu-burger'},
202
+ 'elem': [
203
+ {
204
+ 'html': 'a',
205
+ 'attr': {'href': linkValueToggle('menu', '1'), 'aria-label': 'Menu'},
206
+ 'elem': menuSVGElements
207
+ }
208
+ ]
209
+ },
210
+
211
+ // Popup menu
212
+ !this.menu || !('menu' in this.params) ? null : {
213
+ 'html': 'div',
214
+ 'attr': {'class': 'menu'},
215
+ 'elem': [
216
+ {
217
+ 'html': 'a',
218
+ 'attr': {'href': linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
219
+ 'elem': fontSizeSVGElements
220
+ },
221
+ {
222
+ 'html': 'a',
223
+ 'attr': {'href': linkCycle('lineHeight', this.lineHeight, 0.2, 1), 'aria-label': 'Line height'},
224
+ 'elem': lineHeightSVGElements
225
+ },
226
+ {
227
+ 'html': 'a',
228
+ 'attr': {'href': linkCommandToggle('markdown', 1), 'aria-label': 'Show Markdown'},
229
+ 'elem': markdownSVGElements
230
+ },
231
+ {
232
+ 'html': 'a',
233
+ 'attr': {'href': linkCommandToggle('help', 1), 'aria-label': 'Help'},
234
+ 'elem': helpSVGElements
235
+ }
236
+ ]
237
+ }
238
+ ]
239
+ };
240
+ }
241
+ }
242
+
243
+
244
+ // Icon sizes
245
+ const burgerSize = '24';
246
+ const iconSize = '36';
247
+
248
+
249
+ // "menu" SVG element model
250
+ const menuSVGElements = {
251
+ 'svg': 'svg',
252
+ 'attr': {'width': burgerSize, 'height': burgerSize, 'viewBox': '0 0 24 24'},
253
+ 'elem': {
254
+ 'svg': 'path',
255
+ 'attr': {'d': 'M3,5 L21,5 M3,12 L21,12 M3,19 L21,19', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
256
+ }
257
+ };
258
+
259
+
260
+ // Font size cycle SVG element model
261
+ const fontSizeSVGElements = {
262
+ 'svg': 'svg',
263
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
264
+ 'elem': {
265
+ 'svg': 'path',
266
+ 'attr': {'d': 'M4,22 L10,2 L14,2 L20,22 M6,14 L18,14', 'stroke': 'black', 'stroke-width': '4', 'fill': 'none'}
267
+ }
268
+ };
269
+
270
+
271
+ // Line height cycle SVG element model
272
+ const lineHeightSVGElements = {
273
+ 'svg': 'svg',
274
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
275
+ 'elem': {
276
+ 'svg': 'path',
277
+ 'attr': {'d': 'M2,3 L22,3 M2,9 L22,9 M2,15 L22,15 M2,21 L22,21', 'stroke': 'black', 'stroke-width': '2', 'fill': 'none'}
278
+ }
279
+ };
280
+
281
+
282
+ // "markdown" SVG element model
283
+ const markdownSVGElements = {
284
+ 'svg': 'svg',
285
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
286
+ 'elem': [
287
+ {
288
+ 'svg': 'path',
289
+ 'attr': {'d': 'M4,2 L20,2 L20,22 L4,22 Z', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
290
+ },
291
+ {
292
+ 'svg': 'path',
293
+ 'attr': {
294
+ 'd': 'M7,7.5 L17,7.5 M7,12 L17,12 M7,16.5 L17,16.5',
295
+ 'stroke': 'black', 'stroke-width': '2', 'fill': 'none'
296
+ }
297
+ }
298
+ ]
299
+ };
300
+
301
+
302
+ // "help" SVG element model
303
+ const helpSVGElements = {
304
+ 'svg': 'svg',
305
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
306
+ 'elem': {
307
+ 'svg': 'path',
308
+ 'attr': {'d': 'M7,9 L7,4 L17,4 L17,12 L12,12 L12,16 M12,19 L12,22', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
309
+ }
310
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.0.10",
4
+ "version": "1.3.1",
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": {
25
+ "element-app": "~1.0",
26
26
  "element-model": "~1.0",
27
- "markdown-model": "~1.1",
28
- "schema-markdown": "~1.0",
29
- "schema-markdown-doc": "~1.0"
27
+ "markdown-model": "~1.2",
28
+ "schema-markdown": "~1.0"
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';
@@ -1,130 +0,0 @@
1
- // Licensed under the MIT License
2
- // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
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 {renderElements} from 'element-model/index.js';
8
-
9
-
10
- // The application's hash parameter type model
11
- const appHashTypes = (new smd.SchemaMarkdownParser(`\
12
- # The MarkdownUp application hash parameters struct
13
- struct MarkdownUp
14
-
15
- # The resource URL
16
- optional string(len > 0) url
17
-
18
- # Optional command
19
- optional Command cmd
20
-
21
- # Application command union
22
- union Command
23
-
24
- # Render the application's hash parameter documentation
25
- int(== 1) help
26
- `).types);
27
-
28
-
29
- /**
30
- * The MarkdownUp application
31
- *
32
- * @property {Object} window - The web browser window object
33
- * @property {string} defaultURL - The default resource URL
34
- * @property {Object} params - The validated hash parameters object
35
- */
36
- export class MarkdownUp {
37
- /**
38
- * Create an application instance
39
- *
40
- * @property {Object} window - The web browser window object
41
- * @property {string} defaultURL - The default resource URL
42
- */
43
- constructor(window, defaultURL) {
44
- this.window = window;
45
- this.defaultURL = defaultURL;
46
- this.params = null;
47
- }
48
-
49
- /**
50
- * Run the application
51
- *
52
- * @property {Object} window - The web browser window object
53
- * @property {string} [defaultURL='README.md'] - The default resource URL
54
- * @returns {MarkdownUp}
55
- */
56
- static async run(window, defaultURL = 'README.md') {
57
- const app = new MarkdownUp(window, defaultURL);
58
- await app.render();
59
- window.addEventListener('hashchange', () => app.render(), false);
60
- return app;
61
- }
62
-
63
- // Helper function to parse and validate the hash parameters
64
- updateParams(paramStr = null) {
65
- // Clear, then validate the hash parameters (may throw)
66
- this.params = null;
67
-
68
- // Decode the params string
69
- const paramStrActual = paramStr !== null ? paramStr : this.window.location.hash.substring(1);
70
- const params = smd.decodeQueryString(paramStrActual);
71
-
72
- // Validate the params
73
- this.params = smd.validateType(appHashTypes, 'MarkdownUp', params);
74
- }
75
-
76
- // Render the application
77
- async render() {
78
- let result;
79
- try {
80
- // Validate hash parameters
81
- const paramsPrev = this.params;
82
- this.updateParams();
83
-
84
- // Skip the render if the page params haven't changed
85
- if (paramsPrev !== null && JSON.stringify(paramsPrev) === JSON.stringify(this.params)) {
86
- return;
87
- }
88
-
89
- // Render the application elements
90
- result = await this.main();
91
- } catch ({message}) {
92
- result = {'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}};
93
- }
94
-
95
- // Render the application
96
- this.window.document.title = 'title' in result ? result.title : 'MarkdownUp';
97
- renderElements(this.window.document.body, result.elements);
98
- }
99
-
100
- // Generate the application's element model
101
- async main() {
102
- // Application command?
103
- if ('cmd' in this.params) {
104
- // 'help' in this.params.cmd
105
- return {'elements': (new UserTypeElements(this.params)).getElements(appHashTypes, 'MarkdownUp')};
106
- }
107
-
108
- // Load the text resource
109
- const url = 'url' in this.params ? this.params.url : this.defaultURL;
110
- const response = await this.window.fetch(url);
111
- if (!response.ok) {
112
- const status = response.statusText;
113
- throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
114
- }
115
- const text = await response.text();
116
-
117
- // Render the text as Markdown
118
- const markdownModel = parseMarkdown(text);
119
- const markdownTitle = getMarkdownTitle(markdownModel);
120
- const result = {'elements': markdownElements(markdownModel, {
121
- 'hashPrefix': smd.encodeQueryString(this.params),
122
- 'headerIds': true,
123
- url
124
- })};
125
- if (markdownTitle !== null) {
126
- result.title = markdownTitle;
127
- }
128
- return result;
129
- }
130
- }