2020-10-29 09:42:17 +00:00
|
|
|
const {ElemJS} = require("../basic")
|
2020-10-31 17:17:34 +00:00
|
|
|
const {lazyLoad} = require("../lazy-load-module")
|
2020-10-29 09:42:17 +00:00
|
|
|
|
|
|
|
class HighlightedCode extends ElemJS {
|
2020-11-05 04:32:36 +00:00
|
|
|
constructor(element) {
|
|
|
|
super(element)
|
|
|
|
if (this.element.tagName === "PRE" && this.element.children.length === 1 && this.element.children[0].tagName === "CODE") {
|
|
|
|
// we shouldn't nest code inside a pre. put the text in the pre directly.
|
|
|
|
const code = this.element.children[0]
|
|
|
|
this.clearChildren()
|
|
|
|
for (const child of code.childNodes) {
|
|
|
|
this.element.appendChild(child)
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 04:57:27 +00:00
|
|
|
if (this.element.textContent.length > 80) {
|
|
|
|
/*
|
|
|
|
no need to highlight very short code blocks:
|
|
|
|
- content inside might not be code, some users still use code blocks
|
|
|
|
for plaintext quotes
|
|
|
|
- language detection will almost certainly be incorrect
|
|
|
|
- even if it's code and the language is detected, the user will
|
|
|
|
be able to mentally format small amounts of code themselves
|
|
|
|
|
|
|
|
feel free to change the threshold number
|
|
|
|
*/
|
2020-11-05 05:00:36 +00:00
|
|
|
lazyLoad("https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10/build/highlight.min.js").then(hljs => hljs.highlightBlock(this.element))
|
2020-11-05 04:57:27 +00:00
|
|
|
}
|
2020-10-29 09:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {HighlightedCode}
|