0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-29 00:22:47 +00:00

Merge branch 'master' into skipCountingSnippet

This commit is contained in:
Trevor Buckner
2024-09-09 11:10:47 -04:00
committed by GitHub
6 changed files with 103 additions and 61 deletions

View File

@@ -1,57 +1,60 @@
const _ = require('lodash');
const dedent = require('dedent-tabs').default;
const mapPages = (iframeDocument, pageMap)=>{
let actualPage = 0;
let mappedPage = 0;
const pages = iframeDocument.querySelectorAll('.page');
_.each(pages, (page)=>{
actualPage++;
const doSkip = page.querySelector('.skipCounting');
const doReset = page.querySelector('.resetCounting');
if(doReset)
mappedPage = 1;
if(!doSkip && !doReset)
mappedPage++;
pageMap[actualPage] = {
mappedPage : mappedPage,
showPage : !doSkip
};
});
};
const recursiveAdd = (title, page, actualPage, targetDepth, child, curDepth=0)=>{
const anchor = `p${actualPage}`;
if(curDepth > 5) return; // Something went wrong.
if(curDepth == targetDepth) {
child.push({
title : title,
page : page,
anchor : anchor,
children : []
});
} else {
if(child.length == 0) {
child.push({
title : null,
page : page,
anchor : anchor,
children : []
});
}
recursiveAdd(title, page, anchor, targetDepth, _.last(child).children, curDepth+1,);
}
};
const getTOC = ()=>{
const pageMap = [];
const entries = [];
const iframe = document.getElementById('BrewRenderer');
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
const headings = iframeDocument.querySelectorAll('h1, h2, h3, h4, h5, h6');
const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
const res = [];
const pageMap = [];
const walkPages = ()=>{
let current = 0;
let skip = 0;
let reset = 0;
const pages = iframeDocument.querySelectorAll('.page');
_.each(pages, (page)=>{
current++;
if(page.querySelectorAll('.skipCounting').length > 0) {
skip += 1;
} else if(page.querySelectorAll('.resetCounting').length > 0) {
reset = current - 1;
skip = 0;
}
pageMap[current] = current - reset - skip;
});
};
const recursiveAdd = (title, page, anchor, targetDepth, child, curDepth=0)=>{
if(curDepth > 5) return; // Something went wrong.
if(curDepth == targetDepth) {
child.push({
title : title,
page : page,
anchor : anchor,
children : []
});
} else {
if(child.length == 0) {
child.push({
title : null,
page : page,
anchor : anchor,
children : []
});
}
recursiveAdd(title, page, anchor, targetDepth, _.last(child).children, curDepth+1,);
}
};
walkPages();
mapPages(iframeDocument, pageMap);
_.each(headings, (heading)=>{
const pageAnchor = heading.closest('.page').id;
@@ -59,10 +62,10 @@ const getTOC = ()=>{
const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC');
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), entries);
}
});
return res;
return entries;
};
@@ -70,7 +73,7 @@ const ToCIterate = (entries, curDepth=0)=>{
const levelPad = ['- ###', ' - ####', ' - ', ' - ', ' - ', ' - '];
const toc = [];
if(entries.title !== null){
toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page}}}](#${entries.anchor})`);
if(entries.page.showPage) toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page.mappedPage}}}](#${entries.anchor})`);
}
if(entries.children.length) {
_.each(entries.children, (entry, idx)=>{
@@ -88,7 +91,7 @@ module.exports = function(props){
const markdown = _.reduce(TOC, (r, g1, idx1)=>{
r.push(ToCIterate(g1).join('\n'));
return r;
}, []).join('\n');
}, []).join('\n').replace('\n\n', '\n');
return dedent`
{{toc,wide