2022-01-04 12:02:48 +00:00
|
|
|
import {html} from "../skooma.js"
|
|
|
|
import {css, variable} from "../css.js"
|
|
|
|
import {template} from "../template.js"
|
|
|
|
|
2022-06-09 11:11:25 +00:00
|
|
|
document.head.append(html.link({rel: 'stylesheet', href: 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.3.1/build/styles/github.min.css'}))
|
2022-01-04 12:02:48 +00:00
|
|
|
import hljs from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.3.1/build/es/highlight.min.js';
|
|
|
|
import javascript from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.3.1/build/es/languages/javascript.min.js';
|
|
|
|
hljs.registerLanguage('javascript', javascript);
|
|
|
|
|
|
|
|
const escapes = { ">": ">" }
|
|
|
|
class CodeBlock extends HTMLElement {
|
|
|
|
connectedCallback() {
|
2022-02-08 13:50:32 +00:00
|
|
|
let content = this.innerHTML
|
|
|
|
.replace(/^\s*\n/, "")
|
|
|
|
.replace(/\n\s*$/, "")
|
|
|
|
let prefix = new RegExp(`(?<![^\n])${content.match(/^\t*/)}`, "g")
|
|
|
|
content = content
|
|
|
|
.replace(prefix, "")
|
|
|
|
.replace(/&[a-z]+;/g, str => escapes[str] ?? str)
|
2022-01-13 21:02:20 +00:00
|
|
|
this.replaceChildren(html.pre(html.code(template(hljs.highlight(content, {language: 'javascript'}).value))))
|
2022-01-04 12:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("code-block", CodeBlock)
|
|
|
|
|
|
|
|
document.head.append(html.style(css({
|
|
|
|
'code-block': {
|
|
|
|
code: {
|
2022-06-22 12:09:08 +00:00
|
|
|
//backgroundColor: "#00000006",
|
|
|
|
backgroundColor: "#fff4",
|
2022-01-04 12:02:48 +00:00
|
|
|
borderRadius: CSS.em(.6),
|
2022-06-22 12:09:08 +00:00
|
|
|
border: 'none',
|
2022-06-09 11:11:25 +00:00
|
|
|
boxShadow: [
|
2022-06-22 12:09:08 +00:00
|
|
|
[...[.2, .4, .6].map(CSS.em), '#0003'],
|
2022-06-09 11:11:25 +00:00
|
|
|
],
|
2022-01-04 12:02:48 +00:00
|
|
|
display: "block",
|
2022-06-22 12:09:08 +00:00
|
|
|
fontSize: CSS.em(1.2),
|
|
|
|
padding: [[.8, 1.2].map(CSS.em)],
|
|
|
|
tabSize: 3,
|
2022-01-04 12:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})))
|