diff --git a/app/assets/exporttemplate.html b/app/assets/export-template.html similarity index 81% rename from app/assets/exporttemplate.html rename to app/assets/export-template.html index 7f968dc..4ce3908 100644 --- a/app/assets/exporttemplate.html +++ b/app/assets/export-template.html @@ -42,6 +42,10 @@ background-color: beige; } + .navsec a:hover { + color: rgb(207, 169, 43); + } + .nav-link { color: beige; } @@ -53,13 +57,9 @@
- \ No newline at end of file diff --git a/app/controllers/Export.controller.js b/app/controllers/Export.controller.js index db50292..b2bf1ef 100644 --- a/app/controllers/Export.controller.js +++ b/app/controllers/Export.controller.js @@ -36,12 +36,13 @@ class Export extends Controller { 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 page0 = await user.get_root_page() //topmost page const temp_dir = path.resolve(os.tmpdir(), uuid()) await fs.mkdir(temp_dir) this._markdown_recurse(page0, temp_dir) - + + /* Archive directory */ const exec = require('child_process').exec; exec('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => { if (error) { @@ -57,7 +58,6 @@ class Export extends Controller { async _markdown_recurse(root, temp_dir) { //recursive function for creating directory and generating markdown 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) { @@ -83,12 +83,28 @@ class Export extends Controller { 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()) + const pages = await Page.find({ OrgUserId: user._id }) + const mapping = {} //keeps track of html file UUID and file name + const page0 = await user.get_root_page() //topmost page + for (const page of pages) { + const html_name = page.Name.replace(/\s/g, '_') + '.html' + mapping[page.UUID] = html_name + } + const sidebar_html = this._generate_sidebar(page0) //generates the sidebar contents + + const temp_dir = path.resolve(os.tmpdir(), uuid()) //make temporary directory await fs.mkdir(temp_dir) - this._html_recurse(page0, temp_dir) + /* Generates html files from template */ + for(const page of pages) { + const fs = require('fs').promises + const contents = await fs.readFile('../assets/export-template.html') + const content_html = this._generate_html(page) + const page_html = contents.replace('%%SIDEBAR%%', sidebar_html).replace('%%CONTENTS%%', content_html) + await fs.writeFile(path.resolve(temp_dir, mapping[page.UUID]), page_html) + } + /* Archive directory */ const exec = require('child_process').exec; exec('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => { if (error) { @@ -100,25 +116,21 @@ class Export extends Controller { 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_sidebar(root) { //generate sidebar from mapping array and root + if (root.ChildPageIds < 1) { + return '' + root.Name + '' + } else { + let base = '