markdown-up 1.1.0 → 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/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.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.9.0",
34
- "eslint": "7.32.0",
35
- "jsdoc": "3.6.7",
36
- "jsdom": "14.1.0"
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,14 @@
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
- import {encodeQueryString} from 'schema-markdown/index.js';
8
7
  import {renderElements} from 'element-model/index.js';
9
8
 
10
9
 
11
10
  // The application's hash parameter type model
12
- const appHashTypes = (new smd.SchemaMarkdownParser(`\
11
+ const appHashTypes = (new SchemaMarkdownParser(`\
13
12
  #
14
13
  # **markdown-up** is a Markdown viewer application. Click the following link to learn more.
15
14
  #
@@ -102,16 +101,16 @@ export class MarkdownUp {
102
101
  this.params = null;
103
102
 
104
103
  // Decode the params string
105
- const paramStrActual = paramStr !== null ? paramStr : this.window.location.hash.substring(1);
106
- const params = smd.decodeQueryString(paramStrActual);
104
+ const params = decodeQueryString(paramStr !== null ? paramStr : this.window.location.hash.slice(1));
107
105
 
108
106
  // Validate the params
109
- this.params = smd.validateType(appHashTypes, 'MarkdownUp', params);
107
+ this.params = validateType(appHashTypes, 'MarkdownUp', params);
110
108
  }
111
109
 
112
110
  // Render the application
113
111
  async render() {
114
112
  let result;
113
+ let isError = false;
115
114
  try {
116
115
  // Validate hash parameters
117
116
  const paramsPrev = this.params;
@@ -126,6 +125,7 @@ export class MarkdownUp {
126
125
  result = await this.main();
127
126
  } catch ({message}) {
128
127
  result = {'elements': {'html': 'p', 'elem': {'text': `Error: ${message}`}}};
128
+ isError = true;
129
129
  }
130
130
 
131
131
  // Set the font size
@@ -139,6 +139,12 @@ export class MarkdownUp {
139
139
  // Render the application
140
140
  this.window.document.title = 'title' in result ? result.title : 'MarkdownUp';
141
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
+ }
142
148
  }
143
149
 
144
150
  // Generate the application's element model
@@ -148,7 +154,7 @@ export class MarkdownUp {
148
154
  let text = null;
149
155
  let markdownModel = null;
150
156
  let markdownTitle = null;
151
- if (!('cmd' in this.params) || !('help' in this.params.cmd)) {
157
+ if (!('cmd' in this.params) || 'markdown' in this.params.cmd) {
152
158
  const response = await this.window.fetch(url);
153
159
  if (!response.ok) {
154
160
  const status = response.statusText;
@@ -199,17 +205,38 @@ export class MarkdownUp {
199
205
  // Render the text as Markdown
200
206
  const result = {
201
207
  '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
- }),
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
213
240
  !this.menu ? null : {
214
241
  'html': 'div',
215
242
  'attr': {'class': 'menu-burger'},
@@ -221,6 +248,8 @@ export class MarkdownUp {
221
248
  }
222
249
  ]
223
250
  },
251
+
252
+ // Popup menu
224
253
  !this.menu || !('menu' in this.params) ? null : {
225
254
  'html': 'div',
226
255
  'attr': {'class': 'menu'},
@@ -249,14 +278,26 @@ export class MarkdownUp {
249
278
  }
250
279
  ]
251
280
  };
281
+
282
+ // Set the page title
252
283
  if (markdownTitle !== null) {
253
284
  result.title = markdownTitle;
254
285
  }
286
+
255
287
  return result;
256
288
  }
257
289
  }
258
290
 
259
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
+
260
301
  // Icon sizes
261
302
  const burgerSize = '24';
262
303
  const iconSize = '36';