UNPKG

leetcode-tools

Version:
232 lines (226 loc) 8.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BuildContest = exports.BuildProblemSet = exports.Build = void 0; const leetcode_api_typescript_1 = require("leetcode-api-typescript"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const Constant = __importStar(require("./constant")); const interface_1 = require("./interface"); const utils_1 = require("./utils"); function Build(name) { if (name === "problems") { return BuildProblemSet; } return BuildContest; } exports.Build = Build; async function BuildProblemSet(src, dst, docsRelativePath) { const navList = []; const dirs = fs_1.default.readdirSync(src); dirs.sort((a, b) => { const _a = Number(a.split(".")[0]); const _b = Number(b.split(".")[0]); if (_a < _b) { return -1; } if (_a > _b) { return 1; } return 0; }); for (const dir of dirs) { buildProblemContent(path_1.default.join(src, dir), path_1.default.join(dst, dir)); navList.push({ [dir]: path_1.default.join(docsRelativePath, dir, "index.md") }); } return navList; } exports.BuildProblemSet = BuildProblemSet; async function BuildContest(src, dst, docsRelativePath) { const navList = []; const dirs = fs_1.default.readdirSync(src); dirs.sort((a, b) => { const _a = Number(a); const _b = Number(b); if (_a < _b) return 1; if (_a > _b) return -1; return 0; }); for (const dir of dirs) { buildContestContent(path_1.default.join(src, dir), path_1.default.join(dst, dir)); navList.push({ [dir]: path_1.default.join(docsRelativePath, dir, "index.md") }); } return navList; } exports.BuildContest = BuildContest; function buildProblemContent(src, dst) { const problemJsonFilePath = path_1.default.join(src, Constant.ProblemAssetsName, "problem.json"); let mdContent = "---\n"; if (fs_1.default.existsSync(problemJsonFilePath)) { const problemJsonContent = JSON.parse(fs_1.default.readFileSync(problemJsonFilePath).toString()); mdContent += makeTags(problemJsonContent); } mdContent += "\n---\n"; mdContent += `# ${src.split(path_1.default.sep).slice(-1)[0]}`; mdContent += makeContent(src, dst, 2); fs_1.default.writeFileSync(path_1.default.join(dst, "index.md"), mdContent); } function buildContestContent(src, dst) { let mdContent = `# ${src.split(path_1.default.sep).slice(-2).join("-")}`; const dirs = fs_1.default.readdirSync(src); dirs.sort(); for (const dir of dirs) { mdContent += ` ## ${String.fromCharCode("A".charCodeAt(0) + (dir.charCodeAt(0) - "a".charCodeAt(0)))} ${makeContent(path_1.default.join(src, dir), path_1.default.join(dst, dir), 3).replace(new RegExp(Constant.ProblemAssetsName, "g"), `${dir}/${Constant.ProblemAssetsName}`)} `; fs_1.default.writeFileSync(path_1.default.join(dst, "index.md"), mdContent); } } function makeContent(src, dst, tocBase) { const problemJsonFilePath = path_1.default.join(dst, Constant.ProblemAssetsName, "problem.json"); if (fs_1.default.existsSync(problemJsonFilePath)) { fs_1.default.rmSync(problemJsonFilePath); } let content = ""; content += "\n" + makeStatementContent(src, dst, tocBase); content += "\n" + makeTutorialAndSolutionContent(src, dst, tocBase); return content; } // eslint-disable-next-line @typescript-eslint/no-explicit-any function makeTags(problemJsonContent) { let content = "tags:\n"; // eslint-disable-next-line @typescript-eslint/no-explicit-any problemJsonContent?.tag.forEach((tag) => { content += ` - ${tag.name}\n`; }); return content; } function makeStatementContent(src, dst, tocBase) { const statementContent = (() => { let content = ""; for (const locale in interface_1.LocaleEnum) { const statementFileName = (0, interface_1.StatementFileName)(locale); const statementSrcPath = path_1.default.join(src, statementFileName); const statementDstPath = path_1.default.join(dst, statementFileName); if (fs_1.default.existsSync(statementSrcPath)) { const statementContent = (0, utils_1.EscapeStatement)(fs_1.default.readFileSync(statementSrcPath).toString()) .split("\n") .filter((s) => s.length > 0) .map((s) => " " + s) .join("\n"); content += ` === "${interface_1.LocaleText[locale]}" ${statementContent} `; fs_1.default.rmSync(statementDstPath); } } return content; })(); if (statementContent.length === 0) { return ""; } return ` ${"#".repeat(tocBase)} Statement ${statementContent} `; } function makeTutorialAndSolutionContent(src, dst, tocBase) { const solutionContentList = (() => { const solutionContentList = []; for (let i = 0;; i++) { let content = ""; const tutorialContent = (() => { let content = ""; for (const locale in interface_1.LocaleEnum) { const tutorialFileName = (0, interface_1.TutorialFileName)(locale, (0, utils_1.GetIdx)(i)); const tutorialFileSrcPath = path_1.default.join(src, tutorialFileName); const tutorialFileDstPath = path_1.default.join(dst, tutorialFileName); if (fs_1.default.existsSync(tutorialFileSrcPath)) { content += ` === "${interface_1.LocaleText[locale]}" --8<-- "${tutorialFileSrcPath}" `; fs_1.default.rmSync(tutorialFileDstPath); } } return content; })(); if (tutorialContent.length !== 0) { content += ` ${"#".repeat(tocBase)} Tutorial${(0, utils_1.GetIdx)(i)} ${tutorialContent} `; } const solutionContent = (() => { let content = ""; for (const langSlug in leetcode_api_typescript_1.LangSlug) { const langExt = (0, leetcode_api_typescript_1.LangExt)(langSlug); const solutionFileName = (0, interface_1.SolutionFileName)(langSlug, (0, utils_1.GetIdx)(i)); const solutionSrcPath = path_1.default.join(src, solutionFileName); const solutionDstPath = path_1.default.join(dst, solutionFileName); if (fs_1.default.existsSync(solutionSrcPath)) { const codeContent = fs_1.default .readFileSync(solutionSrcPath) .toString() .split("\n") .map((s) => " " + s) .join("\n"); content += ` === "${leetcode_api_typescript_1.LangText[langSlug]}" \`\`\`${langExt} ${codeContent} \`\`\` `; fs_1.default.rmSync(solutionDstPath); } } return content; })(); if (solutionContent.length !== 0) { content += ` ${"#".repeat(tocBase)} Solution${(0, utils_1.GetIdx)(i)} ${solutionContent} `; } if (content.length === 0) { break; } solutionContentList.push(content); } return solutionContentList; })(); if (solutionContentList.length === 0) { return ""; } return solutionContentList.join("\n"); } //# sourceMappingURL=mkdocs.js.map