"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 = ๐ถ`
`
// - 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: ๐ถ` `,
divider: ๐ถ``,
code: function({text, lang}) { // ?
let e = this()
e.textContent = text
return e
}.bind(๐ถ`
`),
italic: ๐ถ``,
bold: ๐ถ``,
strikethrough: ๐ถ``,
underline: ๐ถ``,
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(๐ถ``),
// what if instead of the \a tag, we just supported
// an [id=...] attribute on every tag? just need to set id, so...
// well except is safer than id...
anchor: function({id}) {
let e = this()
if (id)
e.name = id
return e
}.bind(๐ถ``),
quote: function({cite}) {
if (cite==null)
return this[0]()
let e = this[1]()
e.firstChild.textContent = cite
return e.lastChild
}.bind([
๐ถ`
`,
๐ถ`
:
`, // should we have -outer class?
]),
table: function() {
let e = this()
return e.firstChild.firstChild
}.bind(๐ถ`
`),
table_row: ๐ถ`
`,
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([๐ถ`