0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-10 11:22:40 +00:00

First pass at code fixes.

Move functions out of function
Use querySelector instead of querySelectorAll
Flip skip and reset counter order.
This commit is contained in:
David Bolack
2024-09-07 19:47:16 -05:00
parent 92f963d798
commit dcc7a22272

View File

@@ -1,8 +1,6 @@
const _ = require('lodash'); const _ = require('lodash');
const dedent = require('dedent-tabs').default; const dedent = require('dedent-tabs').default;
const getTOC = ()=>{
const iframe = document.getElementById('BrewRenderer'); const iframe = document.getElementById('BrewRenderer');
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6'); const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6');
@@ -19,17 +17,20 @@ const getTOC = ()=>{
const pages = iframeDocument.querySelectorAll('.page'); const pages = iframeDocument.querySelectorAll('.page');
_.each(pages, (page)=>{ _.each(pages, (page)=>{
current++; current++;
if(page.querySelectorAll('.skipCounting').length > 0) { const doSkip = (page.querySelector('.skipCounting').length > 0);
skip += 1; const doReset = (page.querySelector('.resetCounting').length > 0);
} else if(page.querySelectorAll('.resetCounting').length > 0) { if(doReset) {
reset = current - 1; reset = current - 1;
skip = 0; skip = 0;
} else if(doSkip){
skip += 1;
} }
pageMap[current] = current - reset - skip; pageMap[current] = current - reset - skip;
}); });
}; };
const recursiveAdd = (title, page, anchor, targetDepth, child, curDepth=0)=>{ const recursiveAdd = (title, page, actualPage, targetDepth, child, curDepth=0)=>{
const anchor = `p${actualPage}`;
if(curDepth > 5) return; // Something went wrong. if(curDepth > 5) return; // Something went wrong.
if(curDepth == targetDepth) { if(curDepth == targetDepth) {
child.push({ child.push({
@@ -51,6 +52,9 @@ const getTOC = ()=>{
} }
}; };
const getTOC = ()=>{
walkPages(); walkPages();
_.each(headings, (heading)=>{ _.each(headings, (heading)=>{
@@ -59,7 +63,7 @@ const getTOC = ()=>{
const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC'); const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC');
if(ToCExclude != 'exclude') { if(ToCExclude != 'exclude') {
recursiveAdd(heading.textContent.trim(), pageMap[onPage], pageAnchor, headerDepth.indexOf(heading.tagName), res); recursiveAdd(heading.textContent.trim(), pageMap[onPage], onPage, headerDepth.indexOf(heading.tagName), res);
} }
}); });
return res; return res;