0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-01 23:52:40 +00:00

Fix TOC generation for level 3 entries not inside level 2

This commit is contained in:
Trevor Buckner
2021-07-20 15:40:32 -04:00
parent 324d0e265e
commit 7ff6d9e825

View File

@@ -10,7 +10,7 @@ const getTOC = (pages)=>{
});
};
const add2 = (title, page)=>{
if(!_.last(res)) add1('', page);
if(!_.last(res)) add1(null, page);
_.last(res).children.push({
title : title,
page : page + 1,
@@ -18,8 +18,8 @@ const getTOC = (pages)=>{
});
};
const add3 = (title, page)=>{
if(!_.last(res)) add1('', page);
if(!_.last(_.last(res).children)) add2('', page);
if(!_.last(res)) add1(null, page);
if(!_.last(_.last(res).children)) add2(null, page);
_.last(_.last(res).children).children.push({
title : title,
page : page + 1,
@@ -52,13 +52,22 @@ module.exports = function(brew){
const pages = brew.text.split('\\page');
const TOC = getTOC(pages);
const markdown = _.reduce(TOC, (r, g1, idx1)=>{
r.push(`\t\t- ### [{{ ${g1.title}}}{{ ${g1.page}}}](#p${g1.page})`);
if(g1.title !== null) {
r.push(`\t\t- ### [{{ ${g1.title}}}{{ ${g1.page}}}](#p${g1.page})`);
}
if(g1.children.length){
_.each(g1.children, (g2, idx2)=>{
r.push(`\t\t - #### [{{ ${g2.title}}}{{ ${g2.page}}}](#p${g2.page})`);
if(g2.title !== null) {
r.push(`\t\t - #### [{{ ${g2.title}}}{{ ${g2.page}}}](#p${g2.page})`);
}
if(g2.children.length){
_.each(g2.children, (g3, idx3)=>{
r.push(`\t\t - [{{ ${g3.title}}}{{ ${g3.page}}}](#p${g3.page})`);
if(g2.title !== null) {
r.push(`\t\t - [{{ ${g3.title}}}{{ ${g3.page}}}](#p${g3.page})`);
}
else { // Don't over-indent if no level-2 parent entry
r.push(`\t\t - [{{ ${g3.title}}}{{ ${g3.page}}}](#p${g3.page})`);
}
});
}
});