Giter Site home page Giter Site logo

Comments (1)

ksd3 avatar ksd3 commented on September 3, 2024

Found somewhat of a solution: This is a common problem with all markdown-based book generators (even Quatro has it) that the chapter numbering is drawn from Markdown's #toc - so it's hardcoded and can't easily be changed. A workaround would be to have numbering as follows:

Preliminary Chapter

Preliminary Chapter

Section 1

Section 1 Chapter 1 (1. Chapter 1)

Section 1 Chapter 2 (2. Chapter 2)

Section 1 Subchapter 1 (2.1. Chapter 2.1)

Section 2

Section 2 Chapter 1 (1. Chapter 1)

.
.

Final Chapter

This is specifically because it is ambiguous to specify what the suffix chapter would be for a section (if its a nested chapter then it should have an index, rather than no index at all), so this format looks better.

The solution is to add a function at the bottom of the book.js file

(function customizeChapterNumbers() {
    function resetChapterNumbers() {
        const sidebar = document.querySelector('#sidebar .sidebar-scrollbox');
        if (!sidebar) {
            console.log('Sidebar not found.');
            return;
        }

        let chapterCounter = 0;  // Counter for main chapters
        let subChapterCounter = 0;  // Counter for subchapters

        sidebar.querySelectorAll('ol.chapter > li').forEach((item) => {
            const partTitle = item.classList.contains('part-title');
            const chapterLink = item.querySelector('a');

            if (partTitle) {
                // Reset chapter counter for each new section
                chapterCounter = 0;
                console.log(`New section found: ${item.textContent.trim()}, resetting chapter counter.`);
            } else if (chapterLink) {
                // Determine if it's a main chapter or a subchapter
                const isSubChapter = item.querySelector('ol.section');

                if (!isSubChapter) {
                    // It's a main chapter
                    chapterCounter++;    // Increment main chapter counter
                    subChapterCounter = 0;  // Reset subchapter counter for each new chapter
                    const chapterNumber = `${chapterCounter}`;
                    const strongElem = chapterLink.querySelector('strong');
                    if (strongElem) {
                        console.log(`Setting chapter number: ${chapterNumber} for chapter ${chapterLink.textContent.trim()}`);
                        strongElem.textContent = `${chapterNumber}. `;
                    }
                } else {
                    // It's a subchapter
                    subChapterCounter++;
                    const chapterNumber = `${chapterCounter}`;
                    const subChapterNumber = `${chapterNumber}.${subChapterCounter}`;
                    const strongElem = chapterLink.querySelector('strong');
                    if (strongElem) {
                        console.log(`Setting subchapter number: ${subChapterNumber} for chapter ${chapterLink.textContent.trim()}`);
                        strongElem.textContent = `${subChapterNumber}. `;
                    }
                }
            }
        });
    }

    document.addEventListener('DOMContentLoaded', resetChapterNumbers);
})();

This generates chapter numbers according to the above scheme. This issue's name should now be changed to something like 'Reset Chapter Numbering', but I'm leaving these keywords in in the hope that someone encountering this problem finds this solution in the future.

from mdbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.