feat: TOC, scroll to header, page UI improvements

This commit is contained in:
Nicolas Giard
2018-11-17 23:03:58 -05:00
parent e067fe3abd
commit 3abc254685
15 changed files with 1054 additions and 785 deletions

View File

@@ -1,6 +1,7 @@
require('../core/worker')
const _ = require('lodash')
const cheerio = require('cheerio')
/* global WIKI */
@@ -21,15 +22,43 @@ module.exports = async (job) => {
})
}
// 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)
const leafPath = _.reduce(_.times(depth), (curPath, curIdx) => {
if (_.has(toc, curPath)) {
const lastLeafIdx = _.get(toc, curPath).length - 1
curPath = `${curPath}[${lastLeafIdx}].children`
}
return curPath
}, 'root')
const leafSlug = $('.toc-anchor', el).first().attr('href')
$('.toc-anchor', el).remove()
_.get(toc, leafPath).push({
title: _.trim($(el).text()),
anchor: leafSlug,
children: []
})
})
// Save to DB
await WIKI.models.pages.query()
.patch({ render: output })
.patch({
render: output,
toc: JSON.stringify(toc.root)
})
.where('id', job.data.page.id)
// Save to cache
await WIKI.models.pages.savePageToCache({
...job.data.page,
render: output
render: output,
toc: JSON.stringify(toc.root)
})
WIKI.logger.info(`Rendering page ${job.data.page.path}: [ COMPLETED ]`)