fipamo/brain/data/Book.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-11-23 09:13:53 +01:00
import fh from 'filehound';
import fs from 'fs-extra';
import metadataParser from 'markdown-yaml-metadata-parser';
import _ from 'lodash';
2019-11-23 09:13:53 +01:00
export default class Pages {
//--------------------------
// constructor
//--------------------------
constructor() {}
//--------------------------
// methods
//--------------------------
start() {}
getPage(id) {
2019-11-23 09:13:53 +01:00
return new Promise((resolve, reject) => {
fh.create()
.paths('content/pages')
.ext('md')
.find()
.then(files => {
let pages = [];
for (let index = 0; index < files.length; index++) {
fs.readFile(files[index], { encoding: 'utf8' }, (err, file) => {
pages.push(metadataParser(file));
});
}
2019-11-23 09:13:53 +01:00
if (id === null || id === null || id === undefined) {
setTimeout(() => {
//TODO: Duct tape solution until something better created
resolve(pages);
}, 100);
} else {
setTimeout(() => {
//TODO: Duct tape solution until something better created
let page = _.find(pages, list => {
return list.metadata.uuid === id;
});
resolve(page);
}, 100);
2019-11-23 09:13:53 +01:00
}
})
.catch(err => {
reject(err);
});
});
}
//--------------------------
// event handlers
//--------------------------
}