Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions javascript/tocUtils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
export const generateChapterIndex = (filename: string) => {
let chapterIndex = "";
if (filename.match(/chapter/)) {
// match the number after string "chapter"
chapterIndex += filename.match(/(?<=chapter)\d+/g)![0];
const chapterMatch = filename.match(/(?<=chapter)\d+/g);
if (chapterMatch) {
chapterIndex += chapterMatch[0];
}
if (filename.match(/section/)) {
// "section"
chapterIndex += "." + filename.match(/(?<=section)\d+/g)![0];
const sectionMatch = filename.match(/(?<=section)\d+/g);
if (sectionMatch) {
chapterIndex += "." + sectionMatch[0];
}
if (filename.match(/subsection/)) {
// "subsection"
chapterIndex += "." + filename.match(/(?<=subsection)\d+/g)![0];
const subsectionMatch = filename.match(/(?<=subsection)\d+/g);
if (subsectionMatch) {
chapterIndex += "." + subsectionMatch[0];
}
if (filename.match(/foreword/)) {
chapterIndex = filename.match(/foreword\d*/g)![0];
} else if (filename.match(/prefaces/)) {
chapterIndex = filename.match(/prefaces\d*/g)![0];
const forewordMatch = filename.match(/foreword\d*/g);
if (forewordMatch) {
chapterIndex = forewordMatch[0];
} else {
const prefacesMatch = filename.match(/prefaces\d*/g);
if (prefacesMatch) {
chapterIndex = prefacesMatch[0];
}
} else if (filename.match(/acknowledgements/)) {
Comment on lines +18 to 23
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This refactoring introduces a syntax error. The else if on line 23 is not valid because it follows a complete if-else statement (lines 16-23). An else if must follow an if or another else if without an intervening else block.

To preserve the original logic of a single chain of mutually exclusive checks, the subsequent else if statements should be nested correctly inside the else block.

  } else {
    const prefacesMatch = filename.match(/prefaces\d*/g);
    if (prefacesMatch) {
      chapterIndex = prefacesMatch[0];
    } else if (filename.match(/acknowledgements/)) {
      chapterIndex = "acknowledgements";
    }
  }

chapterIndex = "acknowledgements";
} else if (filename.match(/references/)) {
Expand Down