Highlight rewrite

This commit is contained in:
bingus_violet 2024-09-11 15:08:42 -05:00
parent 6f2b40d75a
commit c6b2f65171

View file

@ -111,117 +111,166 @@ function highlighter(json, full = true, linkParent = false) {
if (element.tagName == "code") { if (element.tagName == "code") {
valid = false valid = false
} }
if (valid) { if (valid) {
element.children = highlighter(element.children, full, linkParent || element.tagName == "a") element.children = highlighter(element.children, full, linkParent || element.tagName == "a")
} }
} }
} else if (element.type == "text") { } else if (element.type == "text") {
var index = 0
for (let i = 0; i < highlightedWords.length; i++) { for (let i = 0; i < highlightedWords.length; i++) {
var dict = highlightedWords[i] var dict = highlightedWords[i]
for (let x = 0; x < dict.words.length; x++) { for (let x = 0; x < dict.words.length; x++) {
index += 1
var term = dict.words[x]; var term = dict.words[x];
var termWordCount = term.split(" ").length
var termProps = dict var termProps = dict
var reg = term var reg = new RegExp(`(${term})`, "g")
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(reg, "{TERM" + index + "}")
element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
}
}
if (full) { var style = termProps.style || ""
var index = 0 var classes = termProps.classes || ""
for (let i = 0; i < highlightedWords.length; i++) { var link = termProps.link || ""
var dict = highlightedWords[i]
for (let x = 0; x < dict.words.length; x++) {
index += 1
var termKey = "{TERM" + index + "}"
var termProps = dict
while (element.content.includes(termKey)) {
var termIndex = element.content.indexOf(termKey)
var spanEnd = element.content.indexOf(" ", termIndex) if (termProps.color) {
style += `color: ${termProps.color};`
}
if (spanEnd == -1) { if (termProps.italicized) {
spanEnd = element.content.length style += "font-style: italic;"
} }
var spanStart = element.content.substring(0, termIndex).lastIndexOf(" ") + 1 if (termProps.outline) {
var width = 2
// style += `text-shadow: -1px -1px 0 ${termProps.outline}, 1px -1px 0 ${termProps.outline}, -1px 1px 0 ${termProps.outline}, 1px 1px 0 ${termProps.outline};`
style += `-webkit-text-stroke: 1px ${termProps.outline};`
// ^ Not in use because it looks bad :3
}
// if (highTable[index] == "ULTRAKILL") { if (termProps.bold) {
// console.log(startContent, " ---- ", endContent) classes += "bold"
// } }
var startContent = element.content.substring(spanStart - 1, termIndex) if (style.length > 2) {
var endContent = element.content.substring(termIndex + termKey.length, spanEnd) style = `style="${style}"`
}
// if (startContent.includes("(") && !endContent.includes(")")) { if (classes.length > 2) {
// spanEnd = element.content.indexOf(")", spanStart) + 1 classes = `class="${classes}"`
// endContent = element.content.substring(termIndex + termKey.length, spanEnd) }
// }
// else if (endContent.includes(")") && !startContent.includes("(")) {
// spanStart = element.content.substring(0, spanStart).lastIndexOf("(")
// startContent = element.content.substring(spanStart - 1, termIndex)
// }
var style = termProps.style || ""
var classes = termProps.classes || ""
var link = termProps.link || ""
if (termProps.color) { var splitContent = element.content.split(" ")
style += `color: ${termProps.color};` for (let y = 0; y < splitContent.length; y++) {
} var word = splitContent[y]
if (termProps.italicized) { if (reg.test(word)) {
style += "font-style: italic;"
}
if (termProps.outline) { if (link && !linkParent) {
var width = 2 splitContent[y] = `<a href="${link}">${splitContent[y]}</a>`
// style += `text-shadow: -1px -1px 0 ${termProps.outline}, 1px -1px 0 ${termProps.outline}, -1px 1px 0 ${termProps.outline}, 1px 1px 0 ${termProps.outline};`
style += `-webkit-text-stroke: 1px ${termProps.outline};`
// ^ Not in use because it looks bad :30
}
if (termProps.bold) {
classes += "bold"
}
if (style.length > 2) {
style = `style="${style}"`
}
if (classes.length > 2) {
classes = `class="${classes}"`
}
var stuff = (startContent + dict.words[x] + endContent).trim()
if (!stuff.includes("span")) {
var replacement = `<span ${style} ${classes}>${stuff}</span>`
if (link && !linkParent) {
replacement = `<a href="${link}">${replacement}</a>`
// console.log(replacement)
}
element.content = element.content.substring(0, spanStart) + replacement + element.content.substring(spanEnd)
} else { } else {
splitContent[y] = `<span ${style} ${classes}>${splitContent[y]}</span>`
element.content = element.content.replace(termKey, dict.words[x])
} }
} }
} }
}
// element.content = element.content.replaceAll(termKey, replacement) element.content = splitContent.join(" ")
// element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
// element.content = element.content.replaceAll(reg, "{TERM" + index + "}")
// element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
}
} }
// if (full) {
// var index = 0
// for (let i = 0; i < highlightedWords.length; i++) {
// var dict = highlightedWords[i]
// for (let x = 0; x < dict.words.length; x++) {
// index += 1
// var termKey = "{TERM" + index + "}"
// var termProps = dict
// while (element.content.includes(termKey)) {
// var termIndex = element.content.indexOf(termKey)
// var spanEnd = element.content.indexOf(" ", termIndex)
// if (spanEnd == -1) {
// spanEnd = element.content.length
// }
// var spanStart = element.content.substring(0, termIndex).lastIndexOf(" ") + 1
// // if (highTable[index] == "ULTRAKILL") {
// // console.log(startContent, " ---- ", endContent)
// // }
// var startContent = element.content.substring(spanStart - 1, termIndex)
// var endContent = element.content.substring(termIndex + termKey.length, spanEnd)
// // if (startContent.includes("(") && !endContent.includes(")")) {
// // spanEnd = element.content.indexOf(")", spanStart) + 1
// // endContent = element.content.substring(termIndex + termKey.length, spanEnd)
// // }
// // else if (endContent.includes(")") && !startContent.includes("(")) {
// // spanStart = element.content.substring(0, spanStart).lastIndexOf("(")
// // startContent = element.content.substring(spanStart - 1, termIndex)
// // }
// var style = termProps.style || ""
// var classes = termProps.classes || ""
// var link = termProps.link || ""
// if (termProps.color) {
// style += `color: ${termProps.color};`
// }
// if (termProps.italicized) {
// style += "font-style: italic;"
// }
// if (termProps.outline) {
// var width = 2
// // style += `text-shadow: -1px -1px 0 ${termProps.outline}, 1px -1px 0 ${termProps.outline}, -1px 1px 0 ${termProps.outline}, 1px 1px 0 ${termProps.outline};`
// style += `-webkit-text-stroke: 1px ${termProps.outline};`
// // ^ Not in use because it looks bad :30
// }
// if (termProps.bold) {
// classes += "bold"
// }
// if (style.length > 2) {
// style = `style="${style}"`
// }
// if (classes.length > 2) {
// classes = `class="${classes}"`
// }
// var stuff = (startContent + dict.words[x] + endContent).trim()
// if (!stuff.includes("span")) {
// var replacement = `<span ${style} ${classes}>${stuff}</span>`
// if (link && !linkParent) {
// replacement = `<a href="${link}">${replacement}</a>`
// // console.log(replacement)
// }
// element.content = element.content.substring(0, spanStart) + replacement + element.content.substring(spanEnd)
// } else {
// element.content = element.content.replace(termKey, dict.words[x])
// }
// }
// }
// }
// // element.content = element.content.replaceAll(termKey, replacement)
// }
} }
} }
@ -231,7 +280,7 @@ function highlighter(json, full = true, linkParent = false) {
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')))
var staticReplacers = { var staticReplacers = {
"ALL_HIGHLIGHTS": () => { "ALL_HIGHLIGHTS": () => {
var addedHTML = "" var addedHTML = ""
@ -311,7 +360,7 @@ function converter(html, dynamic = true) {
var siteName = sitesTable[x] var siteName = sitesTable[x]
var siteData = sites[siteName] var siteData = sites[siteName]
html += `<a class="chip" ${onlyIfExists(`href="${siteData.url}"`, siteData.url)}>${siteName}: <span class="noHighlight">${siteData.name}</span></a>` html += `<a class="chip" ${onlyIfExists(`href="${siteData.url}"`, siteData.url)}>${siteName}: <span class="noHighlight">${siteData.name}</span></a>`
} }
html += "</div></div>" html += "</div></div>"
} }
@ -323,8 +372,8 @@ function converter(html, dynamic = true) {
for (var i in api.blogPosts) { for (var i in api.blogPosts) {
var post = api.blogPosts[i] var post = api.blogPosts[i]
if (!post.hidden) { if (!post.hidden) {
addedHTML += addedHTML +=
`<div class="post"> `<div class="post">
<a style="text-decoration: none;" href="./${post.folder}"> <a style="text-decoration: none;" href="./${post.folder}">
<h2>${post.title}</h2> <h2>${post.title}</h2>
<p style="color: white; font-size: 1rem;">${post.desc}</p> <p style="color: white; font-size: 1rem;">${post.desc}</p>
@ -438,7 +487,7 @@ module.exports = {
} }
res.send(data) res.send(data)
} }
else { else {
next() next()
} }