Skip to content
Open
Show file tree
Hide file tree
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
36 changes: 31 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,18 @@ window.changeSemester = function (sem) {
renderPDFs();
};

function prepareSearchIndex() {
// ⚑ Bolt: Optimize search by pre-calculating the search string.
// This avoids repeatedly calling toLowerCase() during search input events.
pdfDatabase.forEach(pdf => {
const t = pdf.title || '';
const d = pdf.description || '';
const c = pdf.category || '';
const a = pdf.author || '';
pdf._searchStr = (t + ' ' + d + ' ' + c + ' ' + a).toLowerCase();
});
}

async function syncClassSwitcher() {
const classSelect = document.getElementById('classSelect');
if (!classSelect) return;
Expand Down Expand Up @@ -451,6 +463,7 @@ async function loadPDFDatabase() {

if (shouldUseCache) {
pdfDatabase = cachedData;
prepareSearchIndex();
// --- FIX: CALL THIS TO POPULATE UI ---
syncClassSwitcher();
renderSemesterTabs();
Expand All @@ -474,6 +487,8 @@ async function loadPDFDatabase() {
data: pdfDatabase
}));

prepareSearchIndex();

// --- FIX: CALL THIS TO POPULATE UI ---
syncClassSwitcher();
renderPDFs();
Expand Down Expand Up @@ -874,9 +889,17 @@ function showSkeletons() {
grid.innerHTML = skeletonHTML;
}

const HTML_ESCAPE_MAP = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#039;'
};

const escapeHtml = (text) => {
if (!text) return '';
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
return text.replace(/[&<>"']/g, (m) => HTML_ESCAPE_MAP[m]);
};

function renderPDFs() {
Expand Down Expand Up @@ -915,10 +938,13 @@ function renderPDFs() {
matchesCategory = currentCategory === 'all' || pdf.category === currentCategory;
}

const matchesSearch = pdf.title.toLowerCase().includes(searchTerm) ||
pdf.description.toLowerCase().includes(searchTerm) ||
pdf.category.toLowerCase().includes(searchTerm) ||
pdf.author.toLowerCase().includes(searchTerm);
// ⚑ Bolt: Use pre-calculated search string for performance
const matchesSearch = pdf._searchStr
? pdf._searchStr.includes(searchTerm)
: (pdf.title || '').toLowerCase().includes(searchTerm) ||
(pdf.description || '').toLowerCase().includes(searchTerm) ||
(pdf.category || '').toLowerCase().includes(searchTerm) ||
(pdf.author || '').toLowerCase().includes(searchTerm);

// Update return statement to include matchesClass
return matchesSemester && matchesClass && matchesCategory && matchesSearch;
Expand Down
Binary file removed verification.png
Binary file not shown.