diff --git a/app/controllers/Export.controller.js b/app/controllers/Export.controller.js index 28b369e..db50292 100644 --- a/app/controllers/Export.controller.js +++ b/app/controllers/Export.controller.js @@ -79,6 +79,47 @@ class Export extends Controller { return x.Value.Value }).join('\n') } + async html_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._html_recurse(page0, temp_dir) + + const exec = require('child_process').exec; + exec('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => { + if (error) { + console.error(`exec error: ${error}`); + return res.error(500); + return; + } + + return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`) + }) + } + async _html_recurse(root, temp_dir) { //recursive function for creating directory and generating html pages + 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 } }) + + for (const page of child) { + const html = this._generate_html(page) + if (page.ChildPageIds && page.ChildPageIds.length) { + const sub_temp_dir = path.resolve(temp_dir, page.Name.replace(/\s/g, '_')) + await fs.mkdir(sub_temp_dir) + this._html_recurse(page, sub_temp_dir) + } + } + } + async _generate_html(page) { //generate html + const Node = this.models.get('api:Node') + const nodes = await Node.find({ PageId: page.UUID }) + return nodes.map(x => {return x.to_html()}).join('\n') + } } module.exports = exports = Export \ No newline at end of file diff --git a/app/models/api/Node.model.js b/app/models/api/Node.model.js index 5d374ee..47fd9d0 100644 --- a/app/models/api/Node.model.js +++ b/app/models/api/Node.model.js @@ -30,6 +30,25 @@ class Node extends Model { return this.belongs_to_one(Page, "PageId", "_id") } + to_html() { + switch(this.Type){ + case 'paragraph': + return '

' + this.Value.Value + '

' + case 'header1': + return '

' + this.Value.Value + '

' + case 'header2': + return '

' + this.Value.Value + '

' + case 'header3': + return '

' + this.Value.Value + '

' + case 'header4': + return '

' + this.Value.Value + '

' + case 'block_code': + return '
' + this.Value.Value + '
' + case 'click_link': + return '' + this.Value.Value + '' + } + } + update_from_raw(data) { if ( data.Type ) this.Type = data.Type if ( data.Value ) this.Value = data.Value diff --git a/app/routing/routers/index.routes.js b/app/routing/routers/index.routes.js index 60a0dea..308976c 100644 --- a/app/routing/routers/index.routes.js +++ b/app/routing/routers/index.routes.js @@ -47,7 +47,8 @@ const index = { '/dash': [ 'controller::Home.welcome' ], '/login': [ 'middleware::auth:GuestOnly', 'controller::Home.get_login' ], '/test-json' : [ 'controller::Export.json_export' ], - '/test-markdown' : [ 'controller::Export.markdown_export' ] + '/test-markdown' : [ 'controller::Export.markdown_export' ], + '/test-html' : [ 'controller::Export.html_export' ] }, /*