Skip to content

Commit 6d29951

Browse files
authored
Optimize main.js (#20093)
This Pull Request optimizes the functions used in `/docs/_spec/public/scripts/main.js`. ## Changes made: 1. `currentChapter()` function was simplified using `split()` and `pop()` instead of `lastIndexOf()` and `substring()`. 2. Used template literals for string interpolation. 3. Simplified the `heading` function by reducing repetitive code and improving readability. 4. Changed `.removeClass()` and `.addClass()` to `.toggleClass()` for toggling the class based on condition. 5. General cleanup and optimization for better readability and performance.
1 parent b0d32aa commit 6d29951

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

docs/_spec/public/scripts/main.js

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
function currentChapter() {
2-
var path = document.location.pathname;
3-
var idx = path.lastIndexOf("/") + 1;
4-
var chap = path.substring(idx, idx + 2);
5-
return parseInt(chap, 10);
2+
return parseInt(document.location.pathname.split('/').pop().substr(0, 2), 10);
63
}
74

85
function heading(i, heading, $heading) {
9-
var currentLevel = parseInt(heading.tagName.substring(1));
10-
var result = "";
6+
const currentLevel = parseInt(heading.tagName.substring(1));
7+
118
if (currentLevel === this.headerLevel) {
12-
this.headerCounts[this.headerLevel] += 1;
13-
return "" + this.headerCounts[this.headerLevel] + " " + $heading.text();
9+
this.headerCounts[this.headerLevel]++;
1410
} else if (currentLevel < this.headerLevel) {
15-
while(currentLevel < this.headerLevel) {
11+
while (currentLevel < this.headerLevel) {
1612
this.headerCounts[this.headerLevel] = 1;
17-
this.headerLevel -= 1;
13+
this.headerLevel--;
1814
}
19-
this.headerCounts[this.headerLevel] += 1;
20-
return "" + this.headerCounts[this.headerLevel]+ " " + $heading.text();
15+
this.headerCounts[this.headerLevel]++;
2116
} else {
22-
while(currentLevel > this.headerLevel) {
23-
this.headerLevel += 1;
17+
while (currentLevel > this.headerLevel) {
18+
this.headerLevel++;
2419
this.headerCounts[this.headerLevel] = 1;
2520
}
26-
return "" + this.headerCounts[this.headerLevel]+ " " + $heading.text();
2721
}
22+
return `${this.headerCounts[this.headerLevel]} ${$heading.text()}`;
2823
}
2924

3025
// ignore when using wkhtmltopdf, or it won't work...
31-
if(window.jekyllEnv !== 'spec-pdf') {
26+
if (window.jekyllEnv !== 'spec-pdf') {
3227
$('#toc').toc(
3328
{
3429
'selectors': 'h1,h2,h3',
@@ -64,8 +59,6 @@ document.addEventListener("DOMContentLoaded", function() {
6459
});
6560

6661
$("#chapters a").each(function (index) {
67-
if (document.location.pathname.endsWith($(this).attr("href")))
68-
$(this).addClass("chapter-active");
69-
else
70-
$(this).removeClass("chapter-active");
62+
const href = $(this).attr("href");
63+
$(this).toggleClass("chapter-active", document.location.pathname.endsWith(href));
7164
});

0 commit comments

Comments
 (0)