Skip to content

Instantly share code, notes, and snippets.

@AnandChowdhary
Created November 14, 2020 16:33
Show Gist options
  • Save AnandChowdhary/8cea4cdd63956e142e1794a2160ce7af to your computer and use it in GitHub Desktop.
Save AnandChowdhary/8cea4cdd63956e142e1794a2160ce7af to your computer and use it in GitHub Desktop.
const { readdir, readFile, writeFile } = require("fs-extra");
const { execSync } = require("child_process");
const { join } = require("path");
const slugify = require("@sindresorhus/slugify");
const dayjs = require("dayjs");
const frontMatter = require("front-matter");
const run = async () => {
const files = (await readdir(join("..", "exported"))).filter((file) =>
file.endsWith(".md")
);
for await (const file of files) {
const contents = await readFile(join("..", "exported", file), "utf8");
const data = frontMatter(contents);
const date = dayjs(date).format("ddd MMM DD HH:mm:ss YYYY ZZ");
const title = data.attributes.title;
const body = data.body;
const slug = title
? slugify(title)
: slugify(body.split(" ").slice(0, 3).join(" "));
console.log(date, slug);
await writeFile(join(".", `${slug}.md`), body.trim());
execSync(
`git add . && GIT_AUTHOR_DATE="${date}" GIT_COMMITTER_DATE="${date}" git commit -m ":pencil: Add ${
title ?? "note"
}"`,
{ stdio: "inherit" }
);
}
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment