Skip to content

Instantly share code, notes, and snippets.

@azu

azu/auto-toc.ts Secret

Created May 6, 2021
Embed
What would you like to do?
toc for mumemo memo
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