Comments (1)
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)
- Use capital letters to indicate the default value in (y/n) prompts
- serve only html backend
- Support for automated tests in languages other than Rust
- Exclude page from printing
- If `!search_config.copy_js`, then perform useless search indexing, and search is disabled
- feat(ux): add CMD+K support HOT 3
- Cannot load custom font files with 'mdbook serve' on macOS
- warn on orphan files
- Ordering of footnote HOT 1
- Ensure Consistent Table Widths in mdBook
- Keep summary links untouched (including Markdown formatting) instead of extracting just plain text
- Is there an option to disable automatic file creation from `SUMMARY.md`? HOT 2
- Small options for creating interactive fictions HOT 2
- Use Content-Secure-Policy compatible scripts
- Add predefined reference files for compilation
- Support different URL formats HOT 1
- mdBook x PyOdide: How to make Python code snippets run using pyodide? HOT 1
- Is there a way to force horizontal rules under headings while generating the book? HOT 2
- Support include directives in SUMMARY.md HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mdbook.