-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrender.js
422 lines (378 loc) ยท 11.6 KB
/
render.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
"use strict"
12||+typeof await/2//2; export default
/**
DOM node renderer (for use in browsers)
factory class
**/
class Markup_Render_Dom { constructor() {
// This tag-function parses an HTML string, and returns a function
// which creates a copy of that HTML DOM tree when called.
// ex: let create = ๐ถ`<div></div>`
// - create() acts like document.createElement('div')
let temp = document.createElement('template')
function ๐ถ([html]) {
temp.innerHTML = html.replace(/\s*?\n\s*/g, "")
return document.importNode.bind(document, temp.content.firstChild, true)
}
// todo: this needs to be more powerful. i.e. returning entire elements in some cases etc. gosh idk.. need to handle like, sbs emotes ? how uh nno that should be the parser's job.. oh and also this should, like,
// for embeds, need separate handlers for normal urls and embeds and
let URL_SCHEME = {
__proto__: null,
"sbs:": (url, thing)=> "#"+url.pathname+url.search+url.hash,
"https:": (url, thing)=> url.href,
"http:": (url, thing)=> url.href,
"data:": (url, thing)=> url.href,
DEFAULT: (url, thing)=> "about:blank#"+url.href,
// these take a url string instead of URL
RELATIVE: (href, thing)=> href.replace(/^[/]{0,2}/, "https://"),
ERROR: (href, thing)=> "about:blank#"+href,
}
function filter_url(url, thing) {
try {
let u = new URL(url, "no-scheme:/")
if ('no-scheme:'==u.protocol)
return URL_SCHEME.RELATIVE(url, thing)
else
return (URL_SCHEME[u.protocol] || URL_SCHEME.DEFAULT)(u, thing)
} catch (e) {
return URL_SCHEME.ERROR(url, thing)
}
}
let preview
let CREATE = {
__proto__: null,
newline: ๐ถ`<br>`,
divider: ๐ถ`<hr>`,
code: function({text, lang}) { // <tt>?
let e = this()
e.textContent = text
return e
}.bind(๐ถ`<pre>`),
// .bind(value) makes that value accessible as `this` inside the function, when it's called. (note that the value is only evaluated once)
// I'm just using this as a simple trick to store the html templates with their init functions, but there's no special reason to do it this way
icode: function({text}) {
let e = this()
e.textContent = text.replace(/ /g, "ย ") // non breaking space..
return e
}.bind(๐ถ`<code>`),
simple_link: function({url, text}) {
let e = this()
if (text==null) {
e.textContent = url
} else {
e.textContent = text
e.className += ' M-link-custom'
}
if (!url.startsWith("#")) {
url = filter_url(url, 'link')
e.target = '_blank'
} else
e.target = '_self' //hack
e.href = url
return e
}.bind(๐ถ`<a href="" class='M-link'>`),
image: function({url, alt, width, height}) {
let src = filter_url(url, 'image')
let e = document.createElement('img')
e.classList.add('M-image')
e.dataset.shrink = ""
if (alt!=null)
e.alt = e.title = alt
e.tabIndex = 0
const set_size = (state, width=e.naturalWidth, height=e.naturalHeight)=>{
e.width = width
e.height = height
e.style.setProperty('--width', width)
e.style.setProperty('--height', height)
e.dataset.state = state
}
if (height)
set_size('size', width, height)
e.src = src
// check whether the image is "available" (i.e. size is known) by looking at naturalHeight
// https://html.spec.whatwg.org/multipage/images.html#img-available
// this will happen here if the image is VERY cached, i guess
if (e.naturalHeight)
set_size('loaded')
else // otherwise wait for load
e.decode().then(ok=>{
set_size('loaded')
}, no=>{
e.dataset.state = 'error'
})
return e
},
error: ๐ถ`<div class='error'><code>๐ฏerror๐ฏ</code>๐ฏmessage๐ฏ<pre>๐ฏstack๐ฏ`,
audio: function({url}) {
url = filter_url(url, 'audio')
let e = this()
e.dataset.src = url
e.onclick = ev=>{
ev.preventDefault()
let e = ev.currentTarget
let audio = document.createElement('audio')
audio.controls = true
audio.autoplay = true
audio.src = e.dataset.src
e.replaceChildren(audio)
e.onclick = null
}
let link = e.firstChild
link.href = url
link.title = url
link.lastChild.textContent = url.replace(/.*[/]/, "โฆ/").replace(/[?].*$/, "?โฆ")
return e
}.bind(๐ถ`<y12-audio><a>๐ต๏ธ<span></span></a></y12-audio>`),
video: function({url}) {
let e = this()
let media = document.createElement('video')
media.setAttribute('tabindex', 0)
media.preload = 'none'
media.dataset.shrink = "video"
media.src = filter_url(url, 'video')
e.firstChild.append(media)
let cl = e.lastChild
let [play, progress, time] = cl.childNodes
play.onclick = e=>{
if (media.paused)
media.play()
else
media.pause()
e.stopPropagation()
}
media.onpause = e=>{
play.textContent = "โถ๏ธ"
}
media.onplay = e=>{
play.textContent = "โธ๏ธ"
}
media.onresize = ev=>{
media.onresize = null
media.parentNode.style.aspectRatio = media.videoWidth+"/"+media.videoHeight
media.parentNode.style.height = media.videoHeight+"px"
media.parentNode.style.width = media.videoWidth+"px"
}
media.onerror = ev=>{
time.textContent = 'Error'
}
media.ondurationchange = e=>{
let s = media.duration
progress.disabled = false
progress.max = s
let m = Math.floor(s / 60)
s = s % 60
time.textContent = m+":"+(s+100).toFixed(2).substring(1)
}
media.ontimeupdate = e=>{
progress.value = media.currentTime
}
progress.onchange = e=>{
media.currentTime = progress.value
}
return e
}.bind(๐ถ`
<y12-video>
<figure class='M-image-wrapper'></figure>
<div class='M-media-controls'>
<button>โถ๏ธ</button>
<input type=range min=0 max=1 step=any value=0 disabled>
<span>not loaded</span>
</div>
</y12-video>
`),
italic: ๐ถ`<i>`,
bold: ๐ถ`<b>`,
strikethrough: ๐ถ`<s>`,
underline: ๐ถ`<u>`,
heading: function({level, id}) {
let e = document.createElement("h"+(level- -1))
if (id) {
let e2 = this()
e2.name = id
e2.appendChild(e)
}
return e
}.bind(๐ถ`<a name="" class=M-anchor></a>`),
// what if instead of the \a tag, we just supported
// an [id=...] attribute on every tag? just need to set id, so...
// well except <a name=...> is safer than id...
anchor: function({id}) {
let e = this()
if (id)
e.name = id
return e
}.bind(๐ถ`<a name="" class=M-anchor></a>`),
quote: function({cite}) {
if (cite==null)
return this[0]()
let e = this[1]()
e.firstChild.textContent = cite
return e.lastChild
}.bind([
๐ถ`<blockquote class='M-quote'>`,
๐ถ`<blockquote class='M-quote'><cite class='M-quote-label'></cite>:<div class='M-quote-inner'></div></blockquote>`, // should we have -outer class?
]),
table: function() {
let e = this()
return e.firstChild.firstChild
}.bind(๐ถ`<div class='M-table-outer'><table><tbody>`),
table_row: ๐ถ`<tr>`,
table_cell: function({header, color, truecolor, colspan, rowspan, align, div}, row_args) {
let e = this[header||row_args.header ? 1 : 0]()
if (color) e.dataset.bgcolor = color
if (truecolor) e.style.backgroundColor = truecolor
if (colspan) e.colSpan = colspan
if (rowspan) e.rowSpan = rowspan
if (align) e.style.textAlign = align
// todo: better way of representing this?
if (div)
e.classList.add('M-wall-right')
if (row_args.divider)
e.classList.add('M-wall-top')
return e
}.bind([๐ถ`<td>`, ๐ถ`<th>`]),
youtube: function({url}) {
let e = this()
e.firstChild.textContent = url
e.firstChild.href = url
e.dataset.href = url
return e
}.bind(๐ถ`<youtube-embed><a target=_blank></a></youtube-embed>`),
link: function({url}) {
let e = this()
if (!url.startsWith("#")) {
url = filter_url(url, 'link')
e.target = '_blank'
} else
e.target = '_self'
e.href = url
return e
}.bind(๐ถ`<a class='M-link M-link-custom' href="">`),
list: function({style}) {
if (style==null)
return this[0]()
let e = this[1]()
//e.style.listStyleType = style // this was only supported by old bbcode so i can probably secretly remove it.
return e
}.bind([๐ถ`<ul>`, ๐ถ`<ol>`]),
/* todo: list bullets suck, because you can't select/copy them
we should create our own fake bullet elements instead.*/
list_item: ๐ถ`<li>`,
align: function({align}) {
let e = this()
e.style.textAlign = align
return e
}.bind(๐ถ`<div>`),
subscript: ๐ถ`<sub>`,
superscript: ๐ถ`<sup>`,
small: ๐ถ`<small>`,
small_caps: ๐ถ`<span class='M-small-caps'>`,
overline: ๐ถ`<span class='M-overline'>`,
/*anchor: function({name}) {
let e = this()
e.id = "Markup-anchor-"+name
return e
}.bind(๐ถ`<span id="" class='M-anchor'>`),*/
ruby: function({text}) {
let e = this()
e.lastChild.textContent = text
return e.firstChild
}.bind(๐ถ`<ruby><span></span><rt>`), // I don't think we need <rp> since we're rendering for modern browsers...
spoiler: function({label, cw}) {
let e = this()
if (cw)
e.classList.add('M-content-warning')
e.firstChild.textContent = label//.replace(/_/g, " ")
//todo: [12y1] maybe replace all underscores in args with spaces, during parsing?
return e.lastChild
}.bind(๐ถ`
<details class='M-spoiler'>
<summary class='M-spoiler-label'></summary>
<div class='M-spoiler-inner'></div>
</details>`),
background_color: function({color}) {
let e = this()
if (color)
e.dataset.bgcolor = color
return e
}.bind(๐ถ`<span class='M-background'>`),
language: function({lang}) {
let e = this()
e.lang = lang
return e
}.bind(๐ถ`<span>`),
invalid: function({text, reason}) {
let e = this()
e.title = reason
e.textContent = text
return e
}.bind(๐ถ`<span class='M-invalid'>`),
key: ๐ถ`<kbd>`,
preview: function(node) {
let e = this()
e.textContent = node.type
return e
}.bind(๐ถ`<div class='M-preview'>`),
}
function fill_branch(branch, leaves) {
for (let leaf of leaves) {
if ('string'==typeof leaf) {
branch.append(leaf)
} else {
let node
if (preview && (leaf.type=='audio' || leaf.type=='video' || leaf.type=='youtube')) {
node = CREATE.preview(leaf)
} else {
let creator = CREATE[leaf.type]
if (!creator) {
if ('object'==typeof leaf && leaf)
throw new RangeError("unknown node .type: โ"+leaf.type+"โ")
else
throw new TypeError("unknown node type: "+typeof leaf)
}
node = creator(leaf.args)
}
if (leaf.content) {
if ('table_row'===leaf.type) {
for (let cell of leaf.content) {
if ('table_cell'!==cell.type)
continue
let c = CREATE.table_cell(cell.args, leaf.args||{})
if (cell.content)
fill_branch(c, cell.content)
node.append(c)
}
} else {
fill_branch(node, leaf.content)
}
}
branch.append(node.getRootNode()) // recursion order?
}
}
}
/**
Render function (closure method)
@param {Tree} ast - input ast
@param {ParentNode} [node=document.createDocumentFragment()] - destination node
@param {?object} options - render options
@return {ParentNode} - node with rendered contents. same as `node` if passed, otherwise is a new DocumentFragment.
**/
this.render = function({args, content}, node=document.createDocumentFragment(), options) {
preview = options && options.preview
node.textContent = "" //mmnn
fill_branch(node, content)
return node
}
/**
block rendering functions
@member {Object<string,function>}
**/
this.create = CREATE
/**
URL processing functions
@member {Object<string,function>}
**/
this.url_scheme = URL_SCHEME
this.filter_url = filter_url
}}
if ('object'==typeof module && module) module.exports = Markup_Render_Dom