Only highlight when nesscary

This commit is contained in:
bingus_violet 2024-06-19 19:40:05 -05:00
parent 8c2883ba5c
commit 983d221664

View file

@ -68,7 +68,7 @@ function pathReplacer(html) {
return html return html
} }
function highlighter(json, full=true) { function highlighter(json, full = true) {
for (var i = 0; i < json.length; i++) { for (var i = 0; i < json.length; i++) {
var element = json[i] var element = json[i]
if (element.type == "element") { if (element.type == "element") {
@ -81,12 +81,12 @@ function highlighter(json, full=true) {
for (let index = 0; index < highTable.length; index++) { for (let index = 0; index < highTable.length; index++) {
var term = highTable[index]; var term = highTable[index];
var termProps = highlightedWords[term] var termProps = highlightedWords[term]
var reg = term var reg = term
if (termProps.caseInsensitive) { if (termProps.caseInsensitive) {
reg = new RegExp(`(${term})`, "gi") reg = new RegExp(`(${term})`, "gi")
} }
element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE") element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
element.content = element.content.replaceAll(reg, "{TERM" + index + "}") element.content = element.content.replaceAll(reg, "{TERM" + index + "}")
element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`) element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
@ -98,46 +98,46 @@ function highlighter(json, full=true) {
var termProps = highlightedWords[highTable[index]] var termProps = highlightedWords[highTable[index]]
while (element.content.includes(termKey)) { while (element.content.includes(termKey)) {
var termIndex = element.content.indexOf(termKey) var termIndex = element.content.indexOf(termKey)
var spanEnd = element.content.indexOf(" ", termIndex) var spanEnd = element.content.indexOf(" ", termIndex)
if (spanEnd == -1) { if (spanEnd == -1) {
spanEnd = element.content.length spanEnd = element.content.length
} }
var endContent = element.content.substring(termIndex + termKey.length, spanEnd) var endContent = element.content.substring(termIndex + termKey.length, spanEnd)
var spanStart = element.content.substring(0, termIndex).lastIndexOf(" ") + 1 var spanStart = element.content.substring(0, termIndex).lastIndexOf(" ") + 1
var startContent = element.content.substring(spanStart - 1, termIndex) var startContent = element.content.substring(spanStart - 1, termIndex)
var style = termProps.style || "" var style = termProps.style || ""
var classes = termProps.classes || "" var classes = termProps.classes || ""
if (termProps.color) { if (termProps.color) {
style += `color: ${termProps.color};` style += `color: ${termProps.color};`
} }
if (termProps.italicized) { if (termProps.italicized) {
style += "font-style: italic;" style += "font-style: italic;"
} }
if (termProps.bold) { if (termProps.bold) {
classes += "bold" classes += "bold"
} }
if (style.length > 2) { if (style.length > 2) {
style = `style="${style}"` style = `style="${style}"`
} }
if (classes.length > 2) { if (classes.length > 2) {
classes = `class="${classes}"` classes = `class="${classes}"`
} }
var replacement = `<span ${style} ${classes}>${startContent + highTable[index] + endContent}</span>` var replacement = `<span ${style} ${classes}>${startContent + highTable[index] + endContent}</span>`
element.content = element.content.substring(0, spanStart) + replacement + element.content.substring(spanEnd) element.content = element.content.substring(0, spanStart) + replacement + element.content.substring(spanEnd)
} }
} }
// element.content = element.content.replaceAll(termKey, replacement) // element.content = element.content.replaceAll(termKey, replacement)
} }
@ -147,7 +147,7 @@ function highlighter(json, full=true) {
return json return json
} }
function converter(html, dynamic=true) { function converter(html, dynamic = true) {
var startTime = Date.now() var startTime = Date.now()
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json'))) var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json')))
@ -178,7 +178,7 @@ function converter(html, dynamic=true) {
if (status.emoji) { if (status.emoji) {
if (status.emoji.id) { if (status.emoji.id) {
addedHTML += `<img src="/emojis/${status.emoji.id}" title="${status.emoji.name}" class="emoji"/>` addedHTML += `<img src="/emojis/${status.emoji.id}" title="${status.emoji.name}" class="emoji"/>`
} else { } else {
addedHTML += status.emoji.name + " " addedHTML += status.emoji.name + " "
} }
} }
@ -244,30 +244,35 @@ function converter(html, dynamic=true) {
for (let index = 0; index < rpTable.length; index++) { for (let index = 0; index < rpTable.length; index++) {
const text = rpTable[index]; const text = rpTable[index];
if (dynamic) {
replacers[text] = himalaya.stringify(highlighter(himalaya.parse(replacers[text])))
}
html = html.replaceAll(`{${text}}`, replacers[text]) html = html.replaceAll(`{${text}}`, replacers[text])
} }
if (html.includes("<body>")) { if (!dynamic) {
var bodyHTML = htmlMinifier.minify(html.substring(html.indexOf("<body>") + 6, html.lastIndexOf("</body>"))) if (html.includes("<body>")) {
var parsedHTML = himalaya.parse(bodyHTML) var bodyHTML = htmlMinifier.minify(html.substring(html.indexOf("<body>") + 6, html.lastIndexOf("</body>")))
} else { var parsedHTML = himalaya.parse(bodyHTML)
var parsedHTML = himalaya.parse(html) } else {
var parsedHTML = himalaya.parse(html)
}
parsedHTML = highlighter(parsedHTML)
parsedHTML = himalaya.stringify(parsedHTML)
if (html.includes("<body>")) {
parsedHTML = "<body>" + parsedHTML + "</body>"
html = html.substring(0, html.indexOf("<body>")) + parsedHTML + html.substring(html.indexOf("</body>") + 7)
} else {
html = parsedHTML
}
} }
// console.log(parsedHTML) if (dynamic) {
html = html.replaceAll("{LOAD_TIME}", (Date.now() - startTime).toString() + "ms")
parsedHTML = highlighter(parsedHTML, dynamic)
parsedHTML = himalaya.stringify(parsedHTML)
if (html.includes("<body>")) {
parsedHTML = "<body>" + parsedHTML + "</body>"
html = html.substring(0, html.indexOf("<body>")) + parsedHTML + html.substring(html.indexOf("</body>") + 7)
} else {
html = parsedHTML
} }
html = html.replaceAll("{LOAD_TIME}", (Date.now() - startTime).toString() + "ms")
return html return html
} }
@ -397,7 +402,7 @@ function socketeer() {
for (var i = 0; i < lanyardData.activities.length; i++) { for (var i = 0; i < lanyardData.activities.length; i++) {
var activity = lanyardData.activities[i] var activity = lanyardData.activities[i]
if (activity.type == 4 && activity.emoji) { if (activity.type == 4 && activity.emoji) {
if (activity.emoji.id) { if (activity.emoji.id) {
if (activity.emoji.animated) { if (activity.emoji.animated) {
var emoji = Buffer.from(await (await fetch(`https://cdn.discordapp.com/emojis/${activity.emoji.id}.gif?quality=lossless`)).arrayBuffer()) var emoji = Buffer.from(await (await fetch(`https://cdn.discordapp.com/emojis/${activity.emoji.id}.gif?quality=lossless`)).arrayBuffer())