import { Page } from '../book' ; import { HierarchyEntry } from '../types' ; export const annotatePagesNumbers = ( pages , offset ) => { const facingPages = true ; if ( facingPages ) { pages . forEach ( ( page , i ) => { page . number = offset + i + 1 ; page . setLeftRight ( i % 2 === 0 ? 'right' : 'left' ) ; } ) ; } else { pages . forEach ( page => { page . setLeftRight ( 'right' ) ; } ) ; } } ; export const annotatePagesHierarchy = ( pages , headerSelectorHierarchy ) => { let currentHierarchy = [ ] ; pages . forEach ( page => { const pageHierarchy = [ ] ; headerSelectorHierarchy . forEach ( ( selector , i ) => { const element = page . element . querySelector ( selector ) ; if ( element ) { currentHierarchy [ i ] = { selector : selector , text : element . textContent ?? '' , el : element , } ; currentHierarchy = currentHierarchy . slice ( 0 , i + 1 ) ; } if ( currentHierarchy [ i ] ) { pageHierarchy [ i ] = currentHierarchy [ i ] ; } } ) ; page . hierarchy = pageHierarchy ; } ) ; } ; export const annotatePages = ( pages , offset ) => { annotatePagesNumbers ( pages , offset ) ; annotatePagesHierarchy ( pages , [ 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] ) ; } ;