Last active
July 20, 2017 14:35
-
-
Save snakazawa/adff036c217a573153d579e07385989f to your computer and use it in GitHub Desktop.
Exporting PileMd directory to pure directory and markdown files (unofficially).
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'); | |
let _id = 0; | |
const usage = ` Usage: | |
node ${process.argv[1]} <src> <dist> | |
Example: | |
node ${process.argv[1]} . dist | |
`; | |
const args = [...process.argv].slice(2); | |
if (args.length < 2) { | |
console.error(usage); | |
process.exit(1); | |
} | |
if (['help', '-h', '--help'].includes(args[0])) { | |
console.log(usage); | |
process.exit(0); | |
} | |
const [src, dist] = args; | |
// check source folders | |
['', 'folders', 'notes', 'racks'].forEach(suffix => { | |
const srcPath = path.join(src, suffix); | |
if (!fs.existsSync(srcPath)) { | |
console.error('<src> should be PileMd directory.'); | |
process.exit(1); | |
} | |
}); | |
// make dist folder | |
mkdirIfNotExists(dist); | |
// read and export racks | |
const rackDistPaths = {}; | |
fs.readdirSync(path.join(src, 'racks')).forEach(suffix => { | |
const srcPath = path.join(src, 'racks', suffix); | |
const data = JSON.parse(fs.readFileSync(srcPath, 'utf8')); | |
const distPath = path.join(dist, data.name); | |
mkdirIfNotExists(distPath); | |
rackDistPaths[data.uid] = distPath; | |
}); | |
// read and export folders | |
const folderDistPaths = {}; // uid: distPath | |
fs.readdirSync(path.join(src, 'folders')).forEach(suffix => { | |
const srcPath = path.join(src, 'folders', suffix); | |
const data = JSON.parse(fs.readFileSync(srcPath, 'utf8')); | |
const rackDistPath = rackDistPaths[data.rackUid]; | |
const distPath = path.join(rackDistPath, data.name); | |
mkdirIfNotExists(distPath); | |
folderDistPaths[data.uid] = distPath; | |
}); | |
// read and export notes | |
fs.readdirSync(path.join(src, 'notes')).forEach(suffix => { | |
const srcPath = path.join(src, 'notes', suffix); | |
const data = JSON.parse(fs.readFileSync(srcPath, 'utf8')); | |
const distDir = data.folderUid ? folderDistPaths[data.folderUid] : dist; | |
const title = findTitle(data.body) || createId(); | |
const distPath = path.join(distDir, title + '.md'); | |
console.log(`create ${distPath}`); | |
fs.writeFileSync(distPath, data.body, 'utf-8'); | |
}); | |
// util functions | |
function mkdirIfNotExists (p) { | |
if (!fs.existsSync(p)) { | |
console.log(`mkdir ${p}`); | |
fs.mkdirSync(p); | |
} | |
} | |
function findTitle (body) { | |
const header = body.split('\n')[0]; | |
if (header.startsWith('# ')) { | |
return header.slice(2).replace(/[\\/:,;*?"<>|]/g, '_'); | |
} else { | |
return null; | |
} | |
} | |
function createId () { | |
_id += 1; | |
return _id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment