73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
import Prism from "prismjs";
|
|
|
|
// 平台组件会直接编译 src/editor,不能依赖 timi-web 构建阶段的 Prism 插件注入语言。
|
|
import "prismjs/components/prism-apacheconf";
|
|
import "prismjs/components/prism-bash";
|
|
import "prismjs/components/prism-css";
|
|
import "prismjs/components/prism-ini";
|
|
import "prismjs/components/prism-java";
|
|
import "prismjs/components/prism-javascript";
|
|
import "prismjs/components/prism-groovy";
|
|
import "prismjs/components/prism-json";
|
|
import "prismjs/components/prism-less";
|
|
import "prismjs/components/prism-markup";
|
|
import "prismjs/components/prism-markup-templating";
|
|
import "prismjs/components/prism-nginx";
|
|
import "prismjs/components/prism-php";
|
|
import "prismjs/components/prism-properties";
|
|
import "prismjs/components/prism-sql";
|
|
import "prismjs/components/prism-typescript";
|
|
import "prismjs/components/prism-yaml";
|
|
|
|
/** Prism 已加载并支持语法高亮的代码语言。 */
|
|
export type CodeLanguageOption = {
|
|
label: string;
|
|
value: string;
|
|
};
|
|
|
|
/** 编辑器可用的代码语言,按名称首字母排序。 */
|
|
export const supportedCodeLanguages: CodeLanguageOption[] = [
|
|
{ label: "Apache Config", value: "apacheconf" },
|
|
{ label: "Bash", value: "bash" },
|
|
{ label: "CSS", value: "css" },
|
|
{ label: "HTML", value: "html" },
|
|
{ label: "INI", value: "ini" },
|
|
{ label: "Groovy", value: "groovy" },
|
|
{ label: "Java", value: "java" },
|
|
{ label: "JavaScript", value: "javascript" },
|
|
{ label: "JSON", value: "json" },
|
|
{ label: "Less", value: "less" },
|
|
{ label: "Nginx", value: "nginx" },
|
|
{ label: "PHP", value: "php" },
|
|
{ label: "Properties", value: "properties" },
|
|
{ label: "Shell", value: "sh" },
|
|
{ label: "SQL", value: "sql" },
|
|
{ label: "TypeScript", value: "typescript" },
|
|
{ label: "Vue", value: "vue" },
|
|
{ label: "XML", value: "xml" },
|
|
{ label: "YAML", value: "yaml" }
|
|
];
|
|
|
|
const prismLanguageAliasMap: Record<string, string> = {
|
|
html: "markup",
|
|
xml: "markup",
|
|
js: "javascript",
|
|
ts: "typescript",
|
|
sh: "bash",
|
|
vue: "markup"
|
|
};
|
|
|
|
/**
|
|
* 把语言名归一化为 Prism 使用的名称,处理 `html → markup`、`ts → typescript`、`sh → bash` 等别名
|
|
*
|
|
* @param language 语言名或别名
|
|
* @returns Prism 语言名,语言未加载或不支持时返回空字符串
|
|
*/
|
|
export function resolvePrismLanguage(language?: string | null) {
|
|
if (!language) {
|
|
return "";
|
|
}
|
|
const nextLanguage = prismLanguageAliasMap[language] || language;
|
|
return Prism.languages[nextLanguage] ? nextLanguage : "";
|
|
}
|