1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-25 16:19:44 +02:00

Show created pages in menu (#11)

* Show created pages in menu

* reverse children sorting
This commit is contained in:
Peter Savchenko 2018-10-19 21:08:24 +03:00 committed by GitHub
parent 730eff7995
commit f845a0d09f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 18 deletions

View file

@ -0,0 +1,33 @@
const Pages = require('../../controllers/pages');
const asyncMiddleware = require('../../utils/asyncMiddleware');
/**
* Process one-level pages list to parent-childrens list
* @param {Page[]} pages - list of all available pages
* @return {Page[]}
*/
function createMenuTree(pages) {
return pages.filter(page => page._parent === '0').map(page => {
return Object.assign({
children: pages.filter(child => child._parent === page._id).reverse()
}, page.data);
});
}
/**
* Middleware for all /page/... routes
* @param req
* @param res
* @param next
*/
module.exports = asyncMiddleware(async function (req, res, next) {
try {
const menu = await Pages.getAll();
res.locals.menu = createMenuTree(menu);
} catch (error) {
console.log('Can not load menu:', error);
}
next();
});