Created
August 14, 2020 09:47
-
-
Save goofmint/fdf9e9dd1c9f83a291b8c01b4729937e to your computer and use it in GitHub Desktop.
Move contents from Notable to Boostnote
This file contains hidden or 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 { promisify } = require('util'); | |
const metadataParser = require('markdown-yaml-metadata-parser'); | |
const baseDir = '/path/to/Notable/notes'; | |
const outputDir = '/path/to/Boostnote'; | |
(async () => { | |
const files = await promisify(fs.readdir)(baseDir); | |
const fileList = files.filter((file) => /.*\.md$/.test(file) && fs.statSync(`${baseDir}/${file}`).isFile()); | |
const tags = new Set(); | |
for (let file of fileList) { | |
const filePath = `${baseDir}/${file}`; | |
const fileContent = await promisify(fs.readFile)(filePath, 'utf-8'); | |
const result = metadataParser(fileContent); | |
let title = ''; | |
if (result.metadata.title) { | |
title = result.metadata.title; | |
} else if (m = result.content.match(/^# (.*)$/m, "$1")) { | |
title = m[0].replace('# ', ''); | |
} else { | |
try { | |
title = result.content.split(/\r\n|\r|\n/).filter(l => l !== '')[0].replace(/#+ /, ''); | |
} catch (e) { | |
continue; | |
} | |
} | |
if (title === '') { | |
title = '(change me)'; | |
} | |
const id = r(); | |
const params = { | |
_id: `note:${id}`, | |
title: title, | |
content: result.content, | |
tags: result.metadata.tags || [], | |
folderPathname: '/', | |
data: {}, | |
createdAt: result.metadata.created || (new Date).toISOString(), | |
updatedAt: result.metadata.modified || (new Date).toISOString(), | |
trashed:false, | |
_rev: r() | |
}; | |
if (params.tags) { | |
params.tags.forEach(v => tags.add(v)); | |
} | |
const writePath = `${outputDir}/notes/${id}.json`; | |
await promisify(fs.writeFile)(writePath, JSON.stringify(params), 'utf-8'); | |
} | |
const configPath = `${outputDir}/boostnote.json`; | |
const fileContent = await promisify(fs.readFile)(configPath, 'utf-8'); | |
const json = JSON.parse(fileContent); | |
for (let v of tags) { | |
json.tagMap[v] = { | |
_id: `tag:${v}`, | |
createdAt: (new Date).toISOString(), | |
data: {}, | |
updatedAt: (new Date).toISOString() | |
} | |
} | |
await promisify(fs.writeFile)(configPath, JSON.stringify(json), 'utf-8'); | |
})(); | |
const r = () => { | |
return [1, 2].map(() => Math.random().toString(36).slice(-5)).join('_') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment