-
-
Save azu/2c9d872a91f9572b3717df35503dbcdb to your computer and use it in GitHub Desktop.
toc for mumemo memo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from "path"; | |
import globby from "globby"; | |
import groupby from "lodash.groupby"; | |
import { mdLink } from "markdown-function"; | |
// @ts-ignore | |
import updateREADME from "add-text-to-markdown"; | |
import * as fs from "fs"; | |
const rootDir = path.join(__dirname, "../../"); // ここ適当に!! | |
// raname dir | |
// directory paths | |
{ | |
const readmes = globby.sync([path.join(rootDir, "20*/**/README.md").split(path.sep).join("/")]); | |
readmes.forEach((readme) => { | |
const dir = path.dirname(readme); | |
const dirName = path.basename(dir); | |
const newDir = path.join(path.dirname(dir), dirName.normalize("NFC")); | |
if (dir !== newDir) { | |
console.log(`Dir: ${dir} → ${newDir}`); | |
fs.renameSync(dir, newDir); | |
} | |
}); | |
} | |
// update toc | |
const readmes = globby.sync([path.join(rootDir, "20*/**/README.md").split(path.sep).join("/")]); | |
const groupByYear = groupby(readmes, (obj) => { | |
return path.basename(path.dirname(path.dirname(obj))); | |
}); | |
let output = ""; | |
Object.entries(groupByYear) | |
.reverse() | |
.forEach(([key, values]) => { | |
const yearLink = mdLink({ | |
text: key, | |
url: `./${key}` | |
}); | |
output += `- ${yearLink}\n`; | |
values.forEach((value) => { | |
const relativePath = path.relative(rootDir, value); | |
const dirName = path.basename(path.dirname(value)); | |
output += ` - ${mdLink({ | |
text: dirName, | |
url: relativePath.split(" ").join("%20") | |
})}\n`; | |
}); | |
}); | |
const readme = fs.readFileSync(path.join(rootDir, "README.md"), "utf-8"); | |
// content, insertContent, sectionName, | |
const newREADME = updateREADME(readme, output, "Notes"); | |
fs.writeFileSync(path.join(rootDir, "README.md"), newREADME, "utf-8"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment