Skip to content

Instantly share code, notes, and snippets.

@FOLLGAD
Last active August 29, 2023 14:56
Show Gist options
  • Select an option

  • Save FOLLGAD/10f8386339c04b08bbbfea7e62f09e76 to your computer and use it in GitHub Desktop.

Select an option

Save FOLLGAD/10f8386339c04b08bbbfea7e62f09e76 to your computer and use it in GitHub Desktop.
Convert PureWriter backup to Markdown files
{
"name": "pw-conv",
"version": "1.0.0",
"description": "",
"main": "purewriter-to-md.js",
"type": "module",
"scripts": {},
"author": "FOLLGAD",
"license": "ISC",
"dependencies": {
"sqlite": "^4.0.23",
"sqlite3": "^5.0.2"
}
}
import fs from "fs"
import path from "path"
import sqlite3 from "sqlite3"
import { open } from "sqlite"
// Directory where output files will appear
const outDir = "/home/ubuntu/pure-writer"
// Path to your PureWriter .db file
const databasePath = "/home/ubuntu/Downloads/PureWriterBackup-xxx/PureWriterBackup-xxx.db"
open({
filename: databasePath,
driver: sqlite3.Database,
}).then(async (db) => {
fs.mkdirSync(filepath, { recursive: true })
let [arts, folds] = await Promise.all([
db.all("SELECT * FROM article"),
db.all("SELECT * FROM folder"),
])
// set folder to folder name
arts.map((a) => (a.folder = folds.find((f) => f.id === a.folderId)?.name))
for (let art of arts) {
const filename = art.id + ".md"
let stream = fs.createWriteStream(
path.resolve(outDir, filename),
{ flags: "w" }
)
// Add info as front matter
stream.write("---\n")
let fms = ["title", "updateTime", "createTime", "folder", "rank"]
fms.forEach((f) =>
stream.write(`${f}: ${f.includes("Time") ? new Date(art[f]).toISOString() : art[f]}\n`)
)
stream.write("---\n\n")
stream.write(art.content)
stream.close()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment