|
| 1 | +import { createLowlight, all } from "lowlight"; |
| 2 | + |
| 3 | +import { processAST, type SyntaxLine } from "./processAST"; |
| 4 | + |
| 5 | +const lowlight = createLowlight(all); |
| 6 | + |
| 7 | +lowlight.register("vue", function hljsDefineVue(hljs) { |
| 8 | + return { |
| 9 | + subLanguage: "xml", |
| 10 | + contains: [ |
| 11 | + hljs.COMMENT("<!--", "-->", { |
| 12 | + relevance: 10, |
| 13 | + }), |
| 14 | + { |
| 15 | + begin: /^(\s*)(<script>)/gm, |
| 16 | + end: /^(\s*)(<\/script>)/gm, |
| 17 | + subLanguage: "javascript", |
| 18 | + excludeBegin: true, |
| 19 | + excludeEnd: true, |
| 20 | + }, |
| 21 | + { |
| 22 | + begin: /^(?:\s*)(?:<script\s+lang=(["'])ts\1>)/gm, |
| 23 | + end: /^(\s*)(<\/script>)/gm, |
| 24 | + subLanguage: "typescript", |
| 25 | + excludeBegin: true, |
| 26 | + excludeEnd: true, |
| 27 | + }, |
| 28 | + { |
| 29 | + begin: /^(\s*)(<style(\s+scoped)?>)/gm, |
| 30 | + end: /^(\s*)(<\/style>)/gm, |
| 31 | + subLanguage: "css", |
| 32 | + excludeBegin: true, |
| 33 | + excludeEnd: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + begin: /^(?:\s*)(?:<style(?:\s+scoped)?\s+lang=(["'])(?:s[ca]ss)\1(?:\s+scoped)?>)/gm, |
| 37 | + end: /^(\s*)(<\/style>)/gm, |
| 38 | + subLanguage: "scss", |
| 39 | + excludeBegin: true, |
| 40 | + excludeEnd: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + begin: /^(?:\s*)(?:<style(?:\s+scoped)?\s+lang=(["'])stylus\1(?:\s+scoped)?>)/gm, |
| 44 | + end: /^(\s*)(<\/style>)/gm, |
| 45 | + subLanguage: "stylus", |
| 46 | + excludeBegin: true, |
| 47 | + excludeEnd: true, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }; |
| 51 | +}); |
| 52 | + |
| 53 | +export type AST = ReturnType<typeof lowlight.highlight>; |
| 54 | + |
| 55 | +export const highlighter = lowlight as typeof lowlight & { |
| 56 | + maxLineToIgnoreSyntax: number; |
| 57 | + autoDetectLang: boolean; |
| 58 | + setMaxLineToIgnoreSyntax: (v: number) => void; |
| 59 | + setAutoDetectLang: (v: boolean) => void; |
| 60 | + ignoreSyntaxHighlightList: (string | RegExp)[]; |
| 61 | + setIgnoreSyntaxHighlightList: (v: (string | RegExp)[]) => void; |
| 62 | + getAST: (raw: string, fileName?: string, lang?: string) => AST; |
| 63 | + processAST: (ast: AST) => { syntaxFileObject: Record<number, SyntaxLine>; syntaxFileLineNumber: number }; |
| 64 | +}; |
| 65 | + |
| 66 | +let _autoDetectLang = true; |
| 67 | + |
| 68 | +let _maxLineToIgnoreSyntax = 2000; |
| 69 | + |
| 70 | +const _ignoreSyntaxHighlightList: (string | RegExp)[] = []; |
| 71 | + |
| 72 | +Object.defineProperty(highlighter, "maxLineToIgnoreSyntax", { |
| 73 | + get: () => _maxLineToIgnoreSyntax, |
| 74 | +}); |
| 75 | + |
| 76 | +Object.defineProperty(highlighter, "setMaxLineToIgnoreSyntax", { |
| 77 | + value: (v: number) => { |
| 78 | + _maxLineToIgnoreSyntax = v; |
| 79 | + }, |
| 80 | +}); |
| 81 | + |
| 82 | +Object.defineProperty(highlighter, "autoDetectLang", { |
| 83 | + get: () => _autoDetectLang, |
| 84 | +}); |
| 85 | + |
| 86 | +Object.defineProperty(highlighter, "setAutoDetectLang", { |
| 87 | + value: (v: boolean) => { |
| 88 | + _autoDetectLang = v; |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +Object.defineProperty(highlighter, "ignoreSyntaxHighlightList", { |
| 93 | + get: () => _ignoreSyntaxHighlightList, |
| 94 | +}); |
| 95 | + |
| 96 | +Object.defineProperty(highlighter, "setIgnoreSyntaxHighlightList", { |
| 97 | + value: (v: (string | RegExp)[]) => { |
| 98 | + _ignoreSyntaxHighlightList.length = 0; |
| 99 | + _ignoreSyntaxHighlightList.push(...v); |
| 100 | + }, |
| 101 | +}); |
| 102 | + |
| 103 | +Object.defineProperty(highlighter, "getAST", { |
| 104 | + value: (raw: string, fileName?: string, lang?: string) => { |
| 105 | + let hasRegisteredLang = true; |
| 106 | + |
| 107 | + if (!highlighter.registered(lang)) { |
| 108 | + __DEV__ && console.warn(`not support current lang: ${lang} yet`); |
| 109 | + hasRegisteredLang = false; |
| 110 | + } |
| 111 | + |
| 112 | + if ( |
| 113 | + fileName && |
| 114 | + highlighter.ignoreSyntaxHighlightList.some((item) => |
| 115 | + item instanceof RegExp ? item.test(fileName) : fileName === item |
| 116 | + ) |
| 117 | + ) { |
| 118 | + __DEV__ && |
| 119 | + console.warn( |
| 120 | + `ignore syntax for current file, because the fileName is in the ignoreSyntaxHighlightList: ${fileName}` |
| 121 | + ); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (hasRegisteredLang) { |
| 126 | + return highlighter.highlight(lang, raw); |
| 127 | + } else { |
| 128 | + return highlighter.highlightAuto(raw); |
| 129 | + } |
| 130 | + }, |
| 131 | +}); |
| 132 | + |
| 133 | +Object.defineProperty(highlighter, "processAST", { |
| 134 | + value: (ast: AST) => { |
| 135 | + return processAST(ast); |
| 136 | + }, |
| 137 | +}); |
0 commit comments