Created
November 14, 2020 16:33
-
-
Save AnandChowdhary/8cea4cdd63956e142e1794a2160ce7af to your computer and use it in GitHub Desktop.
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
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