Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Last active October 13, 2018 01:42
Show Gist options
  • Select an option

  • Save joaofnds/964020c2988c0caaef123aadd2b45c46 to your computer and use it in GitHub Desktop.

Select an option

Save joaofnds/964020c2988c0caaef123aadd2b45c46 to your computer and use it in GitHub Desktop.
Boostnote export
const fs = require("fs");
const path = require("path");
const cson = require("cson");
const { folders } = require("./boostnote.json");
const DIRECTORIES = {
IMPORT: "notes",
EXPORT: "export",
NOTES: "notes",
SNIPPETS: "snippets"
};
const NOTE_TYPES = {
MARKDOWN: "MARKDOWN_NOTE",
SNIPPET: "SNIPPET_NOTE"
};
function* walkSync(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const pathToFile = path.join(dir, file);
const isDirectory = fs.statSync(pathToFile).isDirectory();
if (isDirectory) {
yield* walkSync(pathToFile);
} else {
yield pathToFile;
}
}
}
function getFolderNameByKey(key) {
const folder = folders.filter(f => f.key === key)[0] || {};
return folder.name || key;
}
function sanitizeFilepath(filepath) {
return filepath.replace(/[^a-z0-9.\s]/gi, "_").toLowerCase();
}
function createNestedDirs(dir, nextDir, ...nestedDirs) {
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
if (nextDir) {
const newDir = path.join(dir, nextDir);
createNestedDirs(newDir, ...nestedDirs);
}
}
function writeFile(filepath, contents) {
const fileDir = path.dirname(filepath);
createNestedDirs(...fileDir.split("/"));
fs.writeFileSync(filepath, contents, { encoding: "utf8" });
}
function writeNote(note) {
const exportPath = path.join(
DIRECTORIES.EXPORT,
DIRECTORIES.NOTES,
getFolderNameByKey(note.folder),
`${sanitizeFilepath(note.title)}.md`
);
writeFile(exportPath, note.content);
}
function writeSnippet(note) {
const snippets = note.snippets
.map(snip => {
if (snip.name.length > 0) {
snip.content = `# ${snip.name}\n\n${snip.content}`;
}
return snip.content;
})
.join("\n\n\n");
const exportPath = path.join(
DIRECTORIES.EXPORT,
DIRECTORIES.SNIPPETS,
getFolderNameByKey(note.folder),
`${sanitizeFilepath(note.title)}.md`
);
writeFile(exportPath, snippets);
}
for (const filepath of walkSync(DIRECTORIES.IMPORT)) {
const note = cson.parseCSONFile(filepath);
switch (note.type) {
case NOTE_TYPES.MARKDOWN:
writeNote(note);
break;
case NOTE_TYPES.SNIPPET:
writeSnippet(note);
break;
default:
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment