|
|
|
@ -44,7 +44,6 @@ class Export extends Controller {
|
|
|
|
|
|
|
|
|
|
/* Archive directory */
|
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
|
console.log('zip -9jpr ' + path.resolve(temp_dir, '../archive.zip') + ' ' + path.resolve(temp_dir) + '/')
|
|
|
|
|
exec('cd ' + path.resolve(temp_dir) + '; zip -r archive.zip *', (error) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error(`exec error: ${error}`);
|
|
|
|
@ -93,8 +92,8 @@ class Export extends Controller {
|
|
|
|
|
const html_name = page.Name.replace(/\s/g, '_') + '.html'
|
|
|
|
|
mapping[page.UUID] = html_name
|
|
|
|
|
}
|
|
|
|
|
console.log(page0)
|
|
|
|
|
const sidebar_html = await this._generate_sidebar(mapping, page0) //generates the sidebar contents
|
|
|
|
|
|
|
|
|
|
const sidebar_html = await this._generate_sidebar(mapping, user) //generates the sidebar contents
|
|
|
|
|
|
|
|
|
|
const temp_dir = path.resolve(os.tmpdir(), uuid()) //make temporary directory
|
|
|
|
|
await fs.mkdir(temp_dir)
|
|
|
|
@ -102,16 +101,13 @@ class Export extends Controller {
|
|
|
|
|
for (const page of pages) {
|
|
|
|
|
const fs = require('fs').promises
|
|
|
|
|
const contents = (await fs.readFile(path.resolve(this.app.directories.assets, 'export-template.html'))).toString('utf8')
|
|
|
|
|
console.log(page)
|
|
|
|
|
//console.log({contents})
|
|
|
|
|
const content_html = await this._generate_html(page)
|
|
|
|
|
//console.log({content_html, sidebar_html})
|
|
|
|
|
const page_html = contents.replace('%%SIDEBAR%%', sidebar_html).replace('%%CONTENTS%%', content_html)
|
|
|
|
|
const page_html = contents.replace('%%CONTENTS%%', content_html).replace('%%SIDEBAR%%', sidebar_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) => {
|
|
|
|
|
exec('cd ' + path.resolve(temp_dir) + '; zip -r archive.zip *', (error) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error(`exec error: ${error}`);
|
|
|
|
|
return res.error(500);
|
|
|
|
@ -120,22 +116,32 @@ class Export extends Controller {
|
|
|
|
|
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
async _generate_sidebar(mapping, upper_node) { //generate sidebar from mapping array and upper_node
|
|
|
|
|
async _generate_sidebar(mapping, user) {// the absolutely dreadful method to create, sidebar
|
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
|
console.log(upper_node)
|
|
|
|
|
console.log(upper_node.ChildPageIds == 'undefined')
|
|
|
|
|
if (upper_node.ChildPageIds == 'undefined') {
|
|
|
|
|
return '<a class="nav-link" href="' + mapping[upper_node.UUID] + '">' + upper_node.Name + '</a>'
|
|
|
|
|
} else {
|
|
|
|
|
let base = '<nav class="nav nav-pills flex-column">'
|
|
|
|
|
for (const childrenId of upper_node.ChildPageIds) {
|
|
|
|
|
base += (await this._generate_sidebar(await Page.findOne({ UUID: childrenId })))
|
|
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
|
const virt_root = await user.get_root_page()
|
|
|
|
|
return await this._sidebar_recurse(mapping, virt_root)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
async _sidebar_recurse(mapping, parent) {
|
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
|
const children = await Page.find({ ParentId : parent.UUID})
|
|
|
|
|
let base = ''
|
|
|
|
|
for (const child of children) {
|
|
|
|
|
if (child.ChildPageIds.length === 0) {
|
|
|
|
|
base += '<a class="nav-link ml-2" href="' + mapping[child.UUID] + '">' + child.Name + '</a>' //mapping[child.UUID] gives filename
|
|
|
|
|
} else {
|
|
|
|
|
base += '<a class="nav-link ml-2" href="' + mapping[child.UUID] + '">' + child.Name + '</a>'
|
|
|
|
|
base += '<div class="nav nav-pills flex-column ml-2">'
|
|
|
|
|
base += await this._sidebar_recurse(mapping, child)
|
|
|
|
|
base += '</div>'
|
|
|
|
|
}
|
|
|
|
|
console.log({ base })
|
|
|
|
|
return base
|
|
|
|
|
}
|
|
|
|
|
return base
|
|
|
|
|
}
|
|
|
|
|
async _generate_html(page) { //generate html page from page and node data
|
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
|
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')
|
|
|
|
|