Last active
September 24, 2019 03:41
-
-
Save joaofnds/5566547a3362e44ba5c9845324a17a27 to your computer and use it in GitHub Desktop.
Export notable files to a filetree structure using the first encountered tag
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 fs = require("fs"); | |
const path = require("path"); | |
const { promisify } = require("util"); | |
const matter = require("gray-matter"); // required since notable use it to encode | |
const glob = promisify(require("glob")); | |
const mkdir = promisify(fs.mkdir); | |
const readFile = promisify(fs.readFile); | |
const writeFile = promisify(fs.writeFile); | |
(async () => { | |
const files = await glob("./notes/**/*.md"); | |
for (const filename of files) { | |
const fileString = await readFile(filename, { encoding: "utf8" }); | |
const { | |
content, | |
data: { title, tags: [tag] = ["Untagged"] } | |
} = matter(fileString); | |
const dirname = path.join("export", tag); | |
const newPath = path.join(dirname, path.basename(filename)); | |
await mkdir(dirname, { recursive: true }); | |
await writeFile(newPath, content, "utf8"); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment