UNPKG

asciidoctor-chunker

Version:

Creates chunked (multi-page) HTML from Asciidoctor's single HTML file with supporting the fine-tuned splits in chapters, sections and any depth of subsections.

76 lines (71 loc) 2.59 kB
'use strict'; import Node from './Node.mjs'; /** * Visits the nodes under the #content node and invoke * the given processor callbacks. * * @param {(root:boolean, node:{Node}, isFirstPage:boolean) * => void} preambleProcessor * @param {(root:boolean, node:{Node}, partNumber:number, * isFirstPage:boolean) => void} partProcessor * @param {(config:{object}, root:boolean, node:{Node}, * thisSectLevel:number, filenamePrefix:{string}, * basenameFn:({string}, {number}, {number}) => {string}, * sectionNumber:number, isFirstPage:boolean) => void} * chapterProcessor * @param {Node} rootNode The document root node of the * single html document generated by asciidoctor-chunker. * The node is cloned before processing in this function * to avoid the side effects. * @param {object} config: The configuration object which has * `depth` object to specify the maximum sectLevel to extract. * The default is 1 which extracts parts and chapters. * The example format is as follows: * ``` * { * depth: { * default: 1, // the default is to extract only chapters * 2: 4, // extracts subsubsections in chap 2 * 3: 2, // extracts sections in chap 3 * } * } * ``` * @param {(string, number, number) => string} basenameMaker The * function that generates the basename of the current html given * the section information. */ const processContents = ( preambleProcessor, partProcessor, chapterProcessor, root, config, basenameMaker) => { let chap = 0; let part = 0; let firstPageProcessed = false; let isFirstPage = false; root.find('#content').children().each((node, i) => { if (node.hasClass('partintro')) return; // ignore. this is taken care by part extraction if (node.hasClass('sect1')) { if (!firstPageProcessed && !isFirstPage) { isFirstPage = true; firstPageProcessed = true; } else isFirstPage = false; return chapterProcessor(config, root, node, 1, 'chap', basenameMaker, ++chap, isFirstPage); // recursive extraction of chapters } if (node.hasClass('sect0')) { if (!firstPageProcessed && !isFirstPage) { isFirstPage = true; firstPageProcessed = true; } else isFirstPage = false; // part extraction return partProcessor(config, root, node, ++part, isFirstPage); } if (node.getAttr('id') === 'preamble') { isFirstPage = true; firstPageProcessed = true; return preambleProcessor(config, root, node, isFirstPage); } }); }; export default processContents;