mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-13 21:42:45 +00:00
Merge pull request #3639 from dbolack-ab/actualPageNumber
Rework page counters for skipping and resets.
This commit is contained in:
@@ -1,44 +1,71 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const dedent = require('dedent-tabs').default;
|
const dedent = require('dedent-tabs').default;
|
||||||
|
|
||||||
const getTOC = (pages)=>{
|
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');
|
||||||
|
|
||||||
const recursiveAdd = (title, page, targetDepth, child, curDepth=0)=>{
|
if(doReset)
|
||||||
if(curDepth > 5) return; // Something went wrong.
|
mappedPage = 1;
|
||||||
if(curDepth == targetDepth) {
|
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({
|
child.push({
|
||||||
title : title,
|
title : null,
|
||||||
page : page,
|
page : page,
|
||||||
|
anchor : anchor,
|
||||||
children : []
|
children : []
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
if(child.length == 0) {
|
|
||||||
child.push({
|
|
||||||
title : null,
|
|
||||||
page : page,
|
|
||||||
children : []
|
|
||||||
});
|
|
||||||
}
|
|
||||||
recursiveAdd(title, page, targetDepth, _.last(child).children, curDepth+1,);
|
|
||||||
}
|
}
|
||||||
};
|
recursiveAdd(title, page, anchor, targetDepth, _.last(child).children, curDepth+1,);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const res = [];
|
|
||||||
|
const getTOC = ()=>{
|
||||||
|
const pageMap = [];
|
||||||
|
const entries = [];
|
||||||
|
|
||||||
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');
|
||||||
const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
|
const headerDepth = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
|
||||||
|
|
||||||
|
mapPages(iframeDocument, pageMap);
|
||||||
|
|
||||||
_.each(headings, (heading)=>{
|
_.each(headings, (heading)=>{
|
||||||
const onPage = parseInt(heading.closest('.page').id?.replace(/^p/, ''));
|
const onPage = parseInt(heading.closest('.page').id?.replace(/^p/, ''));
|
||||||
const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC');
|
const ToCExclude = getComputedStyle(heading).getPropertyValue('--TOC');
|
||||||
|
|
||||||
if(ToCExclude != 'exclude') {
|
if(ToCExclude != 'exclude') {
|
||||||
recursiveAdd(heading.textContent.trim(), onPage, headerDepth.indexOf(heading.tagName), res);
|
recursiveAdd(heading.textContent.trim(), pageMap[onPage], onPage, headerDepth.indexOf(heading.tagName), entries);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return res;
|
return entries;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -46,7 +73,7 @@ const ToCIterate = (entries, curDepth=0)=>{
|
|||||||
const levelPad = ['- ###', ' - ####', ' - ', ' - ', ' - ', ' - '];
|
const levelPad = ['- ###', ' - ####', ' - ', ' - ', ' - ', ' - '];
|
||||||
const toc = [];
|
const toc = [];
|
||||||
if(entries.title !== null){
|
if(entries.title !== null){
|
||||||
toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page}}}](#p${entries.page})`);
|
if(entries.page.showPage) toc.push(`${levelPad[curDepth]} [{{ ${entries.title}}}{{ ${entries.page.mappedPage}}}](#${entries.anchor})`);
|
||||||
}
|
}
|
||||||
if(entries.children.length) {
|
if(entries.children.length) {
|
||||||
_.each(entries.children, (entry, idx)=>{
|
_.each(entries.children, (entry, idx)=>{
|
||||||
@@ -60,12 +87,11 @@ const ToCIterate = (entries, curDepth=0)=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports = function(props){
|
module.exports = function(props){
|
||||||
const pages = props.brew.text.split('\\page');
|
const TOC = getTOC();
|
||||||
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;
|
||||||
}, []).join('\n');
|
}, []).join('\n').replace('\n\n', '\n');
|
||||||
|
|
||||||
return dedent`
|
return dedent`
|
||||||
{{toc,wide
|
{{toc,wide
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
@page { margin : 0; }
|
@page { margin : 0; }
|
||||||
body { counter-reset : page-numbers; }
|
body { counter-reset : page-numbers 0; }
|
||||||
* { -webkit-print-color-adjust : exact; }
|
* { -webkit-print-color-adjust : exact; }
|
||||||
|
|
||||||
//*****************************
|
//*****************************
|
||||||
@@ -51,7 +51,6 @@ body { counter-reset : page-numbers; }
|
|||||||
height : 279.4mm;
|
height : 279.4mm;
|
||||||
padding : 1.4cm 1.9cm 1.7cm;
|
padding : 1.4cm 1.9cm 1.7cm;
|
||||||
overflow : hidden;
|
overflow : hidden;
|
||||||
counter-increment : page-numbers;
|
|
||||||
background-color : var(--HB_Color_Background);
|
background-color : var(--HB_Color_Background);
|
||||||
text-rendering : optimizeLegibility;
|
text-rendering : optimizeLegibility;
|
||||||
contain : size;
|
contain : size;
|
||||||
@@ -494,4 +493,13 @@ body { counter-reset : page-numbers; }
|
|||||||
&:nth-child(even) {
|
&:nth-child(even) {
|
||||||
.pageNumber { left : 30px; }
|
.pageNumber { left : 30px; }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
.resetCounting {
|
||||||
|
counter-set : page-numbers 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:not(:has(.skipCounting)) {
|
||||||
|
counter-increment : page-numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user