2023-01-13 01:43:20 +01:00
|
|
|
importScripts("highlight.min.js")
|
2023-01-12 18:55:55 +01:00
|
|
|
|
2023-01-16 23:43:22 +01:00
|
|
|
const HIGHLIGHTJS_LANGUAGES = [
|
|
|
|
"json",
|
|
|
|
"python",
|
|
|
|
"javascript",
|
|
|
|
"html",
|
|
|
|
"sql",
|
|
|
|
"java",
|
|
|
|
"plaintext",
|
|
|
|
"cpp",
|
|
|
|
"php",
|
|
|
|
"css",
|
|
|
|
"markdown",
|
|
|
|
"xml",
|
|
|
|
"rust",
|
|
|
|
]
|
2023-01-03 16:56:07 +01:00
|
|
|
|
|
|
|
onmessage = (event) => {
|
|
|
|
//console.log("worker received message:", event.data)
|
2023-01-12 18:55:55 +01:00
|
|
|
//importScripts("../../lib/highlight.min.js")
|
|
|
|
|
2023-03-07 15:01:41 +01:00
|
|
|
const content = event.data.content
|
|
|
|
|
|
|
|
// we first check some custom heuristic rules to determine if the language is JSON
|
|
|
|
const trimmedContent = content.trim()
|
|
|
|
if ((
|
|
|
|
trimmedContent.startsWith("{") &&
|
|
|
|
trimmedContent.endsWith("}")
|
|
|
|
) || (
|
|
|
|
trimmedContent.startsWith("[") &&
|
|
|
|
trimmedContent.endsWith("]")
|
|
|
|
)) {
|
|
|
|
try {
|
|
|
|
if (typeof JSON.parse(trimmedContent) === "object") {
|
|
|
|
postMessage({
|
|
|
|
highlightjs: {
|
|
|
|
language: "json",
|
|
|
|
relevance: 100,
|
|
|
|
illegal: false,
|
|
|
|
},
|
|
|
|
content: content,
|
|
|
|
idx: event.data.idx,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// JSON could not be parsed, do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = self.hljs.highlightAuto(content, HIGHLIGHTJS_LANGUAGES);
|
2023-01-03 16:56:07 +01:00
|
|
|
postMessage({
|
|
|
|
highlightjs: {
|
|
|
|
language: result.language,
|
|
|
|
relevance: result.relevance,
|
|
|
|
illegal: result.illegal,
|
|
|
|
},
|
2023-03-07 15:01:41 +01:00
|
|
|
content: content,
|
2023-01-03 16:56:07 +01:00
|
|
|
idx: event.data.idx,
|
|
|
|
})
|
|
|
|
}
|