quiver-to-obsidian
Version:
Convert quiver libraray to obsidian library
190 lines • 6.72 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import fse from 'fs-extra';
import * as path from 'path';
// read note
const isQvNote = (notePath) => __awaiter(void 0, void 0, void 0, function* () {
try {
const stat = yield fse.stat(notePath);
if (stat.isDirectory() && notePath.endsWith('.qvnote')) {
return true;
}
return false;
}
catch (error) {
return false;
}
});
const readNoteMeta = (metaPath) => __awaiter(void 0, void 0, void 0, function* () {
try {
const stat = yield fse.stat(metaPath);
if (stat.isFile()) {
const content = yield fse.readFile(metaPath);
return JSON.parse(content.toString());
}
throw new Error(`no such file ${metaPath}`);
}
catch (error) {
throw error;
}
});
export const readNoteContent = (contentPath) => __awaiter(void 0, void 0, void 0, function* () {
try {
const stat = yield fse.stat(contentPath);
if (stat.isFile()) {
const content = yield fse.readFile(contentPath);
return JSON.parse(content.toString());
}
throw new Error(`no such file ${contentPath}`);
}
catch (error) {
throw error;
}
});
const readNoteResources = (resourcesPath) => __awaiter(void 0, void 0, void 0, function* () {
if (!(yield fse.pathExists(resourcesPath))) {
return undefined;
}
try {
const stat = yield fse.stat(resourcesPath);
if (stat.isDirectory()) {
const names = yield fse.readdir(resourcesPath);
const resources = [];
names.forEach((name) => {
resources.push({ name });
});
return resources;
}
throw new Error(`no such directory ${resourcesPath}`);
}
catch (error) {
throw error;
}
});
const readNote = (notePath) => __awaiter(void 0, void 0, void 0, function* () {
if (!isQvNote(notePath)) {
throw new Error(`${notePath} is not a quiver note dir, please check and try again`);
}
const meta = yield readNoteMeta(path.join(notePath, 'meta.json'));
// delay read
const contentPath = path.join(notePath, 'content.json');
if (!fse.existsSync(contentPath)) {
throw new Error(`no such file ${contentPath}`);
}
const resourceFiles = yield readNoteResources(path.join(notePath, 'resources'));
const note = {
meta,
notePath,
contentPath,
};
if (resourceFiles) {
note.resources = { files: resourceFiles };
}
return note;
});
// read notebook
const isQvNoteBook = (notebookPath) => __awaiter(void 0, void 0, void 0, function* () {
try {
const stat = yield fse.stat(notebookPath);
if (stat.isDirectory() && notebookPath.endsWith('.qvnotebook')) {
return true;
}
return false;
}
catch (error) {
return false;
}
});
const readNotebookMeta = (metaPath) => __awaiter(void 0, void 0, void 0, function* () {
const content = yield fse.readFile(metaPath);
return JSON.parse(content.toString());
});
const readNoteBook = (notebookPath) => __awaiter(void 0, void 0, void 0, function* () {
if (!(yield isQvNoteBook(notebookPath))) {
throw new Error(`${notebookPath} is not a quiver notebook dir, please check and try again`);
}
const names = yield fse.readdir(notebookPath);
let meta;
const notes = [];
yield Promise.all(names.map((name) => __awaiter(void 0, void 0, void 0, function* () {
const filePath = path.join(notebookPath, name);
const stat = fse.statSync(filePath);
if (stat.isFile() && name === 'meta.json') {
// read library meta
meta = yield readNotebookMeta(filePath);
}
else if (stat.isDirectory()) {
// read notebook
notes.push(yield readNote(filePath));
}
})));
if (!meta) {
throw new Error(`no such file ${path.join(notebookPath, 'meta.json')}`);
}
return {
meta,
notes,
};
});
// read library
const isQvLibrary = (libraryPath) => __awaiter(void 0, void 0, void 0, function* () {
try {
const stat = fse.statSync(libraryPath);
if (stat.isDirectory() && libraryPath.endsWith('.qvlibrary')) {
return true;
}
return false;
}
catch (error) {
return false;
}
});
const readLibraryMeta = (metaPath) => __awaiter(void 0, void 0, void 0, function* () {
const content = yield fse.readFile(metaPath);
return JSON.parse(content.toString());
});
export const readLibrary = (libraryPath) => __awaiter(void 0, void 0, void 0, function* () {
if (!(yield isQvLibrary(libraryPath))) {
throw new Error(`${libraryPath} is not a quiver library dir, please check and try again`);
}
const names = yield fse.readdir(libraryPath);
let meta;
const notebooks = [];
yield Promise.all(names.map((name) => __awaiter(void 0, void 0, void 0, function* () {
const filePath = path.join(libraryPath, name);
const stat = yield fse.stat(filePath);
if (stat.isFile() && name === 'meta.json') {
// read library meta
meta = yield readLibraryMeta(filePath);
}
else if (stat.isDirectory()) {
// read notebook
notebooks.push(yield readNoteBook(filePath));
}
})));
if (!meta) {
throw new Error(`no such file ${path.join(libraryPath, 'meta.json')}`);
}
return {
meta,
notebooks,
};
});
export function walkThroughNotebookHierarchty(libraryMeta, parents, callback) {
var _a;
callback(libraryMeta.uuid, parents);
if (libraryMeta.children && libraryMeta.children.length > 0) {
const p = [...parents, libraryMeta.uuid];
(_a = libraryMeta.children) === null || _a === void 0 ? void 0 : _a.forEach((meta) => {
walkThroughNotebookHierarchty(meta, p, callback);
});
}
}
//# sourceMappingURL=quiver_parse.js.map