Skip to content

Commit ac58d16

Browse files
committed
实现代码语言自动检测功能,增强代码块处理逻辑,支持多种编程语言的识别与标记。优化用户体验,确保在处理代码时自动识别语言类型并进行相应格式化,同时更新国际化文本以支持新的功能,确保多语言支持的准确性和一致性。
1 parent 2c9f68e commit ac58d16

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

src/components/AITestDialog.tsx

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,138 @@ ${fileContents}`);
19341934
'<span class="break-all">$1</span>'
19351935
);
19361936

1937+
// 自动检测代码语言
1938+
const detectLanguage = (code: string): string => {
1939+
// 检查代码中的特征来判断语言
1940+
if (/\bimport\s+React|\bfrom\s+['"]react['"]/i.test(code)) {
1941+
return code.includes("tsx") || code.includes(":") ? "tsx" : "jsx";
1942+
}
1943+
if (
1944+
/\bimport\s+|\bexport\s+|\bconst\s+\w+\s*=\s*|let\s+\w+\s*=\s*/i.test(
1945+
code
1946+
)
1947+
) {
1948+
return code.includes(":") ||
1949+
code.includes("interface") ||
1950+
code.includes("type ")
1951+
? "typescript"
1952+
: "javascript";
1953+
}
1954+
if (/^(<!DOCTYPE|<html|<head|<body)/i.test(code)) {
1955+
return "html";
1956+
}
1957+
if (/class=".*?"|className=".*?"|<div|<span|<p>/i.test(code)) {
1958+
return "jsx";
1959+
}
1960+
if (/^\s*\.[\w-]+\s*{|@media|@keyframes/i.test(code)) {
1961+
return "css";
1962+
}
1963+
if (/SELECT|INSERT|UPDATE|DELETE|CREATE TABLE/i.test(code)) {
1964+
return "sql";
1965+
}
1966+
if (/def\s+\w+\s*\(|import\s+\w+|from\s+\w+\s+import/i.test(code)) {
1967+
return "python";
1968+
}
1969+
if (/public\s+(static\s+)?(void|class|int|boolean)/i.test(code)) {
1970+
return "java";
1971+
}
1972+
if (/^\s*package\s+\w+|func\s+\w+\s*\(/i.test(code)) {
1973+
return "go";
1974+
}
1975+
if (/^#include\s+<|std::|int\s+main\s*\(\s*\)/i.test(code)) {
1976+
return "cpp";
1977+
}
1978+
if (
1979+
/\$\w+\s*=|\$\w+\s*->|function\s+\w+\s*\(/i.test(code) &&
1980+
code.includes("<?php")
1981+
) {
1982+
return "php";
1983+
}
1984+
if (/^\s*{\s*["']\w+["']\s*:/i.test(code)) {
1985+
return "json";
1986+
}
1987+
if (/npm|yarn|apt-get|sudo|bash|sh\s+/i.test(code)) {
1988+
return "bash";
1989+
}
1990+
if (/^#!\/bin\/bash|^#!\/bin\/sh/i.test(code)) {
1991+
return "bash";
1992+
}
1993+
if (/^\s*#\s+|^##\s+|^\*\*\s+/i.test(code)) {
1994+
return "markdown";
1995+
}
1996+
1997+
// 检查文件扩展名引用
1998+
const extMatch = code.match(/\.([a-zA-Z0-9]+)(\s|$|['")\]}]|:|,)/);
1999+
if (extMatch) {
2000+
const ext = extMatch[1].toLowerCase();
2001+
switch (ext) {
2002+
case "js":
2003+
return "javascript";
2004+
case "jsx":
2005+
return "jsx";
2006+
case "ts":
2007+
return "typescript";
2008+
case "tsx":
2009+
return "tsx";
2010+
case "py":
2011+
return "python";
2012+
case "rb":
2013+
return "ruby";
2014+
case "java":
2015+
return "java";
2016+
case "php":
2017+
return "php";
2018+
case "go":
2019+
return "go";
2020+
case "cs":
2021+
return "csharp";
2022+
case "html":
2023+
return "html";
2024+
case "css":
2025+
return "css";
2026+
case "scss":
2027+
return "scss";
2028+
case "less":
2029+
return "less";
2030+
case "json":
2031+
return "json";
2032+
case "md":
2033+
return "markdown";
2034+
case "xml":
2035+
return "xml";
2036+
case "yml":
2037+
case "yaml":
2038+
return "yaml";
2039+
case "sh":
2040+
return "bash";
2041+
case "sql":
2042+
return "sql";
2043+
}
2044+
}
2045+
2046+
return "";
2047+
};
2048+
2049+
// 处理代码块,自动检测语言
2050+
processed = processed.replace(
2051+
/```(?:(\w+)\n)?([\s\S]*?)```/g,
2052+
(match, lang, code) => {
2053+
// 如果已有语言标识,使用它
2054+
if (lang && lang !== "text" && lang !== "plaintext") {
2055+
return match;
2056+
}
2057+
2058+
// 自动检测语言
2059+
const detectedLang = detectLanguage(code);
2060+
if (detectedLang) {
2061+
return `\`\`\`${detectedLang}\n${code}\`\`\``;
2062+
}
2063+
2064+
// 如果无法检测到语言,使用text
2065+
return `\`\`\`text\n${code}\`\`\``;
2066+
}
2067+
);
2068+
19372069
// 防止文件名和函数关键字被错误地渲染为代码块
19382070
// 1. 修复文件名格式: 将 ```filename.ext``` 替换为 `filename.ext`
19392071
processed = processed.replace(

0 commit comments

Comments
 (0)