2018-09-10 00:33:10 +00:00
|
|
|
const _ = require('lodash')
|
2018-11-18 04:03:58 +00:00
|
|
|
const cheerio = require('cheerio')
|
2018-09-10 00:33:10 +00:00
|
|
|
|
2018-08-09 04:07:15 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
module.exports = async (pageId) => {
|
|
|
|
WIKI.logger.info(`Rendering page ID ${pageId}...`)
|
2018-08-09 04:07:15 +00:00
|
|
|
|
|
|
|
try {
|
2019-02-13 22:20:46 +00:00
|
|
|
WIKI.models = require('../core/db').init()
|
|
|
|
|
|
|
|
const page = await WIKI.models.pages.getPageFromDb(pageId)
|
|
|
|
if (!page) {
|
|
|
|
throw new Error('Invalid Page Id')
|
|
|
|
}
|
|
|
|
|
|
|
|
await WIKI.models.renderers.fetchDefinitions()
|
|
|
|
const pipeline = await WIKI.models.renderers.getRenderingPipeline(page.contentType)
|
|
|
|
|
|
|
|
let output = page.content
|
2019-06-26 02:48:23 +00:00
|
|
|
|
|
|
|
if (_.isEmpty(page.content)) {
|
|
|
|
await WIKI.models.knex.destroy()
|
|
|
|
WIKI.logger.warn(`Failed to render page ID ${pageId} because content was empty: [ FAILED ]`)
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
for (let core of pipeline) {
|
2018-09-10 00:33:10 +00:00
|
|
|
const renderer = require(`../modules/rendering/${_.kebabCase(core.key)}/renderer.js`)
|
|
|
|
output = await renderer.render.call({
|
|
|
|
config: core.config,
|
|
|
|
children: core.children,
|
2019-02-13 22:20:46 +00:00
|
|
|
page: page,
|
2018-09-10 00:33:10 +00:00
|
|
|
input: output
|
|
|
|
})
|
|
|
|
}
|
2018-09-16 00:58:23 +00:00
|
|
|
|
2018-11-18 04:03:58 +00:00
|
|
|
// Parse TOC
|
|
|
|
const $ = cheerio.load(output)
|
|
|
|
let isStrict = $('h1').length > 0 // <- Allows for documents using H2 as top level
|
|
|
|
let toc = { root: [] }
|
|
|
|
|
|
|
|
$('h1,h2,h3,h4,h5,h6').each((idx, el) => {
|
|
|
|
const depth = _.toSafeInteger(el.name.substring(1)) - (isStrict ? 1 : 2)
|
2018-11-18 21:38:00 +00:00
|
|
|
let leafPathError = false
|
|
|
|
|
2018-11-18 04:03:58 +00:00
|
|
|
const leafPath = _.reduce(_.times(depth), (curPath, curIdx) => {
|
|
|
|
if (_.has(toc, curPath)) {
|
|
|
|
const lastLeafIdx = _.get(toc, curPath).length - 1
|
2018-11-18 21:38:00 +00:00
|
|
|
if (lastLeafIdx >= 0) {
|
|
|
|
curPath = `${curPath}[${lastLeafIdx}].children`
|
|
|
|
} else {
|
|
|
|
leafPathError = true
|
|
|
|
}
|
2018-11-18 04:03:58 +00:00
|
|
|
}
|
|
|
|
return curPath
|
|
|
|
}, 'root')
|
|
|
|
|
2018-11-18 21:38:00 +00:00
|
|
|
if (leafPathError) { return }
|
|
|
|
|
2018-11-18 04:03:58 +00:00
|
|
|
const leafSlug = $('.toc-anchor', el).first().attr('href')
|
|
|
|
$('.toc-anchor', el).remove()
|
2018-11-18 21:38:00 +00:00
|
|
|
|
2018-11-18 04:03:58 +00:00
|
|
|
_.get(toc, leafPath).push({
|
|
|
|
title: _.trim($(el).text()),
|
|
|
|
anchor: leafSlug,
|
|
|
|
children: []
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-09-16 00:58:23 +00:00
|
|
|
// Save to DB
|
|
|
|
await WIKI.models.pages.query()
|
2018-11-18 04:03:58 +00:00
|
|
|
.patch({
|
|
|
|
render: output,
|
|
|
|
toc: JSON.stringify(toc.root)
|
|
|
|
})
|
2019-02-13 22:20:46 +00:00
|
|
|
.where('id', pageId)
|
2018-09-16 00:58:23 +00:00
|
|
|
|
|
|
|
// Save to cache
|
|
|
|
await WIKI.models.pages.savePageToCache({
|
2019-02-13 22:20:46 +00:00
|
|
|
...page,
|
2018-11-18 04:03:58 +00:00
|
|
|
render: output,
|
|
|
|
toc: JSON.stringify(toc.root)
|
2018-09-16 00:58:23 +00:00
|
|
|
})
|
2018-09-10 00:33:10 +00:00
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
await WIKI.models.knex.destroy()
|
|
|
|
|
|
|
|
WIKI.logger.info(`Rendering page ID ${pageId}: [ COMPLETED ]`)
|
2018-08-09 04:07:15 +00:00
|
|
|
} catch (err) {
|
2019-02-13 22:20:46 +00:00
|
|
|
WIKI.logger.error(`Rendering page ID ${pageId}: [ FAILED ]`)
|
2018-08-09 04:07:15 +00:00
|
|
|
WIKI.logger.error(err.message)
|
|
|
|
}
|
|
|
|
}
|