mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-09 02:52:38 +00:00
Fix missed issues with converting to delightfully recursive function
This commit is contained in:
@@ -4,7 +4,6 @@ const dedent = require('dedent-tabs').default;
|
|||||||
const getTOC = (pages)=>{
|
const getTOC = (pages)=>{
|
||||||
|
|
||||||
const recursiveAdd = (title, page, targetDepth, child, curDepth=0)=>{
|
const recursiveAdd = (title, page, targetDepth, child, curDepth=0)=>{
|
||||||
console.log(curDepth);
|
|
||||||
if(curDepth > 5) return; // Something went wrong.
|
if(curDepth > 5) return; // Something went wrong.
|
||||||
if(curDepth == targetDepth) {
|
if(curDepth == targetDepth) {
|
||||||
child.push({
|
child.push({
|
||||||
@@ -13,49 +12,36 @@ const getTOC = (pages)=>{
|
|||||||
children : []
|
children : []
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log(typeof child);
|
|
||||||
child.push({
|
child.push({
|
||||||
title : null,
|
title : null,
|
||||||
page : page + 1,
|
page : page + 1,
|
||||||
children : []
|
children : []
|
||||||
});
|
});
|
||||||
console.log(_.last(child));
|
|
||||||
console.log(_.last(child).children);
|
|
||||||
recursiveAdd(title, page, targetDepth, _.last(child).children, curDepth+1,);
|
recursiveAdd(title, page, targetDepth, _.last(child).children, curDepth+1,);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = [];
|
const res = [];
|
||||||
|
|
||||||
_.each(pages, (page, pageNum)=>{
|
const iframe = document.getElementById('BrewRenderer');
|
||||||
if(!page.includes("{{frontCover}}") && !page.includes("{{insideCover}}") && !page.includes("{{partCover}}") && !page.includes("{{backCover}}")) {
|
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
|
||||||
const lines = page.split('\n');
|
const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6');
|
||||||
_.each(lines, (line)=>{
|
const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
|
||||||
if(_.startsWith(line, '# ')){
|
const exclusionElements = iframeDocument.querySelectorAll('frontCover insideCover partCover backCover');
|
||||||
const title = line.replace('# ', '');
|
const excludedPages = [];
|
||||||
recursiveAdd(title, pageNum, 0, res);
|
|
||||||
}
|
// Build a list of excluded pages.
|
||||||
if(_.startsWith(line, '## ')){
|
_.each(exclusionElements, (e)=>{
|
||||||
const title = line.replace('## ', '');
|
const onPage = parseInt(e.closest('.page,.phb').id?.replace(/^p/, ''));
|
||||||
recursiveAdd(title, pageNum, 1, res);
|
if(!excludedPages.includes(onPage)) excludedPages.push(onPage);
|
||||||
}
|
});
|
||||||
if(_.startsWith(line, '### ')){
|
|
||||||
const title = line.replace('### ', '');
|
_.each(headings, (heading)=>{
|
||||||
recursiveAdd(title, pageNum, 2, res);
|
const onPage = parseInt(heading.closest('.page,.phb').id?.replace(/^p/, ''));
|
||||||
}
|
const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC');
|
||||||
if(_.startsWith(line, '#### ')){
|
|
||||||
const title = line.replace('#### ', '');
|
if((ToCExclude != 'exclude') && (!excludedPages.includes(onPage))) {
|
||||||
recursiveAdd(title, pageNum, 3, res);
|
recursiveAdd(heading.innerText.trim(), onPage, headerDepth.indexOf(heading.tagName), res);
|
||||||
}
|
|
||||||
if(_.startsWith(line, '##### ')){
|
|
||||||
const title = line.replace('##### ', '');
|
|
||||||
recursiveAdd(title, pageNum, 4, res);
|
|
||||||
}
|
|
||||||
if(_.startsWith(line, '##### ')){
|
|
||||||
const title = line.replace('##### ', '');
|
|
||||||
recursiveAdd(title, pageNum, 5, res);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user