Task #27 - Defined functions for creating directories for exporting as markdown
This commit is contained in:
parent
617a77b809
commit
270da67977
@ -1,4 +1,8 @@
|
||||
const Controller = require('libflitter/controller/Controller')
|
||||
const os = require('os');
|
||||
const fs = require('fs').promises;
|
||||
const uuid = require('uuid/v4');
|
||||
const path = require('path');
|
||||
|
||||
/*
|
||||
* Export Controller
|
||||
@ -12,22 +16,55 @@ class Export extends Controller {
|
||||
/*
|
||||
* Serve the main page.
|
||||
*/
|
||||
async json_export(req, res){
|
||||
async json_export(req, res) {
|
||||
const Page = this.models.get('api:Page')
|
||||
const user = req.user
|
||||
const pages = await Page.find({OrgUserId:user.id})
|
||||
const Node = this.models.get('api:Node')
|
||||
const user = req.user
|
||||
const pages = await Page.find({ OrgUserId: user._id })
|
||||
|
||||
const ExportData = {}
|
||||
for (const page of pages ) {
|
||||
const nodes = await Node.find({PageId:page.UUID})
|
||||
for (const page of pages) {
|
||||
const nodes = await Node.find({ PageId: page.UUID })
|
||||
|
||||
const PageData = {...page}
|
||||
PageData.Nodes = nodes.map( node => { return {...node} } )
|
||||
const PageData = { ...page }
|
||||
PageData.Nodes = nodes.map(node => { return { ...node } })
|
||||
ExportData[page.Name] = PageData
|
||||
}
|
||||
return res.api(ExportData)
|
||||
}
|
||||
async markdown_export(req, res) {
|
||||
const Page = this.models.get('api:Page')
|
||||
const Node = this.models.get('api:Node')
|
||||
const user = req.user
|
||||
const page0 = await Page.findOne({ OrgUserId: user._id, ParentId: '0' }) //Imaginary root
|
||||
const temp_dir = path.resolve(os.tmpdir(), uuid())
|
||||
|
||||
await fs.mkdir(temp_dir)
|
||||
this._markdown_recurse(page0, temp_dir)
|
||||
return res.api({temp_dir})
|
||||
}
|
||||
|
||||
async _markdown_recurse(root, temp_dir) {
|
||||
console.log('root', root)
|
||||
const Page = this.models.get('api:Page')
|
||||
//const child = await root.childPages
|
||||
const child = await Page.find({ UUID: { $in: root.ChildPageIds }})
|
||||
console.log('child', child)
|
||||
for (const page of child) {
|
||||
const markdown = this._generate_markdown(page)
|
||||
if (page.ChildPageIds && page.ChildPageIds.length) {
|
||||
const sub_temp_dir = path.resolve(temp_dir, page.Name.replace(/\s/g, '_'))
|
||||
console.log('page', page, 'sub_temp', sub_temp_dir)
|
||||
await fs.mkdir(sub_temp_dir)
|
||||
this._markdown_recurse(page, sub_temp_dir)
|
||||
}
|
||||
}
|
||||
}
|
||||
async _generate_markdown(page) {
|
||||
const Node = this.models.get('api:Node')
|
||||
const nodes = await Node.find({ PageId: page.UUID })
|
||||
return nodes.map(x => { return x.Value.Value }).join('\n')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = Export
|
@ -46,7 +46,8 @@ const index = {
|
||||
// your own route protected by 'middleware::auth:UserOnly'
|
||||
'/dash': [ 'controller::Home.welcome' ],
|
||||
'/login': [ 'middleware::auth:GuestOnly', 'controller::Home.get_login' ],
|
||||
'/test-export' : ['controller::Export.json_export' ],
|
||||
'/test-json' : [ 'controller::Export.json_export' ],
|
||||
'/test-markdown' : [ 'controller::Export.markdown_export' ]
|
||||
},
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user