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:
parent
730eff7995
commit
f845a0d09f
4 changed files with 65 additions and 18 deletions
33
src/routes/middlewares/pages.js
Normal file
33
src/routes/middlewares/pages.js
Normal 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();
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue