Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Last active September 24, 2019 03:41
Show Gist options
  • Save joaofnds/5566547a3362e44ba5c9845324a17a27 to your computer and use it in GitHub Desktop.
Save joaofnds/5566547a3362e44ba5c9845324a17a27 to your computer and use it in GitHub Desktop.
Export notable files to a filetree structure using the first encountered tag
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