Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiny JS Optimization #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
138 changes: 43 additions & 95 deletions javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,108 +113,56 @@ function updateProgressBarAndFadeIn() {
}

function createRightSidebar() {
const content = document.getElementsByClassName('content')[0];
if (!content)
return;

const sections = content.getElementsByClassName('section');
if (!sections)
return;

var sidebarContent = document.getElementById('sidebarContent');
if (!sidebarContent)
return;

for (var i = 0; i < sections.length; i++) {
var section = sections[i];
const headers = section.querySelectorAll('h2');
const cards = section.querySelectorAll('.card');

headers.forEach(header => {
if (!header.innerHTML || header.innerHTML.length == 0)
return;

// Create the section div
const sectionDiv = document.createElement('div');
sidebarContent.appendChild(sectionDiv);

// Create the header link
const bold = document.createElement('b');
sectionDiv.appendChild(bold);

const separator = document.createElement('a');
separator.href = `#${section.id}`;
separator.textContent = header.innerHTML;
bold.appendChild(separator);

// Create section links
cards.forEach(card => {
var text;
const title = card.getAttribute('title');
if (title && title.length > 0) {
text = title;
}
else {
text = card.id.replace(/([A-Z])/g, ' $1').trim();
}

if (!text || text.length == 0) {
return;
}

const cardId = card.id;
const cardLink = document.createElement('a');
cardLink.href = `#${cardId}`;
cardLink.textContent = text;

sectionDiv.appendChild(cardLink);
})
const sections = document.querySelectorAll('.section');
const sidebar = document.getElementById('sidebarContent');
if (!sections || !sidebar) return;

sections.forEach(section => {
const div = document.createElement('div');

const { id } = section;
const { innerText } = section.querySelector('h2');

const b = document.createElement('b');
b.innerHTML = `<a href=${id}>${innerText}</a>`;
div.appendChild(b);

const sectionCard = section.querySelectorAll('.card');
sectionCard.forEach(card => {
const { id, title } = card;
const a = document.createElement('a');
a.href = id;
a.innerText = title ? title : id.replace(/([A-Z])/g, ' $1').trim();
div.appendChild(a);
});
};
};


function markActivePage() {
const leftSidebar = document.querySelector(".sidebar.left-sidebar");

if (!leftSidebar)
return;

const sidebarLinks = leftSidebar.querySelectorAll(".sidebar a");

if (!sidebarLinks)
return;

const currentPage = "./" + window.location.pathname.split("/").pop();

let currentIndex = -1;
sidebar.appendChild(div);
});
}

// Loop through the links to find the current page index
sidebarLinks.forEach((link, index) => {
const linkPage = link.getAttribute("href");
const lsBar = () => document.querySelector('.left-sidebar');
const currentPathname = window.location.pathname;

if (linkPage === currentPage) {
link.classList.add("active");
currentIndex = index;
function linkUtil() {
const page = {};
const navLinksNode = lsBar().querySelectorAll('a');
navLinksNode.forEach((node, index) => {
const { href } = node;

if (link.classList.contains("sublink")){
link.setAttribute('style', 'display:flex !important');
}
if (href.endsWith(currentPathname)) {
page.current = node;
page.before = navLinksNode[index - 1];
page.after = navLinksNode[index + 1];
}
});
return page;
}

const allowedPageLinks = leftSidebar.querySelectorAll(".pageLinks");
if (allowedPageLinks) {
let linkCount = 0;

for (let i = 0; i < allowedPageLinks.length; i++) {
linkCount += allowedPageLinks[i].children.length;
}

if (currentIndex > linkCount - 1)
currentIndex = -1;

createPageArrows(currentIndex);
function markActivePage() {
const currentNode = linkUtil().current;
const { href, classList } = currentNode;
classList.toggle('active', href.endsWith(currentPathname));
if (classList.contains('sublink')) {
currentNode.setAttribute('style', 'display:flex !important');
}
}

Expand Down