-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathzero-md-base.js
More file actions
270 lines (250 loc) · 7.36 KB
/
zero-md-base.js
File metadata and controls
270 lines (250 loc) · 7.36 KB
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
/**
* @typedef {object} ZeroMdRenderObject
* @property {'styles'|'body'} [target]
* @property {string} [text]
* @property {string} [hash]
* @property {boolean} [changed]
* @property {string} [baseUrl]
* @property {boolean} [stamped]
*/
export default class ZeroMdBase extends HTMLElement {
get src() {
return this.getAttribute('src')
}
set src(val) {
val ? this.setAttribute('src', val) : this.removeAttribute('src')
}
get auto() {
return !this.hasAttribute('no-auto')
}
get bodyClass() {
const classes = this.getAttribute('body-class')
return `markdown-body${classes ? ' ' + classes : ''}`
}
constructor() {
super()
try {
this.version = __VERSION__
} catch {} // eslint-disable-line no-empty
this.template = ''
const handler = (/** @type {*} */ e) => {
if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey || e.defaultPrevented) return
const a = e.target?.closest('a')
if (a && a.hash && a.host === location.host && a.pathname === location.pathname)
this.goto(a.hash)
}
this._clicked = handler.bind(this)
this._observer = new MutationObserver(() => {
this._observe()
if (this.auto) this.render()
})
this._loaded = false
/** @type {HTMLElement|ShadowRoot} */
this.root = this
}
static get observedAttributes() {
return ['src', 'body-class']
}
/**
* @param {string} name
* @param {string} old
* @param {string} val
*/
attributeChangedCallback(name, old, val) {
if (this.ready && old !== val) {
switch (name) {
case 'body-class':
this.root.querySelector('.markdown-body')?.setAttribute('class', this.bodyClass)
break
case 'src':
if (this.auto) this.render()
}
}
}
async connectedCallback() {
if (!this._loaded) {
await this.load()
if (!this.hasAttribute('no-shadow')) this.root = this.shadowRoot ?? this.attachShadow({ mode: 'open' })
this.root.prepend(
this.frag(`<div class="markdown-styles"></div><div class="${this.bodyClass}"></div>`)
)
this._loaded = true
}
this.shadowRoot?.addEventListener('click', this._clicked)
this._observer.observe(this, { childList: true })
this._observe()
this.ready = true
this.fire('zero-md-ready')
if (this.auto) this.render()
}
disconnectedCallback() {
this.shadowRoot?.removeEventListener('click', this._clicked)
this._observer.disconnect()
this.ready = false
}
_observe() {
this.querySelectorAll('template,script[type="text/markdown"]').forEach(
(/** @type {*} */ node) =>
this._observer.observe(node.content || node, {
childList: true,
subtree: true,
attributes: true,
characterData: true
})
)
}
/**
* Async load function that runs after constructor. Like constructor, only runs once.
* @returns {Promise<*>}
*/
async load() {}
/**
* Async parse function that takes in markdown and returns the html-formatted string.
* Can use any md parser you prefer, like marked.js
* @param {ZeroMdRenderObject} obj
* @returns {Promise<string>}
*/
async parse({ text = '' }) {
return text
}
/**
* Scroll to heading id
* @param {string} id
*/
goto(id) {
const ctx = this.shadowRoot || document
id && ctx.getElementById(decodeURIComponent(id[0] === '#' ? id.slice(1) : id))?.scrollIntoView()
}
/**
* Convert html string to document fragment
* @param {string} html
* @returns {DocumentFragment}
*/
frag(html) {
const tpl = document.createElement('template')
tpl.innerHTML = html
return tpl.content
}
/**
* Compute 32-bit DJB2a hash in base36
* @param {string} str
* @returns {string}
*/
hash(str) {
let hash = 5381
for (let index = 0; index < str.length; index++) {
hash = ((hash << 5) + hash) ^ str.charCodeAt(index)
}
return (hash >>> 0).toString(36)
}
/**
* Await the next tick
* @returns {Promise<*>}
*/
tick() {
return new Promise((resolve) => requestAnimationFrame(resolve))
}
/**
* Fire custom event
* @param {string} name
* @param {*} [detail]
*/
fire(name, detail = {}) {
this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true }))
}
/**
* Retrieve raw style templates and markdown strings
* @param {ZeroMdRenderObject} obj
* @returns {Promise<ZeroMdRenderObject>}
*/
async read(obj) {
const { target } = obj
const results = (text = '', baseUrl = '') => {
const hash = this.hash(text)
const changed =
this.root.querySelector(`.markdown-${target}`)?.getAttribute('data-hash') !== hash
return { ...obj, text, hash, changed, baseUrl }
}
switch (target) {
case 'styles': {
const get = (query = '') => this.querySelector(query)?.innerHTML
return results(
(get('template[data-prepend]') ?? '') +
(get('template:not([data-prepend],[data-append])') ?? this.template) +
(get('template[data-append]') ?? '')
)
}
case 'body': {
if (this.src) {
const response = await fetch(this.src)
if (response.ok) {
const getBaseUrl = () => {
const a = document.createElement('a')
a.href = this.src || ''
return a.href.substring(0, a.href.lastIndexOf('/') + 1)
}
return results(await response.text(), getBaseUrl())
} else {
console.warn('[zero-md] error reading src', this.src)
}
}
/** @type {HTMLScriptElement|null} */
const script = this.querySelector('script[type="text/markdown"]')
return results(script?.text || '')
}
default:
return results()
}
}
/**
* Stamp parsed html strings into dom
* @param {ZeroMdRenderObject} obj
* @returns {Promise<ZeroMdRenderObject>}
*/
async stamp(obj) {
const { target, text = '', hash = '' } = obj
const node = this.root.querySelector(`.markdown-${target}`)
if (!node) return obj
node.setAttribute('data-hash', hash)
const frag = this.frag(text)
/** @type {HTMLLinkElement[]} */
const links = Array.from(frag.querySelectorAll('link[rel="stylesheet"]') || [])
const whenLoaded = Promise.all(
links.map(
(link) =>
new Promise((resolve) => {
link.onload = resolve
link.onerror = (err) => {
console.warn('[zero-md] error loading stylesheet', link.href)
resolve(err)
}
})
)
)
node.innerHTML = ''
node.append(frag)
await whenLoaded
return { ...obj, stamped: true }
}
/**
* Start rendering
* @param {{ fire?: boolean, goto?: string|false }} obj
* @returns {Promise<*>}
*/
async render({ fire = true, goto = location.hash } = {}) {
const styles = await this.read({ target: 'styles' })
const pending = styles.changed && this.stamp(styles)
const md = await this.read({ target: 'body' })
if (md.changed) {
const parsed = this.parse(md)
await pending
await this.tick()
await this.stamp({ ...md, text: await parsed })
} else await pending
await this.tick()
const detail = { styles: styles.changed, body: md.changed }
if (fire) this.fire('zero-md-rendered', detail)
if (this.auto && goto) this.goto(goto)
return detail
}
}