-
Notifications
You must be signed in to change notification settings - Fork 22
/
markdown-content.js
425 lines (383 loc) 路 13.8 KB
/
markdown-content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* DataFormsJS <markdown-content> Web Component
*
* This Web Component can be used to render Markdown to HTML using one of 3 widely used
* Markdown Libraries [marked, markdown-it, and remarkable].
*
* This Web Component does not include a Markdown Library rather one needs to be included
* on the page before this Component is used. Additionally if [highlight.js] is included
* on the page it will be used for Syntax Highlighting and if [DOMPurify] is included
* the rendered HTML will be sanitized for security.
*
* Options are preset, however this Web Component is small in size using simple logic
* so and designed to be easy to modify if you have a site with different markdown needs.
*
* Example usage:
* *) Load Markdown from URL
* <markdown-content url="{url}"></markdown-content>
* *) Show loading screen while source from URL is loading
* <markdown-content loading-selector="#loading-screen" url="{url}"></markdown-content>
* *) Inline Markdown
* <markdown-content><script type="text/markdown"># Hello World</script></markdown-content>
* *) Empty element with source set form JavaScript
* <markdown-content></markdown-content>
* document.querySelector('markdown-content').value = '# Hello World'
* *) Additional Attributes:
* link-target="_blank"
* link-rel="noopener"
* link-root-url="https..."
* load-only-once
* use-root-url="false"
* show-source
* *) When the attribute [show-source] is included the raw text is displayed in a <pre>
* element so when using this component can be used to show text or other files
* without having to include an additional markdown library.
*
* Example:
* @link https://github.com/dataformsjs/dataformsjs/blob/master/examples/markdown-web.htm
*
* Libraries used by this Component:
* @link https://github.com/markedjs/marked
* @link https://github.com/markdown-it/markdown-it
* @link https://github.com/markdown-it/markdown-it-emoji
* @link https://github.com/jonschlinkert/remarkable
* @link https://github.com/highlightjs/highlight.js
* @link https://github.com/cure53/DOMPurify
*/
/* Validates with both [eslint] and [jshint] */
/* global hljs, marked, markdownit, markdownitEmoji, remarkable, DOMPurify */
/* eslint-env browser, es6 */
/* eslint quotes: ["error", "single", { "avoidEscape": true }] */
/* eslint spaced-comment: ["error", "always"] */
/* eslint-disable no-console */
/* jshint esversion:8 */
import { showError } from './utils.js';
/**
* Markdown Caching for when [load-only-once] is used.
*
* Content is cached in memory only while the user has the page open.
* To prevent the cache from taking up to much memory in the event of
* a bot clicking on every page, topic, etc in a site the cache will
* be reset if it reaches the max size.
*/
const markdownCache = [];
const maxCacheSize = 100;
function saveMarkdownToCache(url, content, errorMessage) {
if (markdownCache.length > maxCacheSize) {
markdownCache.length = 0; // Clear array
}
for (let n = 0, m = markdownCache.length; n < m; n++) {
if (url === markdownCache[n].url) {
return; // Already saved
}
}
markdownCache.push({ url, content, errorMessage });
}
function getMarkdownFromCache(url) {
for (let n = 0, m = markdownCache.length; n < m; n++) {
if (url === markdownCache[n].url) {
return markdownCache[n];
}
}
return null;
}
/**
* Shadow DOM for Custom Elements
*/
const shadowTmpl = document.createElement('template');
shadowTmpl.innerHTML = `
<style>
:host { display: block; }
:host([hidden]) { display: none; }
</style>
<slot></slot>
`;
/**
* Optional Highlight Code using [highlight.js].
* When using [marked] the code must be returned if it is not
* highlighted and when using [markdown-it] or [Remarkable]
* empty text must be returned. To handle `returnCode` is set
* prior to this function being called.
*/
let returnCode = false;
function highlight(code, lang) {
if (window.hljs === undefined) {
return (returnCode ? code : '');
}
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, code).value;
} catch (err) {
console.warn(err);
}
}
try {
return hljs.highlightAuto(code).value;
} catch (err) {
console.warn(err);
}
return (returnCode ? code : '');
}
/**
* Define class for the <markdown-content> element
*/
class MarkdownContent extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(shadowTmpl.content.cloneNode(true));
this.content = null;
this.errorMessage = null;
this.isFetching = false;
}
connectedCallback() {
const sourceEl = this.querySelector('script[type="text/markdown"]');
if (sourceEl) {
this.content = sourceEl.innerHTML;
}
this.render();
}
static get observedAttributes() {
return ['url', 'show-source'];
}
attributeChangedCallback(attrName, oldValue, newValue) {
switch (attrName) {
case 'url':
if (oldValue !== newValue) {
this.fetch();
}
break;
case 'show-source':
if (oldValue !== newValue) {
this.render();
}
break;
}
}
clearContent() {
this.content = null;
this.render();
}
get url() {
return this.getAttribute('url');
}
set url(newValue) {
this.setAttribute('url', newValue);
}
get showSource() {
return this.hasAttribute('show-source');
}
set showSource(newValue) {
if (newValue) {
this.setAttribute('show-source', '');
} else {
this.removeAttribute('show-source');
}
}
get loadOnlyOnce() {
return this.hasAttribute('load-only-once');
}
set loadOnlyOnce(newValue) {
if (newValue) {
this.setAttribute('load-only-once', '');
} else {
this.removeAttribute('load-only-once');
}
}
get value() {
return this.content;
}
set value(markdown) {
this.content = markdown;
this.render();
}
fetch() {
const url = this.url;
if (!url) {
showError(this, 'Error - Unable to show Markdown content because [url] is not set.');
this.dispatchRendered();
return;
}
// Option to load markdown from cache rather than fetching each time.
// Good for use with SPA's where content does not change often and the user
// might view same page several times.
if (this.loadOnlyOnce) {
const cache = getMarkdownFromCache(url);
if (cache) {
this.content = cache.content;
this.errorMessage = cache.errorMessage;
this.render();
return;
}
}
// Show loading screen
const loadingSelector = this.getAttribute('loading-selector');
if (loadingSelector) {
const loading = document.querySelector(loadingSelector);
if (loading) {
this.innerHTML = '';
this.appendChild(loading.content.cloneNode(true));
} else {
console.warn(`Could not find template from <${this.tagName.toLowerCase()} [loading-selector="${loadingSelector}"]>.`);
}
}
// Fetch content
this.isFetching = true;
fetch(url)
.then(res => {
const status = res.status;
if ((status >= 200 && status < 300) || status === 304) {
return Promise.resolve(res);
} else {
const error = `Error loading markdown content from [${url}]. Server Response Code: ${status}, Response Text: ${res.statusText}`;
return Promise.reject(error);
}
})
.then(res => { return res.text(); })
.then(text => {
this.content = text;
this.errorMessage = null;
this.isFetching = false;
this.render();
})
.catch(error => {
this.errorMessage = error;
this.isFetching = false;
this.render();
})
.finally(() => {
if (this.loadOnlyOnce) {
saveMarkdownToCache(url, this.content, this.errorMessage);
}
});
}
dispatchRendered() {
this.dispatchEvent(new Event('app:markdownRendered', { bubbles: true }));
}
render() {
// Still loading, this can happen while `fetch` is running
// and [show-source] is being set by the DOM and [loading-selector]
// is set to show an exiting loading screen.
if (this.isFetching) {
return;
}
// Error message (for example a failed fetch)
if (this.errorMessage) {
showError(this, this.errorMessage);
this.dispatchRendered();
return;
}
// Nothing to show
if (this.content === null) {
this.innerHTML = '';
return;
}
// Shows as text rather than rendering source
if (this.showSource) {
this.innerHTML = '<pre></pre>';
this.querySelector('pre').textContent = this.content;
this.dispatchRendered();
return;
}
// Render Markdown to HTML using one of 3 libraries
let html;
let md;
returnCode = false;
if (window.marked) {
returnCode = true;
marked.setOptions({
highlight: highlight
});
if (marked.marked && marked.marked.parse) {
html = marked.marked.parse(this.content); // 4.#
} else {
html = marked(this.content); // 3.#
}
} else if (window.markdownit) {
md = markdownit({
html: true,
linkify: true,
typographer: true,
highlight: highlight
});
if (window.markdownitEmoji) {
md.use(markdownitEmoji);
}
html = md.render(this.content);
} else if (window.remarkable) {
md = new remarkable.Remarkable({
html: true,
typographer: true,
highlight: highlight
}).use(remarkable.linkify);
html = (md).render(this.content);
} else {
showError(this, 'Error - Unable to show Markdown content because a Markdown JavaScript library was not found on the page.');
this.dispatchRendered();
return;
}
// Clean/Sanitize the HTML for Security if DOMPurify is loaded
if (window.DOMPurify !== undefined) {
html = DOMPurify.sanitize(html);
}
// Set Content
this.innerHTML = html;
// Update code blocks so they highlight with the
// correct theme if using [highlight.js].
if (window.hljs !== undefined) {
const codeBlocks = this.querySelectorAll('code[class*="language-"]');
for (let code of codeBlocks) {
code.classList.add('hljs');
}
}
// Update all [a.target] and [a.rel] attributes if specified.
// Example: [link-target="_blank"] and [link-rel="noopener"]
const linkTarget = this.getAttribute('link-target');
const linkRel = this.getAttribute('link-rel');
if (linkTarget || linkRel) {
const links = this.querySelectorAll('a');
for (const link of links) {
link.target = (linkTarget ? linkTarget : link.target);
link.rel = (linkRel ? linkRel : link.rel);
}
}
// Handle relative links and images by default
const useRootUrl = this.getAttribute('use-root-url');
if (useRootUrl !== 'false') {
// Get the root URL of the document if using URL
const url = this.url;
let rootUrl;
if (url) {
const parts = url.split('/');
rootUrl = url.substring(0, url.length - parts[parts.length - 1].length);
}
// Update all local links if [link-root-url] is specified or if [url] is used.
// For example Github readme docs would often point to links in the local repository.
// This feature can be used to specify the root URL so that all links work correctly,
// and link to the main source display page and not raw content.
let linkRootUrl = this.getAttribute('link-root-url');
linkRootUrl = (linkRootUrl ? linkRootUrl : rootUrl);
if (linkRootUrl) {
const links = this.querySelectorAll('a:not([href^="http:"]):not([href^="https:"])');
for (const link of links) {
const href = link.getAttribute('href');
link.setAttribute('data-original-href', href);
link.setAttribute('href', linkRootUrl + href);
}
}
// Update Images that use a relative URL based on the document
const images = this.querySelectorAll('img:not([src^="http:"]):not([src^="https:"])');
for (const img of images) {
const src = img.getAttribute('src');
img.setAttribute('data-original-src', src);
img.setAttribute('src', rootUrl + src);
}
}
// Run event to let app know content has been rendered
this.dispatchRendered();
}
}
/**
* Add <markdown-content> element to the page
*/
window.customElements.define('markdown-content', MarkdownContent);