Skip to content

Commit 553bf75

Browse files
author
songyiping03
committed
feat: 优化语言统计
1 parent f4b7a4a commit 553bf75

4 files changed

Lines changed: 33 additions & 12 deletions

File tree

data/content-handler.data.mts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,38 @@ import { matchCodeBlocks } from '../utils/md-statistics';
44
import { pie_base64 } from '../utils/chart';
55
import { countLines } from '../utils/common';
66

7+
const langMap = {
8+
javascript: ['js', 'javascript', 'JavaScript', 'jsx'],
9+
cpp: ['c++', 'cpp', 'CPP'],
10+
typescript: ['ts', 'typescript', 'TypeScript', 'tsx'],
11+
python: ['py', 'python', 'Python'],
12+
java: ['java', 'Java'],
13+
go: ['go', 'Go', 'golang'],
14+
rust: ['rs', 'rust', 'Rust'],
15+
html: ['html', 'HTML', 'htm'],
16+
css: ['css', 'CSS', 'scss', 'Sass', 'sass', 'less', 'Less'],
17+
shell: ['sh', 'bash', 'Shell', 'zsh'],
18+
text: ['text', 'Text', 'txt', 'Txt', 'plaintext', 'Plaintext', 'in', 'out'],
19+
}
20+
21+
const getStdLang = (lang: string) => {
22+
for (const [stdLang, variants] of Object.entries(langMap)) {
23+
if (variants.includes(lang)) {
24+
return stdLang;
25+
}
26+
}
27+
return lang;
28+
}
29+
730
export default {
831
async load() {
9-
const count = new EasyMap<string, number>({defaultValue: 0});
32+
const count = new EasyMap<string, number>();
1033
const proxy = count.createProxy(p => {
1134
return typeof p === 'string' ? p : p.toString();
12-
});
35+
}, 0);
1336
loadFiles('_posts/**/*.md', (content) => {
1437
matchCodeBlocks(content).forEach(({language, code}) => {
15-
if (language === 'c++') language = 'cpp';
16-
if (language === 'in' || language === 'out') language = 'text';
17-
proxy[language] += countLines(code);
38+
proxy[getStdLang(language)] += countLines(code);
1839
});
1940
});
2041
const pieData = {

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@docsearch/js": "^4.0.1",
2525
"@eslint/js": "^9.10.0",
2626
"@trudbot/json-format": "^0.0.4",
27-
"@trudbot/map": "^0.0.4",
27+
"@trudbot/map": "^0.0.5",
2828
"@types/d3": "^7.4.3",
2929
"@types/d3-cloud": "^1.2.9",
3030
"@types/lodash": "^4.17.20",

utils/ts-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// 定义一个类型来提取函数的返回类型
2-
export type FunctionReturnType<F> = F extends (...args: unknown[]) => infer R ? R : never;
2+
export type FunctionReturnType<F> = F extends (...args: never[]) => infer R ? R : never;
33

44
// 定义一个类型来将函数列表映射为返回值的类型数组
5-
export type MapFunctionReturnTypes<T extends Array<(...args: unknown[]) => unknown>> = {
5+
export type MapFunctionReturnTypes<T extends Array<(...args: never[]) => unknown>> = {
66
[K in keyof T]: FunctionReturnType<T[K]>
77
};

0 commit comments

Comments
 (0)