()
| 35 | main() |
| 36 | |
| 37 | async function main() { |
| 38 | const englishCategoryIndices = getEnglishCategoryIndices() |
| 39 | |
| 40 | for (const categoryIndex of englishCategoryIndices) { |
| 41 | const contents = fs.readFileSync(categoryIndex, 'utf8') |
| 42 | const { data, content } = frontmatter(contents) |
| 43 | |
| 44 | // Get the parent directory name |
| 45 | const categoryDirPath = path.dirname(categoryIndex) |
| 46 | const categoryDirName = path.basename(categoryDirPath) |
| 47 | |
| 48 | const title = await renderContent(data.title, {}, { textOnly: true }) |
| 49 | slugger.reset() |
| 50 | const expectedSlug = slugger.slug(decode(title)) |
| 51 | |
| 52 | // If the directory name already matches the expected slug, bail out now |
| 53 | if (categoryDirName === expectedSlug) continue |
| 54 | |
| 55 | // Figure out the new path for the category |
| 56 | const categoryDirParentDir = path.dirname(categoryDirPath) |
| 57 | const newPath = path.join(categoryDirParentDir, expectedSlug) |
| 58 | |
| 59 | // Figure out redirect path |
| 60 | const relativeOldPath = path.relative(contentDir, categoryDirPath) |
| 61 | const redirectPath = '/' + slash(relativeOldPath) |
| 62 | |
| 63 | // Log it |
| 64 | const relativeNewPath = path.relative(contentDir, newPath) |
| 65 | console.log(`Renaming category directory: |
| 66 | Old: "${relativeOldPath}" |
| 67 | New: "${relativeNewPath}" |
| 68 | Redirect: "${redirectPath}" |
| 69 | `) |
| 70 | |
| 71 | // Add a new redirect to the frontmatter |
| 72 | if (!data.redirect_from) { |
| 73 | data.redirect_from = [] |
| 74 | } |
| 75 | data.redirect_from.push(redirectPath) |
| 76 | |
| 77 | // Update the category index file on disk |
| 78 | fs.writeFileSync(categoryIndex, frontmatter.stringify(content, data, { lineWidth: 10000 })) |
| 79 | |
| 80 | // Update all of the category's articles on disk as well to add a new redirect to their frontmatter |
| 81 | for (const articleFileName of fs.readdirSync(categoryDirPath)) { |
| 82 | const articlePath = path.join(categoryDirPath, articleFileName) |
| 83 | |
| 84 | // Figure out redirect path |
| 85 | const articlePathMinusExtension = path.join( |
| 86 | categoryDirPath, |
| 87 | path.basename(articleFileName, '.md') |
| 88 | ) |
| 89 | const redirectArticlePath = '/' + slash(path.relative(contentDir, articlePathMinusExtension)) |
| 90 | |
| 91 | // Log it |
| 92 | const relativeOldArticlePath = path.relative(contentDir, articlePath) |
| 93 | const newArticlePath = path.join(categoryDirParentDir, expectedSlug, articleFileName) |
| 94 | const relativeNewArticlePath = path.relative(contentDir, newArticlePath) |
no test coverage detected