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