108 lines
4.2 KiB
JavaScript
108 lines
4.2 KiB
JavaScript
const jsKeywords = new Set([
|
|
"abstract", "arguments", "await*", "boolean", "break", "byte", "case",
|
|
"catch", "char", "class*", "const", "continue", "debugger", "default",
|
|
"delete", "do", "double", "else", "enum*", "eval", "export*", "extends*",
|
|
"false", "final", "finally", "float", "for", "function", "goto", "if",
|
|
"implements", "import*", "in", "instanceof", "int", "interface", "let*",
|
|
"long", "native", "new", "null", "package", "private", "protected", "public",
|
|
"return", "short", "static", "super*", "switch", "synchronized", "this",
|
|
"throw", "throws", "transient", "true", "try", "typeof", "var", "void",
|
|
"volatile", "while", "with", "yield"
|
|
])
|
|
|
|
const csKeywords = new Set([
|
|
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char",
|
|
"checked", "class", "const", "continue", "decimal", "default", "delegate",
|
|
"do", "double", "else", "enum", "event", "explicit", "extern", "false",
|
|
"finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit",
|
|
"in", "int", "interface", "internal", "is", "lock", "long", "namespace",
|
|
"new", "null", "object", "operator", "out", "override", "params", "private",
|
|
"protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
|
|
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch",
|
|
"this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked",
|
|
"unsafe", "ushort", "using", "virtual", "void", "volatile", "while",
|
|
])
|
|
|
|
const javaKeywords = new Set([
|
|
"abstract", "continue", "for", "new", "switch", "assert", "default",
|
|
"goto", "package", "synchronized", "boolean", "do", "if", "private",
|
|
"this", "break", "double", "implements", "protected", "throw", "byte",
|
|
"else", "import", "public", "throws", "case", "enum", "instanceof",
|
|
"return", "transient", "catch", "extends", "int", "short", "try", "char",
|
|
"final", "interface", "static", "void", "class", "finally", "long",
|
|
"strictfp", "volatile", "const", "float", "native", "super", "while",
|
|
])
|
|
|
|
const keywordMap = {
|
|
js: jsKeywords,
|
|
mjs: jsKeywords,
|
|
java: javaKeywords,
|
|
cs: csKeywords,
|
|
none: new Set()
|
|
}
|
|
|
|
|
|
const bookendedWith = (text, bookend) => text.startsWith(bookend) && text.endsWith(bookend)
|
|
|
|
const getClass = (text, keywords) => {
|
|
if (keywords.has(text)) {
|
|
return 'hl-keyword'
|
|
}
|
|
|
|
if (bookendedWith(text, '"')) {
|
|
return 'hl-string'
|
|
}
|
|
|
|
return 'hl-variable'
|
|
}
|
|
|
|
const getKeywords = () => keywordMap[getFileName()?.replace(/.*\./g, '')] || keywordMap.none
|
|
|
|
const commentSpan = innerText => {
|
|
const span = document.createElement('span')
|
|
span.innerText = innerText
|
|
span.classList.add('hl-comment')
|
|
return span
|
|
}
|
|
|
|
const colorCodeInComment = (codeStart = '`', codeEnd) => {
|
|
codeEnd ??= codeStart
|
|
const processed = new Set()
|
|
const keywords = getKeywords()
|
|
getClassNameElementsArray('hl-comment').forEach(element => {
|
|
if (!element.innerText?.startsWith(codeStart) || processed.has(element)) {
|
|
return
|
|
}
|
|
const codeStartElement = commentSpan(codeStart)
|
|
element.replaceWith(codeStartElement, element)
|
|
element.innerText = element.innerText.replace(codeStart, '')
|
|
while (element) {
|
|
if (!element.classList.contains('hl-comment')) {
|
|
break
|
|
}
|
|
processed.add(element)
|
|
element.classList.remove('hl-comment')
|
|
const text = element.innerText.replaceAll(codeStart, '').replaceAll(codeEnd, '')
|
|
element.classList.add(getClass(text, keywords))
|
|
const codeEndIndex = element.innerText.indexOf(codeEnd)
|
|
if (codeEndIndex > -1) {
|
|
const codeEndElement = commentSpan(element.innerText.substring(codeEndIndex))
|
|
element.innerText = element.innerText.substring(0, codeEndIndex)
|
|
element.replaceWith(element, codeEndElement)
|
|
break
|
|
}
|
|
element = element.nextElementSibling
|
|
}
|
|
})
|
|
}
|
|
|
|
const commentCodeFix = () => {
|
|
colorCodeInComment('`', '`')
|
|
colorCodeInComment('<code>', '</code>')
|
|
colorCodeInComment('<pre>', '</pre>')
|
|
colorCodeInComment('<tt>', '</tt>')
|
|
}
|
|
|
|
|
|
addFix(commentCodeFix)
|