diff --git a/themes/V3/5ePHB/snippets/tableOfContents.gen.js b/themes/V3/5ePHB/snippets/tableOfContents.gen.js index 97d82ed40..1be583ba1 100644 --- a/themes/V3/5ePHB/snippets/tableOfContents.gen.js +++ b/themes/V3/5ePHB/snippets/tableOfContents.gen.js @@ -1,7 +1,7 @@ const _ = require('lodash'); const dedent = require('dedent-tabs').default; -const getTOC = (pages)=>{ +const getTOC = ()=>{ const add1 = (title, page)=>{ res.push({ title : title, @@ -27,32 +27,45 @@ const getTOC = (pages)=>{ }); }; + const getParentPageNumber = (e)=>{ + let tE = e; + while (tE?.tagName != 'BODY') { + if((tE.className == 'page') || (tE.className.split(' ')?.includes('Page'))) { + // Test for excluded pages here. + return parseInt(tE.id.replace(/^p/, '')); + } + tE = tE.parentElement; + } + return -1; + }; + const res = []; - _.each(pages, (page, pageNum)=>{ - if(!page.includes("{{frontCover}}") && !page.includes("{{insideCover}}") && !page.includes("{{partCover}}") && !page.includes("{{backCover}}")) { - const lines = page.split('\n'); - _.each(lines, (line)=>{ - if(_.startsWith(line, '# ')){ - const title = line.replace('# ', ''); - add1(title, pageNum); + const iframe = document.getElementById('BrewRenderer'); + const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; + const headings = iframeDocument.querySelectorAll('h1, h2, h3'); + + _.each(headings, (heading)=>{ + const onPage = getParentPageNumber(heading); + if(getComputedStyle(heading).getPropertyValue('--TOC') != 'exclude') { + if(onPage != -1) { + const headingText = heading.innerText; + if(heading.tagName == 'H1') { + add1(headingText, onPage); } - if(_.startsWith(line, '## ')){ - const title = line.replace('## ', ''); - add2(title, pageNum); + if(heading.tagName == 'H2') { + add2(headingText, onPage); } - if(_.startsWith(line, '### ')){ - const title = line.replace('### ', ''); - add3(title, pageNum); + if(heading.tagName == 'H3') { + add3(headingText, onPage); } - }); + } } }); return res; }; module.exports = function(props){ - const pages = props.brew.text.split('\\page'); - const TOC = getTOC(pages); + const TOC = getTOC(); const markdown = _.reduce(TOC, (r, g1, idx1)=>{ if(g1.title !== null) { r.push(`- ### [{{ ${g1.title}}}{{ ${g1.page}}}](#p${g1.page})`); diff --git a/themes/V3/5ePHB/style.less b/themes/V3/5ePHB/style.less index 6c6634ce7..fba45df42 100644 --- a/themes/V3/5ePHB/style.less +++ b/themes/V3/5ePHB/style.less @@ -314,6 +314,7 @@ border-image : @monsterBorderImage 14 round; border-image-outset : 0px 2px; box-shadow : 1px 4px 14px #888888; + --TOC : "exclude"; } position : relative; @@ -329,11 +330,13 @@ margin-bottom : 0; font-size : 0.304cm; //Monster size and type subtext } + --TOC : "exclude"; } h3 { font-family : 'ScalySansSmallCapsRemake'; font-size : 0.45cm; border-bottom : 1.5px solid var(--HB_Color_HeaderText); + --TOC : "exclude"; } //Triangle dividers