markdown-up 1.0.8 → 1.2.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "1.0.8",
4
+ "version": "1.2.0",
5
5
  "description": "JavaScript Markdown viewer application",
6
6
  "keywords": [
7
7
  "markdown",
@@ -24,15 +24,15 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "element-model": "~1.0",
27
- "markdown-model": "~1.1",
27
+ "markdown-model": "~1.2",
28
28
  "schema-markdown": "~1.0",
29
29
  "schema-markdown-doc": "~1.0"
30
30
  },
31
31
  "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"
32
+ "ava": "^4.0.0-rc.1",
33
+ "c8": "~7.10",
34
+ "eslint": "~8.3",
35
+ "jsdoc": "~3.6",
36
+ "jsdom": "~18.1"
37
37
  }
38
38
  }
@@ -1,15 +1,23 @@
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';
4
+ import {SchemaMarkdownParser, decodeQueryString, encodeQueryString, validateType} from 'schema-markdown/index.js';
5
+ import {getBaseURL, getMarkdownTitle, isRelativeURL, markdownElements, parseMarkdown} from 'markdown-model/index.js';
6
6
  import {UserTypeElements} from 'schema-markdown-doc/index.js';
7
7
  import {renderElements} from 'element-model/index.js';
8
8
 
9
9
 
10
10
  // The application's hash parameter type model
11
- const appHashTypes = (new smd.SchemaMarkdownParser(`\
12
- # The MarkdownUp application hash parameters struct
11
+ const appHashTypes = (new SchemaMarkdownParser(`\
12
+ #
13
+ # **markdown-up** is a Markdown viewer application. Click the following link to learn more.
14
+ #
15
+ # [markdown-up](https://github.com/craigahobbs/markdown-up#readme)
16
+ #
17
+ # ## Hash Parameters
18
+ #
19
+ # The markdown-up application recognizes the following hash parameters:
20
+ #
13
21
  struct MarkdownUp
14
22
 
15
23
  # The resource URL
@@ -18,11 +26,23 @@ struct MarkdownUp
18
26
  # Optional command
19
27
  optional Command cmd
20
28
 
29
+ # The menu visibility (default is hidden)
30
+ optional int(== 1) menu
31
+
32
+ # The font size
33
+ optional int(>= 8, <= 18) fontSize
34
+
35
+ # The line height
36
+ optional int(>= 1, <= 2) lineHeight
37
+
21
38
  # Application command union
22
39
  union Command
23
40
 
24
41
  # Render the application's hash parameter documentation
25
42
  int(== 1) help
43
+
44
+ # Show the URL's Markdown text
45
+ int(== 1) markdown
26
46
  `).types);
27
47
 
28
48
 
@@ -30,31 +50,46 @@ union Command
30
50
  * The MarkdownUp application
31
51
  *
32
52
  * @property {Object} window - The web browser window object
33
- * @property {string} defaultURL - The default resource URL
53
+ * @property {number} fontSize - The font size, in points
54
+ * @property {number} lineHeight - The line height, in em
55
+ * @property {boolean} menu - If true, show the menu
56
+ * @property {string} url - The resource URL
34
57
  * @property {Object} params - The validated hash parameters object
35
58
  */
36
59
  export class MarkdownUp {
37
60
  /**
38
61
  * Create an application instance
39
62
  *
40
- * @property {Object} window - The web browser window object
41
- * @property {string} defaultURL - The default resource URL
63
+ * @param {Object} window - The web browser window object
64
+ * @param {Object} [options] - The application options
65
+ * @param {number} [options.fontSize = 12] - The font size, in points
66
+ * @param {number} [options.lineHeight = 1.2] - The line height, in em
67
+ * @param {boolean} [options.menu = true] - If true, show the menu
68
+ * @param {string} [options.url = 'README.md'] - The resource URL
42
69
  */
43
- constructor(window, defaultURL) {
70
+ constructor(window, {
71
+ fontSize = 12,
72
+ lineHeight = 1.2,
73
+ menu = true,
74
+ url = 'README.md'
75
+ } = {}) {
44
76
  this.window = window;
45
- this.defaultURL = defaultURL;
77
+ this.fontSize = fontSize;
78
+ this.lineHeight = lineHeight;
79
+ this.menu = menu;
80
+ this.url = url;
46
81
  this.params = null;
47
82
  }
48
83
 
49
84
  /**
50
85
  * Run the application
51
86
  *
52
- * @property {Object} window - The web browser window object
53
- * @property {string} [defaultURL='README.md'] - The default resource URL
87
+ * @param {Object} window - The web browser window object
88
+ * @param {Object} [options = {}] - The application options
54
89
  * @returns {MarkdownUp}
55
90
  */
56
- static async run(window, defaultURL = 'README.md') {
57
- const app = new MarkdownUp(window, defaultURL);
91
+ static async run(window, options = {}) {
92
+ const app = new MarkdownUp(window, options);
58
93
  await app.render();
59
94
  window.addEventListener('hashchange', () => app.render(), false);
60
95
  return app;
@@ -66,16 +101,16 @@ export class MarkdownUp {
66
101
  this.params = null;
67
102
 
68
103
  // Decode the params string
69
- const paramStrActual = paramStr !== null ? paramStr : this.window.location.hash.substring(1);
70
- const params = smd.decodeQueryString(paramStrActual);
104
+ const params = decodeQueryString(paramStr !== null ? paramStr : this.window.location.hash.slice(1));
71
105
 
72
106
  // Validate the params
73
- this.params = smd.validateType(appHashTypes, 'MarkdownUp', params);
107
+ this.params = validateType(appHashTypes, 'MarkdownUp', params);
74
108
  }
75
109
 
76
110
  // Render the application
77
111
  async render() {
78
112
  let result;
113
+ let isError = false;
79
114
  try {
80
115
  // Validate hash parameters
81
116
  const paramsPrev = this.params;
@@ -90,37 +125,243 @@ export class MarkdownUp {
90
125
  result = await this.main();
91
126
  } catch ({message}) {
92
127
  result = {'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}};
128
+ isError = true;
93
129
  }
94
130
 
131
+ // Set the font size
132
+ const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
133
+ this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
134
+
135
+ // Set the line height
136
+ const lineHeight = this.params !== null && 'lineHeight' in this.params ? this.params.lineHeight : this.lineHeight;
137
+ this.window.document.documentElement.style.setProperty('--markdown-model-line-height', `${lineHeight}em`);
138
+
95
139
  // Render the application
96
140
  this.window.document.title = 'title' in result ? result.title : 'MarkdownUp';
97
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
+ }
98
148
  }
99
149
 
100
150
  // Generate the application's element model
101
151
  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
152
  // 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)}`}`);
153
+ const url = 'url' in this.params ? this.params.url : this.url;
154
+ let text = null;
155
+ let markdownModel = null;
156
+ let markdownTitle = null;
157
+ if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
158
+ const response = await this.window.fetch(url);
159
+ if (!response.ok) {
160
+ const status = response.statusText;
161
+ throw new Error(`Could not fetch "${url}"${status === '' ? '' : `, ${JSON.stringify(status)}`}`);
162
+ }
163
+ text = await response.text();
164
+ markdownModel = parseMarkdown(text);
165
+ markdownTitle = getMarkdownTitle(markdownModel);
114
166
  }
115
- const text = await response.text();
167
+
168
+ // Helper function to create a cycle link
169
+ const linkCycle = (paramName, defaultValue, delta, fixed = null) => {
170
+ const value = paramName in this.params ? this.params[paramName] : defaultValue;
171
+ const {attr} = appHashTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
172
+ let valueNew = value + delta;
173
+ if (valueNew > attr.lte) {
174
+ valueNew = attr.gte;
175
+ }
176
+ valueNew = Math.max(attr.gte, Math.min(attr.lte, valueNew));
177
+ const params = {...this.params};
178
+ params[paramName] = fixed === null ? `${valueNew}` : `${valueNew.toFixed(fixed)}`;
179
+ return `#${encodeQueryString(params)}`;
180
+ };
181
+
182
+ // Helper function to create a toggle link
183
+ const linkCommandToggle = (command, value) => {
184
+ const params = {...this.params};
185
+ if ('cmd' in this.params && command in this.params.cmd) {
186
+ delete params.cmd;
187
+ } else {
188
+ params.cmd = {};
189
+ params.cmd[command] = `${value}`;
190
+ }
191
+ return `#${encodeQueryString(params)}`;
192
+ };
193
+
194
+ // Helper function to create a value-toggle link
195
+ const linkValueToggle = (name, value) => {
196
+ const params = {...this.params};
197
+ if (!(name in this.params)) {
198
+ params[name] = value;
199
+ } else {
200
+ delete params[name];
201
+ }
202
+ return `#${encodeQueryString(params)}`;
203
+ };
116
204
 
117
205
  // Render the text as Markdown
118
- const markdownModel = parseMarkdown(text);
119
- const markdownTitle = getMarkdownTitle(markdownModel);
120
- const result = {'elements': markdownElements(markdownModel, {'headerIds': true, url})};
206
+ const result = {
207
+ '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}},
212
+
213
+ // Add a top hash ID, if necessary
214
+ 'cmd' in this.params || Object.keys(this.params).length === 0
215
+ ? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
216
+
217
+ // Render the markdown
218
+ 'cmd' in this.params ? null : markdownElements(markdownModel, {
219
+ 'hashFn': (hashURL) => {
220
+ // Decode the hash params
221
+ const hashParams = decodeQueryString(hashURL.slice(1));
222
+
223
+ // Fixup the "url" param if its relative
224
+ if ('url' in hashParams && isRelativeURL(hashParams.url)) {
225
+ hashParams.url = `${getBaseURL(url)}${hashParams.url}`;
226
+ }
227
+
228
+ // Combine the application params and the hash URL params
229
+ const hashParamStr = encodeQueryString({...this.params, ...hashParams});
230
+
231
+ // Return the combined hash URL
232
+ const hashId = getUrlHashID(hashURL);
233
+ return `#${hashParamStr}${hashId !== null && hashParamStr !== '' ? '&' : ''}${hashId !== null ? hashId : ''}`;
234
+ },
235
+ 'headerIds': true,
236
+ url
237
+ }),
238
+
239
+ // Popup menu burger
240
+ !this.menu ? null : {
241
+ 'html': 'div',
242
+ 'attr': {'class': 'menu-burger'},
243
+ 'elem': [
244
+ {
245
+ 'html': 'a',
246
+ 'attr': {'href': linkValueToggle('menu', '1'), 'aria-label': 'Menu'},
247
+ 'elem': menuSVGElements
248
+ }
249
+ ]
250
+ },
251
+
252
+ // Popup menu
253
+ !this.menu || !('menu' in this.params) ? null : {
254
+ 'html': 'div',
255
+ 'attr': {'class': 'menu'},
256
+ 'elem': [
257
+ {
258
+ 'html': 'a',
259
+ 'attr': {'href': linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
260
+ 'elem': fontSizeSVGElements
261
+ },
262
+ {
263
+ 'html': 'a',
264
+ 'attr': {'href': linkCycle('lineHeight', this.lineHeight, 0.2, 1), 'aria-label': 'Line height'},
265
+ 'elem': lineHeightSVGElements
266
+ },
267
+ {
268
+ 'html': 'a',
269
+ 'attr': {'href': linkCommandToggle('markdown', 1), 'aria-label': 'Show Markdown'},
270
+ 'elem': markdownSVGElements
271
+ },
272
+ {
273
+ 'html': 'a',
274
+ 'attr': {'href': linkCommandToggle('help', 1), 'aria-label': 'Help'},
275
+ 'elem': helpSVGElements
276
+ }
277
+ ]
278
+ }
279
+ ]
280
+ };
281
+
282
+ // Set the page title
121
283
  if (markdownTitle !== null) {
122
284
  result.title = markdownTitle;
123
285
  }
286
+
124
287
  return result;
125
288
  }
126
289
  }
290
+
291
+
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
+ // Icon sizes
302
+ const burgerSize = '24';
303
+ const iconSize = '36';
304
+
305
+
306
+ // "menu" SVG element model
307
+ const menuSVGElements = {
308
+ 'svg': 'svg',
309
+ 'attr': {'width': burgerSize, 'height': burgerSize, 'viewBox': '0 0 24 24'},
310
+ 'elem': {
311
+ 'svg': 'path',
312
+ 'attr': {'d': 'M3,5 L21,5 M3,12 L21,12 M3,19 L21,19', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
313
+ }
314
+ };
315
+
316
+
317
+ // Font size cycle SVG element model
318
+ const fontSizeSVGElements = {
319
+ 'svg': 'svg',
320
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
321
+ 'elem': {
322
+ 'svg': 'path',
323
+ 'attr': {'d': 'M4,22 L10,2 L14,2 L20,22 M6,14 L18,14', 'stroke': 'black', 'stroke-width': '4', 'fill': 'none'}
324
+ }
325
+ };
326
+
327
+
328
+ // Line height cycle SVG element model
329
+ const lineHeightSVGElements = {
330
+ 'svg': 'svg',
331
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
332
+ 'elem': {
333
+ 'svg': 'path',
334
+ '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'}
335
+ }
336
+ };
337
+
338
+
339
+ // "markdown" SVG element model
340
+ const markdownSVGElements = {
341
+ 'svg': 'svg',
342
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
343
+ 'elem': [
344
+ {
345
+ 'svg': 'path',
346
+ 'attr': {'d': 'M4,2 L20,2 L20,22 L4,22 Z', 'stroke': 'black', 'stroke-width': '3', 'fill': 'none'}
347
+ },
348
+ {
349
+ 'svg': 'path',
350
+ 'attr': {
351
+ 'd': 'M7,7.5 L17,7.5 M7,12 L17,12 M7,16.5 L17,16.5',
352
+ 'stroke': 'black', 'stroke-width': '2', 'fill': 'none'
353
+ }
354
+ }
355
+ ]
356
+ };
357
+
358
+
359
+ // "help" SVG element model
360
+ const helpSVGElements = {
361
+ 'svg': 'svg',
362
+ 'attr': {'width': iconSize, 'height': iconSize, 'viewBox': '0 0 24 24'},
363
+ 'elem': {
364
+ 'svg': 'path',
365
+ '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'}
366
+ }
367
+ };