|
| 1 | +import { createLowlight, common } from "lowlight" |
| 2 | + |
| 3 | +// NOTE: |
| 4 | +// `@git-diff-view/lowlight` pulls in `createLowlight(all)`, which drags almost the |
| 5 | +// whole highlight.js grammar registry into the deferred diff bundle. We intentionally |
| 6 | +// switch to `common` here to keep the diff viewer lightweight. Less-common languages |
| 7 | +// may fall back to auto-detection/plain highlighting; add targeted registrations here |
| 8 | +// if real-world usage shows important gaps. |
| 9 | + |
| 10 | +type AstNode = { |
| 11 | + type: string |
| 12 | + value?: string |
| 13 | + children?: AstNode[] |
| 14 | + startIndex?: number |
| 15 | + endIndex?: number |
| 16 | + lineNumber?: number |
| 17 | +} |
| 18 | + |
| 19 | +type SyntaxNodeEntry = { |
| 20 | + node: AstNode |
| 21 | + wrapper?: AstNode |
| 22 | +} |
| 23 | + |
| 24 | +type SyntaxFileLine = { |
| 25 | + value: string |
| 26 | + lineNumber: number |
| 27 | + valueLength: number |
| 28 | + nodeList: SyntaxNodeEntry[] |
| 29 | +} |
| 30 | + |
| 31 | +type LowlightApi = ReturnType<typeof createLowlight> |
| 32 | + |
| 33 | +export function processAST(ast: { children: AstNode[] }) { |
| 34 | + let lineNumber = 1 |
| 35 | + const syntaxObj: Record<number, SyntaxFileLine> = {} |
| 36 | + |
| 37 | + const loopAST = (nodes: AstNode[], wrapper?: AstNode) => { |
| 38 | + nodes.forEach((node) => { |
| 39 | + if (node.type === "text") { |
| 40 | + const textValue = node.value ?? "" |
| 41 | + if (!textValue.includes("\n")) { |
| 42 | + const valueLength = textValue.length |
| 43 | + if (!syntaxObj[lineNumber]) { |
| 44 | + node.startIndex = 0 |
| 45 | + node.endIndex = valueLength - 1 |
| 46 | + syntaxObj[lineNumber] = { |
| 47 | + value: textValue, |
| 48 | + lineNumber, |
| 49 | + valueLength, |
| 50 | + nodeList: [{ node, wrapper }], |
| 51 | + } |
| 52 | + } else { |
| 53 | + node.startIndex = syntaxObj[lineNumber].valueLength |
| 54 | + node.endIndex = node.startIndex + valueLength - 1 |
| 55 | + syntaxObj[lineNumber].value += textValue |
| 56 | + syntaxObj[lineNumber].valueLength += valueLength |
| 57 | + syntaxObj[lineNumber].nodeList.push({ node, wrapper }) |
| 58 | + } |
| 59 | + node.lineNumber = lineNumber |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + const lines = textValue.split("\n") |
| 64 | + node.children = node.children || [] |
| 65 | + for (let index = 0; index < lines.length; index++) { |
| 66 | + const value = index === lines.length - 1 ? lines[index] : `${lines[index]}\n` |
| 67 | + const currentLineNumber = index === 0 ? lineNumber : ++lineNumber |
| 68 | + const valueLength = value.length |
| 69 | + const childNode: AstNode = { |
| 70 | + type: "text", |
| 71 | + value, |
| 72 | + startIndex: Infinity, |
| 73 | + endIndex: Infinity, |
| 74 | + lineNumber: currentLineNumber, |
| 75 | + } |
| 76 | + |
| 77 | + if (!syntaxObj[currentLineNumber]) { |
| 78 | + childNode.startIndex = 0 |
| 79 | + childNode.endIndex = valueLength - 1 |
| 80 | + syntaxObj[currentLineNumber] = { |
| 81 | + value, |
| 82 | + lineNumber: currentLineNumber, |
| 83 | + valueLength, |
| 84 | + nodeList: [{ node: childNode, wrapper }], |
| 85 | + } |
| 86 | + } else { |
| 87 | + childNode.startIndex = syntaxObj[currentLineNumber].valueLength |
| 88 | + childNode.endIndex = childNode.startIndex + valueLength - 1 |
| 89 | + syntaxObj[currentLineNumber].value += value |
| 90 | + syntaxObj[currentLineNumber].valueLength += valueLength |
| 91 | + syntaxObj[currentLineNumber].nodeList.push({ node: childNode, wrapper }) |
| 92 | + } |
| 93 | + |
| 94 | + node.children.push(childNode) |
| 95 | + } |
| 96 | + |
| 97 | + node.lineNumber = lineNumber |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + if (node.children) { |
| 102 | + loopAST(node.children, node) |
| 103 | + node.lineNumber = lineNumber |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + loopAST(ast.children) |
| 109 | + return { syntaxFileObject: syntaxObj, syntaxFileLineNumber: lineNumber } |
| 110 | +} |
| 111 | + |
| 112 | +export function _getAST() { |
| 113 | + return {} |
| 114 | +} |
| 115 | + |
| 116 | +const lowlight = createLowlight(common) |
| 117 | + |
| 118 | +lowlight.register("vue", function hljsDefineVue(hljs: any) { |
| 119 | + return { |
| 120 | + subLanguage: "xml", |
| 121 | + contains: [ |
| 122 | + hljs.COMMENT("<!--", "-->", { relevance: 10 }), |
| 123 | + { |
| 124 | + begin: /^(\s*)(<script>)/gm, |
| 125 | + end: /^(\s*)(<\/script>)/gm, |
| 126 | + subLanguage: "javascript", |
| 127 | + excludeBegin: true, |
| 128 | + excludeEnd: true, |
| 129 | + }, |
| 130 | + { |
| 131 | + begin: /^(?:\s*)(?:<script\s+lang=(["'])ts\1>)/gm, |
| 132 | + end: /^(\s*)(<\/script>)/gm, |
| 133 | + subLanguage: "typescript", |
| 134 | + excludeBegin: true, |
| 135 | + excludeEnd: true, |
| 136 | + }, |
| 137 | + { |
| 138 | + begin: /^(\s*)(<style(\s+scoped)?>)/gm, |
| 139 | + end: /^(\s*)(<\/style>)/gm, |
| 140 | + subLanguage: "css", |
| 141 | + excludeBegin: true, |
| 142 | + excludeEnd: true, |
| 143 | + }, |
| 144 | + { |
| 145 | + begin: /^(?:\s*)(?:<style(?:\s+scoped)?\s+lang=(["'])(?:s[ca]ss)\1(?:\s+scoped)?>)/gm, |
| 146 | + end: /^(\s*)(<\/style>)/gm, |
| 147 | + subLanguage: "scss", |
| 148 | + excludeBegin: true, |
| 149 | + excludeEnd: true, |
| 150 | + }, |
| 151 | + { |
| 152 | + begin: /^(?:\s*)(?:<style(?:\s+scoped)?\s+lang=(["'])stylus\1(?:\s+scoped)?>)/gm, |
| 153 | + end: /^(\s*)(<\/style>)/gm, |
| 154 | + subLanguage: "stylus", |
| 155 | + excludeBegin: true, |
| 156 | + excludeEnd: true, |
| 157 | + }, |
| 158 | + ], |
| 159 | + } |
| 160 | +}) |
| 161 | + |
| 162 | +let maxLineToIgnoreSyntax = 2000 |
| 163 | +const ignoreSyntaxHighlightList: (string | RegExp)[] = [] |
| 164 | + |
| 165 | +export const highlighter = { |
| 166 | + name: "lowlight", |
| 167 | + get maxLineToIgnoreSyntax() { |
| 168 | + return maxLineToIgnoreSyntax |
| 169 | + }, |
| 170 | + setMaxLineToIgnoreSyntax(value: number) { |
| 171 | + maxLineToIgnoreSyntax = value |
| 172 | + }, |
| 173 | + get ignoreSyntaxHighlightList() { |
| 174 | + return ignoreSyntaxHighlightList |
| 175 | + }, |
| 176 | + setIgnoreSyntaxHighlightList(values: (string | RegExp)[]) { |
| 177 | + ignoreSyntaxHighlightList.length = 0 |
| 178 | + ignoreSyntaxHighlightList.push(...values) |
| 179 | + }, |
| 180 | + getAST(raw: string, fileName?: string, lang?: string) { |
| 181 | + const language = typeof lang === "string" ? lang.trim() : "" |
| 182 | + if ( |
| 183 | + fileName && |
| 184 | + ignoreSyntaxHighlightList.some((item) => (item instanceof RegExp ? item.test(fileName) : fileName === item)) |
| 185 | + ) { |
| 186 | + return undefined |
| 187 | + } |
| 188 | + |
| 189 | + if (language && lowlight.registered(language)) { |
| 190 | + return lowlight.highlight(language, raw) |
| 191 | + } |
| 192 | + |
| 193 | + return lowlight.highlightAuto(raw) |
| 194 | + }, |
| 195 | + processAST(ast: { children: AstNode[] }) { |
| 196 | + return processAST(ast) |
| 197 | + }, |
| 198 | + hasRegisteredCurrentLang(lang: string) { |
| 199 | + return lowlight.registered(lang) |
| 200 | + }, |
| 201 | + getHighlighterEngine(): LowlightApi { |
| 202 | + return lowlight |
| 203 | + }, |
| 204 | + type: "class" as const, |
| 205 | +} |
| 206 | + |
| 207 | +export const versions = "local-common" |
0 commit comments