-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero-md.js
380 lines (351 loc) · 10.6 KB
/
zero-md.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
export class ZeroMd extends HTMLElement {
get src() {
return this.getAttribute("src");
}
set src(val) {
this.reflect("src", val);
}
get manualRender() {
return this.hasAttribute("manual-render");
}
set manualRender(val) {
this.reflect("manual-render", val);
}
reflect(name, val) {
if (val === false) {
this.removeAttribute(name);
} else {
this.setAttribute(name, val === true ? "" : val);
}
}
static get observedAttributes() {
return ["src"];
}
attributeChangedCallback(name, old, val) {
if (name === "src" && this.connected && !this.manualRender && val !== old) {
this.render();
}
}
constructor(defaults) {
super();
this.version = "$VERSION";
this.config = {
markedUrl: "https://cdn.jsdelivr.net/gh/markedjs/marked@4/marked.min.js",
prismUrl: [
[
"https://cdn.jsdelivr.net/gh/PrismJS/prism@1/prism.min.js",
"data-manual",
],
"https://cdn.jsdelivr.net/gh/PrismJS/prism@1/plugins/autoloader/prism-autoloader.min.js",
],
cssUrls: [
"https://cdn.jsdelivr.net/gh/sindresorhus/github-markdown-css@4/github-markdown.min.css",
"https://cdn.jsdelivr.net/gh/PrismJS/prism@1/themes/prism.min.css",
],
hostCss:
":host{display:block;position:relative;contain:content;}:host([hidden]){display:none;}",
...defaults,
...window.ZeroMdConfig,
};
this.root = this.hasAttribute("no-shadow")
? this
: this.attachShadow({ mode: "open" });
this.root.prepend(
...this.makeNodes(
`<div class="markdown-styles"></div><div class="markdown-body"></div>`
)
);
if (!this.constructor.ready) {
this.constructor.ready = Promise.all([
!!window.marked || this.loadScript(this.config.markedUrl),
!!window.Prism || this.loadScript(this.config.prismUrl),
]);
}
this.clicked = this.clicked.bind(this);
if (!this.manualRender) {
// Scroll to hash id after first render. However, `history.scrollRestoration` inteferes with this
// on refresh. It's much better to use a `setTimeout` rather than to alter the browser's behaviour.
this.render().then(() => setTimeout(() => this.goto(location.hash), 250));
}
this.observer = new MutationObserver(() => {
this.observeChanges();
if (!this.manualRender) this.render();
});
this.observer.observe(this, { childList: true });
this.observeChanges();
}
/**
* Start observing changes, if not already so, in `template` and `script`.
*/
observeChanges() {
this.querySelectorAll('template,script[type="text/markdown"]').forEach(
(n) => {
this.observer.observe(n.content || n, {
childList: true,
subtree: true,
attributes: true,
characterData: true,
});
}
);
}
connectedCallback() {
this.connected = true;
this.fire("zero-md-connected", {}, { bubbles: false, composed: false });
this.waitForReady().then(() => {
this.fire("zero-md-ready");
});
if (this.shadowRoot) {
this.shadowRoot.addEventListener("click", this.clicked);
}
}
disconnectedCallback() {
this.connected = false;
if (this.shadowRoot) {
this.shadowRoot.removeEventListener("click", this.clicked);
}
}
waitForReady() {
const ready =
this.connected ||
new Promise((resolve) => {
this.addEventListener("zero-md-connected", function handler() {
this.removeEventListener("zero-md-connected", handler);
resolve();
});
});
return Promise.all([this.constructor.ready, ready]);
}
fire(name, detail = {}, opts = { bubbles: true, composed: true }) {
if (detail.msg) {
console.warn(detail.msg);
}
this.dispatchEvent(
new CustomEvent(name, {
detail: { node: this, ...detail },
...opts,
})
);
}
tick() {
return new Promise((resolve) => requestAnimationFrame(resolve));
}
// Coerce anything into an array
arrify(any) {
return any ? (Array.isArray(any) ? any : [any]) : [];
}
// Promisify an element's onload callback
onload(node) {
return new Promise((resolve, reject) => {
node.onload = resolve;
node.onerror = (err) =>
reject(err.path ? err.path[0] : err.composedPath()[0]);
});
}
// Load a url or load (in order) an array of urls via <script> tags
loadScript(urls) {
return Promise.all(
this.arrify(urls).map((item) => {
const [url, ...attrs] = this.arrify(item);
const el = document.createElement("script");
el.src = url;
el.async = false;
attrs.forEach((attr) => el.setAttribute(attr, ""));
return this.onload(document.head.appendChild(el));
})
);
}
// Scroll to selected element
goto(sel) {
let el;
try {
el = this.root.querySelector(sel);
} catch {}
if (el) el.scrollIntoView();
}
// Hijack same-doc anchor hash links
clicked(ev) {
if (
ev.metaKey ||
ev.ctrlKey ||
ev.altKey ||
ev.shiftKey ||
ev.defaultPrevented
) {
return;
}
const a = ev.target.closest("a");
if (
a &&
a.hash &&
a.host === location.host &&
a.pathname === location.pathname
) {
this.goto(a.hash);
}
}
dedent(str) {
str = str.replace(/^\n/, "");
const match = str.match(/^\s+/);
return match ? str.replace(new RegExp(`^${match[0]}`, "gm"), "") : str;
}
getBaseUrl(src) {
const a = document.createElement("a");
a.href = src;
return a.href.substring(0, a.href.lastIndexOf("/") + 1);
}
// Runs Prism highlight async; falls back to sync if Web Workers throw
highlight(container) {
return new Promise((resolve) => {
const unhinted = container.querySelectorAll(
'pre>code:not([class*="language-"])'
);
unhinted.forEach((n) => {
// Dead simple language detection :)
const lang = n.innerText.match(/^\s*</)
? "markup"
: n.innerText.match(/^\s*(\$|#)/)
? "bash"
: "js";
n.classList.add(`language-${lang}`);
});
try {
window.Prism.highlightAllUnder(container, true, resolve());
} catch {
window.Prism.highlightAllUnder(container);
resolve();
}
});
}
/**
* Converts HTML string into HTMLCollection of nodes
* @param {string} html
* @returns {HTMLCollection}
*/
makeNodes(html) {
const tpl = document.createElement("template");
tpl.innerHTML = html;
return tpl.content.children;
}
/**
* Constructs the styles dom and returns HTML string
* @returns {string} `markdown-styles` string
*/
buildStyles() {
const get = (query) => {
const node = this.querySelector(query);
return node ? node.innerHTML || " " : "";
};
const urls = this.arrify(this.config.cssUrls);
const html = `<style>${this.config.hostCss}</style>${get(
'template[data-merge="prepend"]'
)}${
get("template:not([data-merge])") ||
urls.reduce((a, c) => `${a}<link rel="stylesheet" href="${c}">`, "")
}${get('template[data-merge="append"]')}`;
return html;
}
/**
* Constructs the markdown body nodes and returns HTML string
* @param {*} opts Markedjs options
* @returns {Promise<string>} `markdown-body` string
*/
async buildMd(opts = {}) {
const src = async () => {
if (!this.src) return "";
const resp = await fetch(this.src);
if (resp.ok) {
const md = await resp.text();
return window.marked.parse(md, {
baseUrl: this.getBaseUrl(this.src),
...opts,
});
} else {
this.fire("zero-md-error", {
msg: `[zero-md] HTTP error ${resp.status} while fetching src`,
status: resp.status,
src: this.src,
});
return "";
}
};
const script = () => {
const el = this.querySelector('script[type="text/markdown"]');
if (!el) return "";
const md = el.hasAttribute("data-dedent")
? this.dedent(el.text)
: el.text;
return window.marked.parse(md, opts);
};
return (await src()) || script();
}
/**
* Returns 32-bit DJB2a hash in base36
* @param {string} str
* @returns {string}
*/
getHash(str) {
let hash = 5381;
for (let index = 0; index < str.length; index++) {
hash = ((hash << 5) + hash) ^ str.charCodeAt(index);
}
return (hash >>> 0).toString(36);
}
/**
* Insert or replace styles node in root from a HTML string. If there are external stylesheet
* links, wait for them to load.
* @param {string} html styles string
* @returns {Promise<boolean|undefined>} returns true if stamped
*/
async stampStyles(html) {
const hash = this.getHash(html);
const target = this.root.querySelector(".markdown-styles");
if (target.getAttribute("data-hash") !== hash) {
target.setAttribute("data-hash", hash);
const nodes = this.makeNodes(html);
const links = [...nodes].filter(
(i) => i.tagName === "LINK" && i.getAttribute("rel") === "stylesheet"
);
target.innerHTML = "";
target.append(...nodes);
await Promise.all(links.map((l) => this.onload(l))).catch((err) => {
this.fire("zero-md-error", {
msg: "[zero-md] An external stylesheet failed to load",
status: undefined,
src: err.href,
});
});
return true;
}
}
/**
* Insert or replace HTML body string into DOM
* @param {string} html markdown-body string
* @param {string[]} [classes] list of classes to apply to `.markdown-body` wrapper
* @returns {Promise<boolean|undefined>} returns true if stamped
*/
async stampBody(html, classes) {
const names = this.arrify(classes);
const hash = this.getHash(html + JSON.stringify(names));
const target = this.root.querySelector(".markdown-body");
if (target.getAttribute("data-hash") !== hash) {
target.setAttribute("data-hash", hash);
names.unshift("markdown-body");
target.setAttribute("class", names.join(" "));
const nodes = this.makeNodes(html);
target.innerHTML = "";
target.append(...nodes);
await this.highlight(target);
return true;
}
}
async render(opts = {}) {
await this.waitForReady();
const pending = this.buildMd(opts);
const styles = await this.stampStyles(this.buildStyles());
await this.tick();
const body = await this.stampBody(await pending, opts.classes);
this.fire("zero-md-rendered", { node: this, stamped: { styles, body } });
}
}
customElements.define("zero-md", ZeroMd);