- Wails v2.12.0升级(App绑定新增API、runtime类型扩展) - 修复mermaid暗色主题切换渲染失败(SVG textContent污染→data-mermaid-src保存源码) - 修复代码高亮全语言失效(languageMap静态白名单替代运行时hljs检查) - 文件系统:FileListPanel重写、FileItemRow合并删除、Toolbar简化 - 新增剪贴板图片粘贴(Ctrl+V粘贴图片到当前目录) - 死代码清理:DeviceTest/errorHandler/useLocalStorage移除 - MarkdownEditor优化、theme store增强、CodeMirror加载器精简
152 lines
5.0 KiB
TypeScript
152 lines
5.0 KiB
TypeScript
/**
|
||
* 统一语言映射
|
||
* 供 highlight.js(Markdown 预览)和 CodeMirror(代码编辑器)共用
|
||
*/
|
||
|
||
/**
|
||
* 文件扩展名/缩写 → 语言标识符
|
||
* - hljs: 用于 markedExtensions.ts 的代码块高亮
|
||
* - cm: 用于 codeMirrorLoader.js 的编辑器语言
|
||
* 值为 false 表示该扩展名不对应任何编程语言
|
||
*/
|
||
const extensionToLanguage: Record<string, { hljs?: string; cm?: string }> = {
|
||
// === JavaScript / TypeScript ===
|
||
js: { hljs: 'javascript', cm: 'javascript' },
|
||
jsx: { hljs: 'javascript', cm: 'javascript' },
|
||
mjs: { hljs: 'javascript', cm: 'javascript' },
|
||
cjs: { hljs: 'javascript', cm: 'javascript' },
|
||
ts: { hljs: 'typescript', cm: 'typescript' },
|
||
tsx: { hljs: 'typescript', cm: 'typescript' },
|
||
cts: { hljs: 'typescript', cm: 'typescript' },
|
||
mts: { hljs: 'typescript', cm: 'typescript' },
|
||
|
||
// === Web ===
|
||
html: { hljs: 'xml', cm: 'html' },
|
||
htm: { hljs: 'xml', cm: 'html' },
|
||
css: { hljs: 'css', cm: 'css' },
|
||
scss: { hljs: 'scss', cm: 'css' },
|
||
sass: { hljs: 'scss', cm: 'css' },
|
||
less: { hljs: 'less', cm: 'css' },
|
||
vue: { hljs: 'xml', cm: 'html' },
|
||
|
||
// === 数据格式 ===
|
||
json: { hljs: 'json', cm: 'json' },
|
||
xml: { hljs: 'xml', cm: 'html' },
|
||
yaml: { hljs: 'yaml', cm: 'yaml' },
|
||
yml: { hljs: 'yaml', cm: 'yaml' },
|
||
toml: { cm: 'text' },
|
||
csv: { cm: 'text' },
|
||
tsv: { cm: 'text' },
|
||
|
||
// === C / C++ / 系统编程 ===
|
||
c: { hljs: 'c', cm: 'cpp' },
|
||
cpp: { hljs: 'cpp', cm: 'cpp' },
|
||
cc: { hljs: 'cpp', cm: 'cpp' },
|
||
cxx: { hljs: 'cpp', cm: 'cpp' },
|
||
h: { hljs: 'cpp', cm: 'cpp' },
|
||
hpp: { hljs: 'cpp', cm: 'cpp' },
|
||
hxx: { hljs: 'cpp', cm: 'cpp' },
|
||
cs: { hljs: 'csharp', cm: 'text' },
|
||
swift: { hljs: 'swift', cm: 'text' },
|
||
kt: { hljs: 'kotlin', cm: 'text' },
|
||
rs: { hljs: 'rust', cm: 'rust' },
|
||
go: { hljs: 'go', cm: 'go' },
|
||
java: { hljs: 'java', cm: 'java' },
|
||
pch: { hljs: 'cpp', cm: 'cpp' },
|
||
tcc: { hljs: 'cpp', cm: 'cpp' },
|
||
|
||
// === 脚本 ===
|
||
py: { hljs: 'python', cm: 'python' },
|
||
pyw: { hljs: 'python', cm: 'python' },
|
||
rb: { hljs: 'ruby', cm: 'text' },
|
||
php: { hljs: 'php', cm: 'php' },
|
||
sh: { hljs: 'bash', cm: 'shell' },
|
||
bash: { hljs: 'bash', cm: 'shell' },
|
||
shell: { hljs: 'bash', cm: 'shell' },
|
||
zsh: { hljs: 'bash', cm: 'shell' },
|
||
ps1: { hljs: 'powershell', cm: 'powershell' },
|
||
bat: { hljs: 'dos', cm: 'text' },
|
||
ahk: { hljs: 'autohotkey', cm: 'text' },
|
||
lua: { hljs: 'lua', cm: 'text' },
|
||
r: { hljs: 'r', cm: 'text' },
|
||
m: { hljs: 'objectivec', cm: 'text' },
|
||
scala: { hljs: 'scala', cm: 'text' },
|
||
dart: { hljs: 'dart', cm: 'dart' },
|
||
|
||
// === 数据库 / 标记 ===
|
||
sql: { hljs: 'sql', cm: 'sql' },
|
||
md: { hljs: 'markdown', cm: 'markdown' },
|
||
markdown: { hljs: 'markdown', cm: 'markdown' },
|
||
tex: { hljs: 'latex', cm: 'text' },
|
||
rst: { hljs: 'plaintext', cm: 'text' },
|
||
adoc: { hljs: 'plaintext', cm: 'text' },
|
||
|
||
// === 构建工具 / 配置 ===
|
||
// CodeMirror 6 无内置 Dockerfile 支持,用 shell 模式近似(Dockerfile 本质是类 shell 指令)
|
||
dockerfile: { hljs: 'dockerfile', cm: 'shell' },
|
||
makefile: { hljs: 'makefile', cm: 'text' },
|
||
mk: { hljs: 'makefile', cm: 'text' },
|
||
cmake: { hljs: 'cmake', cm: 'text' },
|
||
ini: { hljs: 'ini', cm: 'text' },
|
||
cfg: { hljs: 'ini', cm: 'text' },
|
||
conf: { hljs: 'ini', cm: 'text' },
|
||
env: { cm: 'text' },
|
||
props: { cm: 'text' },
|
||
manifest: { cm: 'text' },
|
||
lock: { cm: 'text' },
|
||
ignore: { cm: 'text' },
|
||
|
||
// === 纯文本 ===
|
||
txt: { cm: 'text' },
|
||
text: { cm: 'text' },
|
||
log: { cm: 'text' },
|
||
msg: { cm: 'text' },
|
||
}
|
||
|
||
// 从映射表中收集所有已知的 hljs 语言名
|
||
const knownHljsLanguages = new Set(
|
||
Object.values(extensionToLanguage)
|
||
.map(v => v.hljs)
|
||
.filter(Boolean) as string[]
|
||
)
|
||
|
||
// highlight.js lib/common 内置的常用语言名(代码块标记直接使用)
|
||
const commonLangNames = new Set([
|
||
'javascript', 'typescript', 'python', 'java', 'go', 'rust', 'c', 'cpp',
|
||
'csharp', 'php', 'ruby', 'swift', 'kotlin', 'scala', 'dart', 'lua',
|
||
'r', 'bash', 'shell', 'powershell', 'dos', 'cmd', 'sql', 'yaml', 'json', 'xml',
|
||
'markdown', 'css', 'scss', 'less', 'html', 'ini', 'makefile', 'dockerfile',
|
||
'cmake', 'latex', 'plaintext', 'diff', 'graphql', 'nginx', 'perl',
|
||
'objectivec', 'haskell', 'elixir', 'erlang', 'clojure', 'ocaml',
|
||
'vbnet', 'wasm', 'fsharp', 'groovy', 'julia', 'matlab', 'zig'
|
||
])
|
||
|
||
/**
|
||
* 获取 hljs 语言标识(带别名解析)
|
||
*/
|
||
export function getHljsLanguage(langOrExt: string): string {
|
||
if (!langOrExt) return 'plaintext'
|
||
const lower = langOrExt.toLowerCase()
|
||
|
||
// 1. 直接是已知语言名(代码块 ```python / ```typescript 等)
|
||
if (commonLangNames.has(lower)) return lower
|
||
|
||
// 2. 扩展名映射(.ts → typescript, .py → python 等)
|
||
const mapped = extensionToLanguage[lower]?.hljs
|
||
if (mapped) return mapped
|
||
|
||
// 3. 已在 known 集合中的映射值
|
||
if (knownHljsLanguages.has(lower)) return lower
|
||
|
||
return 'plaintext'
|
||
}
|
||
|
||
/**
|
||
* 获取 CodeMirror 语言标识
|
||
*/
|
||
export function getCmLanguage(extension: string): string {
|
||
if (!extension) return 'text'
|
||
const lower = extension.toLowerCase()
|
||
return extensionToLanguage[lower]?.cm || 'text'
|
||
}
|