Skip to content

fix: use pinyin instead of Chinese in file name for leetcode-cn problem #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 70 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@
"lint": "tslint --project tsconfig.json -e src/*.d.ts -t verbose"
},
"devDependencies": {
"@types/fs-extra": "5.0.0",
"@types/fs-extra": "^5.0.5",
"@types/lodash.kebabcase": "^4.1.5",
"@types/mocha": "^2.2.42",
"@types/node": "^7.0.43",
"@types/pinyin": "^2.8.1",
"@types/require-from-string": "^1.2.0",
"tslint": "^5.9.1",
"typescript": "^2.6.1",
Expand All @@ -279,6 +280,7 @@
"fs-extra": "^6.0.1",
"leetcode-cli": "2.6.1",
"lodash.kebabcase": "^4.1.1",
"pinyin": "^2.8.3",
"require-from-string": "^2.0.2"
}
}
2 changes: 1 addition & 1 deletion src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function showProblemInternal(node: IProblem): Promise<void> {
outDir = path.join(outDir, relativePath);
await fse.ensureDir(outDir);

const originFilePath: string = await leetCodeExecutor.showProblem(node, language, outDir);
const originFilePath: string = await leetCodeExecutor.showProblem(node, language, leetCodeConfig.endpoint, outDir);
const filePath: string = wsl.useWsl() ? await wsl.toWinPath(originFilePath) : originFilePath;
await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false });

Expand Down
4 changes: 2 additions & 2 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class LeetCodeExecutor {
);
}

public async showProblem(node: IProblem, language: string, outDir: string): Promise<string> {
const fileName: string = genFileName(node, language);
public async showProblem(node: IProblem, language: string, endpoint: Endpoint, outDir: string): Promise<string> {
const fileName: string = genFileName(node, language, endpoint);
const filePath: string = path.join(outDir, fileName);

if (!await fse.pathExists(filePath)) {
Expand Down
17 changes: 14 additions & 3 deletions src/utils/problemUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import kebabCase = require("lodash.kebabcase");
import { IProblem, langExt } from "../shared";
import * as pinyin from "pinyin";
import { Endpoint, IProblem, langExt } from "../shared";

export function genFileExt(language: string): string {
const ext: string | undefined = langExt.get(language);
Expand All @@ -9,8 +10,18 @@ export function genFileExt(language: string): string {
return ext;
}

export function genFileName(node: IProblem, language: string): string {
const slug: string = kebabCase(node.name);
export function genFileSlug(node: IProblem, endpoint: Endpoint): string {
switch (endpoint) {
case Endpoint.LeetCodeCN:
return kebabCase(pinyin(node.name, { style: pinyin.STYLE_NORMAL }).join(""));
case Endpoint.LeetCode:
default:
return kebabCase(node.name);
}
}

export function genFileName(node: IProblem, language: string, endpoint: Endpoint): string {
const slug: string = genFileSlug(node, endpoint);
const ext: string = genFileExt(language);
return `${node.id}.${slug}.${ext}`;
}