mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-13 13:02:45 +00:00
Adapt Recursive Toc function from 3254
This commit is contained in:
@@ -1,100 +1,67 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const dedent = require('dedent-tabs').default;
|
const dedent = require('dedent-tabs').default;
|
||||||
|
|
||||||
const getTOC = ()=>{
|
const getTOC = (pages)=>{
|
||||||
const add1 = (title, page)=>{
|
|
||||||
res.push({
|
const recursiveAdd = (title, page, targetDepth, child, curDepth=0)=>{
|
||||||
title : title,
|
console.log(curDepth);
|
||||||
page : page,
|
if(curDepth > 5) return; // Something went wrong.
|
||||||
children : []
|
if(curDepth == targetDepth) {
|
||||||
});
|
child.push({
|
||||||
};
|
title : title,
|
||||||
const add2 = (title, page)=>{
|
page : page + 1,
|
||||||
if(!_.last(res)) add1(null, page);
|
children : []
|
||||||
_.last(res).children.push({
|
});
|
||||||
title : title,
|
} else {
|
||||||
page : page,
|
console.log(typeof child);
|
||||||
children : []
|
child.push({
|
||||||
});
|
title : null,
|
||||||
};
|
page : page + 1,
|
||||||
const add3 = (title, page)=>{
|
children : []
|
||||||
if(!_.last(res)) add1(null, page);
|
});
|
||||||
if(!_.last(_.last(res).children)) add2(null, page);
|
console.log(_.last(child));
|
||||||
_.last(_.last(res).children).children.push({
|
console.log(_.last(child).children);
|
||||||
title : title,
|
recursiveAdd(title, page, targetDepth, _.last(child).children, curDepth+1,);
|
||||||
page : page,
|
}
|
||||||
children : []
|
|
||||||
});
|
|
||||||
};
|
|
||||||
const add4 = (title, page)=>{
|
|
||||||
if(!_.last(res)) add1(null, page);
|
|
||||||
if(!_.last(_.last(res).children)) add2(null, page);
|
|
||||||
if(!_.last(!_.last(_.last(res).children))) add3(null, page);
|
|
||||||
_.last(_.last(_.last(res).children).children).children.push({
|
|
||||||
title : title,
|
|
||||||
page : page,
|
|
||||||
children : []
|
|
||||||
});
|
|
||||||
};
|
|
||||||
const add5 = (title, page)=>{
|
|
||||||
if(!_.last(res)) add1(null, page);
|
|
||||||
if(!_.last(_.last(res).children)) add2(null, page);
|
|
||||||
if(!_.last(!_.last(_.last(res).children))) add3(null, page);
|
|
||||||
if(!_.last(!_.last(!_.last(_.last(res).children)))) add4(null, page);
|
|
||||||
_.last(_.last(_.last(_.last(res).children).children).children).children.push({
|
|
||||||
title : title,
|
|
||||||
page : page,
|
|
||||||
children : []
|
|
||||||
});
|
|
||||||
};
|
|
||||||
const add6 = (title, page)=>{
|
|
||||||
if(!_.last(res)) add1(null, page);
|
|
||||||
if(!_.last(_.last(res).children)) add2(null, page);
|
|
||||||
if(!_.last(!_.last(_.last(res).children))) add3(null, page);
|
|
||||||
if(!_.last(!_.last(!_.last(_.last(res).children)))) add4(null, page);
|
|
||||||
if(!_.last(!_.last(!_.last(!_.last(_.last(res).children))))) add5(null, page);
|
|
||||||
_.last(_.last(_.last(_.last(_.last(res).children).children).children).children).children.push({
|
|
||||||
title : title,
|
|
||||||
page : page,
|
|
||||||
children : []
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = [];
|
const res = [];
|
||||||
const iframe = document.getElementById('BrewRenderer');
|
|
||||||
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
|
|
||||||
const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6');
|
|
||||||
|
|
||||||
_.each(headings, (heading)=>{
|
_.each(pages, (page, pageNum)=>{
|
||||||
const onPage = parseInt(heading.closest('.page,.phb').id?.replace(/^p/, ''));
|
if(!page.includes("{{frontCover}}") && !page.includes("{{insideCover}}") && !page.includes("{{partCover}}") && !page.includes("{{backCover}}")) {
|
||||||
const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC');
|
const lines = page.split('\n');
|
||||||
if(ToCExclude != 'exclude') {
|
_.each(lines, (line)=>{
|
||||||
if(!isNaN(onPage)) {
|
if(_.startsWith(line, '# ')){
|
||||||
const headingText = heading.innerText.trim();
|
const title = line.replace('# ', '');
|
||||||
if(heading.tagName == 'H1') {
|
recursiveAdd(title, pageNum, 0, res);
|
||||||
add1(headingText, onPage);
|
|
||||||
}
|
}
|
||||||
if(heading.tagName == 'H2') {
|
if(_.startsWith(line, '## ')){
|
||||||
add2(headingText, onPage);
|
const title = line.replace('## ', '');
|
||||||
|
recursiveAdd(title, pageNum, 1, res);
|
||||||
}
|
}
|
||||||
if(heading.tagName == 'H3') {
|
if(_.startsWith(line, '### ')){
|
||||||
add3(headingText, onPage);
|
const title = line.replace('### ', '');
|
||||||
|
recursiveAdd(title, pageNum, 2, res);
|
||||||
}
|
}
|
||||||
if(heading.tagName == 'H4') {
|
if(_.startsWith(line, '#### ')){
|
||||||
add4(headingText, onPage);
|
const title = line.replace('#### ', '');
|
||||||
|
recursiveAdd(title, pageNum, 3, res);
|
||||||
}
|
}
|
||||||
if(heading.tagName == 'H5') {
|
if(_.startsWith(line, '##### ')){
|
||||||
add5(headingText, onPage);
|
const title = line.replace('##### ', '');
|
||||||
|
recursiveAdd(title, pageNum, 4, res);
|
||||||
}
|
}
|
||||||
if(heading.tagName == 'H6') {
|
if(_.startsWith(line, '##### ')){
|
||||||
add6(headingText, onPage);
|
const title = line.replace('##### ', '');
|
||||||
|
recursiveAdd(title, pageNum, 5, res);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const ToCIterate = (entries, curDepth=0)=>{
|
const ToCIterate = (entries, curDepth=0)=>{
|
||||||
const levelPad = ['- ###', ' - ####', ' - ####', ' - ####', ' - ####', ' - ####'];
|
const levelPad = ['- ###', ' - ####', ' - ####', ' - ####', ' - ####', ' - ####'];
|
||||||
const toc = [];
|
const toc = [];
|
||||||
@@ -113,7 +80,8 @@ const ToCIterate = (entries, curDepth=0)=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports = function(props){
|
module.exports = function(props){
|
||||||
const TOC = getTOC();
|
const pages = props.brew.text.split('\\page');
|
||||||
|
const TOC = getTOC(pages);
|
||||||
const markdown = _.reduce(TOC, (r, g1, idx1)=>{
|
const markdown = _.reduce(TOC, (r, g1, idx1)=>{
|
||||||
r.push(ToCIterate(g1).join('\n'));
|
r.push(ToCIterate(g1).join('\n'));
|
||||||
return r;
|
return r;
|
||||||
|
|||||||
Reference in New Issue
Block a user