|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const chalk = require('chalk'); |
| 4 | + |
| 5 | +const url = require('./scripts/url'); |
| 6 | +const meta = require('./scripts/meta'); |
| 7 | +const getSlug = require('./scripts/slug'); |
| 8 | +const markdownRender = require('./scripts/markdownRender'); |
| 9 | +const listRender = require('./scripts/listRender'); |
| 10 | +const problemRender = require('./scripts/problemRender'); |
| 11 | + |
| 12 | +const problemList = []; |
| 13 | +const difficultyMap = {}; |
| 14 | +const tagMap = {}; |
| 15 | + |
| 16 | +function buildProblems () { |
| 17 | + const pathReg = /^\d{3}-\d{3}$/; |
| 18 | + const fileReg = /\.md$/; |
| 19 | + fs.readdirSync(path.resolve(__dirname, '../')).forEach(floder => { |
| 20 | + if (!pathReg.test(floder)) return; |
| 21 | + fs.readdirSync(path.resolve(__dirname, '../', floder)).forEach(file => { |
| 22 | + if (!fileReg.test(file)) return; |
| 23 | + const markdown = fs.readFileSync(path.resolve(__dirname, '../', floder, file), 'utf-8'); |
| 24 | + const content = markdownRender(markdown); |
| 25 | + const id = meta.getId(markdown); |
| 26 | + const name = meta.getName(markdown); |
| 27 | + const slug = getSlug(name); |
| 28 | + const difficulty = { |
| 29 | + name: meta.getDifficulty(markdown), |
| 30 | + slug: '' |
| 31 | + }; |
| 32 | + const tags = meta.getRelatedTopics(markdown).map(name => { |
| 33 | + return { |
| 34 | + name, |
| 35 | + slug: getSlug(name) |
| 36 | + }; |
| 37 | + }); |
| 38 | + const similarQuestions = meta.getSimilarQuestions(markdown).map(name => { |
| 39 | + return { |
| 40 | + name, |
| 41 | + slug: getSlug(name) |
| 42 | + }; |
| 43 | + }); |
| 44 | + |
| 45 | + difficulty.slug = getSlug(difficulty.name); |
| 46 | + |
| 47 | + problemList.push({ |
| 48 | + id, |
| 49 | + name, |
| 50 | + slug, |
| 51 | + difficulty, |
| 52 | + tags |
| 53 | + }); |
| 54 | + if (!difficultyMap[difficulty.slug]) difficultyMap[difficulty.slug] = difficulty.name; |
| 55 | + tags.forEach(tag => { |
| 56 | + if (!tagMap[tag.slug]) tagMap[tag.slug] = tag.name; |
| 57 | + }); |
| 58 | + |
| 59 | + problemRender({ |
| 60 | + id, |
| 61 | + name, |
| 62 | + slug, |
| 63 | + difficulty, |
| 64 | + tags, |
| 65 | + similarQuestions, |
| 66 | + content, |
| 67 | + url |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function buildPages () { |
| 74 | + listRender( |
| 75 | + '', |
| 76 | + url, |
| 77 | + Object.keys(tagMap).map(t => ({ name: tagMap[t], slug: t })), |
| 78 | + Object.keys(difficultyMap).map(d => ({ name: difficultyMap[d], slug: d })), |
| 79 | + problemList, |
| 80 | + {}, |
| 81 | + {} |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function buildTags () { |
| 86 | + let list = null; |
| 87 | + Object.keys(tagMap).forEach(slug => { |
| 88 | + list = problemList.filter(item => { |
| 89 | + let res = false; |
| 90 | + item.tags.forEach(tag => { |
| 91 | + if (tag.slug === slug) res = true; |
| 92 | + }); |
| 93 | + return res; |
| 94 | + }); |
| 95 | + listRender( |
| 96 | + `./tag/${slug}`, |
| 97 | + url, |
| 98 | + Object.keys(tagMap).map(t => ({ name: tagMap[t], slug: t })), |
| 99 | + Object.keys(difficultyMap).map(d => ({ name: difficultyMap[d], slug: d })), |
| 100 | + list, |
| 101 | + { |
| 102 | + name: tagMap[slug], |
| 103 | + slug |
| 104 | + }, |
| 105 | + {} |
| 106 | + ); |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +function buildDifficulties () { |
| 111 | + let list = null; |
| 112 | + Object.keys(difficultyMap).forEach(slug => { |
| 113 | + list = problemList.filter(item => item.difficulty.slug === slug); |
| 114 | + listRender( |
| 115 | + `./difficulty/${slug}`, |
| 116 | + url, |
| 117 | + Object.keys(tagMap).map(t => ({ name: tagMap[t], slug: t })), |
| 118 | + Object.keys(difficultyMap).map(d => ({ name: difficultyMap[d], slug: d })), |
| 119 | + list, |
| 120 | + {}, |
| 121 | + { |
| 122 | + name: difficultyMap[slug], |
| 123 | + slug |
| 124 | + } |
| 125 | + ); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +buildProblems(); |
| 130 | +buildPages(); |
| 131 | +buildTags(); |
| 132 | +buildDifficulties(); |
| 133 | + |
| 134 | +console.log(`\n${chalk.blue('Done!')}\n`); |
0 commit comments