Skip to content

Commit b60e8ba

Browse files
authored
Update index.html
1 parent 71f56e3 commit b60e8ba

File tree

1 file changed

+70
-30
lines changed

1 file changed

+70
-30
lines changed

index.html

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,8 @@ <h2 id="problems-title"></h2>
633633
for (const platform of ['leetcode', 'kattis']) {
634634
const problems = problemsData[platform];
635635
const totalProblems = problems.length;
636-
const withPages = problems.filter(p => p.hasPage).length;
637636

638-
document.getElementById(`${platform}-count`).textContent =
639-
`${totalProblems} problems, ${withPages} with pages`;
637+
document.getElementById(`${platform}-count`).textContent = `${totalProblems} problems`;
640638
}
641639
}
642640

@@ -694,6 +692,21 @@ <h2 id="problems-title"></h2>
694692
}
695693

696694
async function generatePage(platform, problem) {
695+
// Instead of opening a new window, we'll create a URL hash-based system
696+
// This allows for shareable links
697+
const problemId = encodeURIComponent(`${platform}/${problem.name}`);
698+
const newUrl = `${window.location.origin}${window.location.pathname}#view/${problemId}`;
699+
window.location.href = newUrl;
700+
}
701+
702+
async function renderProblemPage(platform, problemName) {
703+
// Find the problem data
704+
const problem = problemsData[platform]?.find(p => p.name === problemName);
705+
if (!problem) {
706+
document.body.innerHTML = '<div class="error">Problem not found</div>';
707+
return;
708+
}
709+
697710
// Create a new page content
698711
let problemUrl = '';
699712
let pythonCode = '';
@@ -722,13 +735,19 @@ <h2 id="problems-title"></h2>
722735
const pageTitle = problem.displayName.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
723736
const platformTitle = platform.charAt(0).toUpperCase() + platform.slice(1);
724737

738+
// Get GitHub repo URL for the folder
739+
const repoUrl = `https://github.com/${getRepoPath()}/tree/main/${platform}/${problem.name}`;
740+
725741
let pageContent = `
726742
<div class="generated-page">
727743
<div class="nav-buttons">
728744
<a href="javascript:history.back()" class="nav-btn">← Back</a>
729-
<a href="../../index.html" class="nav-btn">🏠 Home</a>
745+
<a href="${window.location.pathname}" class="nav-btn">🏠 Home</a>
730746
${problemUrl ? `<a href="${problemUrl}" target="_blank" class="nav-btn">🔗 Problem</a>` : ''}
731-
${problem.mainPyFile ? `<a href="${platform}/${problem.name}/${problem.mainPyFile.name}" target="_blank" class="nav-btn">💻 Code</a>` : ''}
747+
<a href="${repoUrl}" target="_blank" class="nav-btn">
748+
<img src="VN.ico" alt="GitHub" style="width: 16px; height: 16px; margin-right: 4px;">
749+
Code
750+
</a>
732751
</div>
733752
734753
<h1 class="page-title">${pageTitle}</h1>
@@ -756,8 +775,8 @@ <h1 class="page-title">${pageTitle}</h1>
756775
pageContent += `</div>`;
757776
}
758777

759-
// Add code section if no images
760-
if (!problem.hasPng && pythonCode) {
778+
// Add code section (always show if available, even with images)
779+
if (pythonCode) {
761780
pageContent += `
762781
<div class="code-section">
763782
<h3>Python Solution</h3>
@@ -768,23 +787,19 @@ <h3>Python Solution</h3>
768787

769788
pageContent += `</div>`;
770789

771-
// Create a new document and write the content
772-
const newWindow = window.open('', '_blank');
773-
newWindow.document.write(`
774-
<!DOCTYPE html>
775-
<html lang="en">
776-
<head>
777-
<meta charset="UTF-8">
778-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
779-
<title>${pageTitle} - ${platformTitle}</title>
780-
<style>${document.querySelector('style').innerHTML}</style>
781-
</head>
782-
<body>
783-
${pageContent}
784-
</body>
785-
</html>
786-
`);
787-
newWindow.document.close();
790+
// Replace the body content with the generated page
791+
document.body.innerHTML = pageContent;
792+
793+
// Update page title and favicon
794+
document.title = `${pageTitle} - ${platformTitle}`;
795+
796+
// Add favicon if not already present
797+
if (!document.querySelector('link[rel="icon"]')) {
798+
const link = document.createElement('link');
799+
link.rel = 'icon';
800+
link.href = 'VN.ico';
801+
document.head.appendChild(link);
802+
}
788803
}
789804

790805
function escapeHtml(text) {
@@ -855,16 +870,41 @@ <h3>Python Solution</h3>
855870
if (window.location.pathname.endsWith('index.html') ||
856871
window.location.pathname.endsWith('/') ||
857872
window.location.pathname.split('/').pop() === '') {
858-
scanForProblems();
873+
874+
// Handle hash navigation for problem views
875+
const hash = window.location.hash;
876+
if (hash.startsWith('#view/')) {
877+
const problemPath = decodeURIComponent(hash.substring(6));
878+
const [platform, problemName] = problemPath.split('/');
879+
880+
// Load problems data first, then render the problem page
881+
scanForProblems().then(() => {
882+
renderProblemPage(platform, problemName);
883+
});
884+
} else {
885+
scanForProblems();
886+
887+
// Handle platform navigation
888+
window.addEventListener('load', () => {
889+
const simpleHash = window.location.hash.substring(1);
890+
if (simpleHash === 'leetcode' || simpleHash === 'kattis') {
891+
setTimeout(() => showProblems(simpleHash), 1000);
892+
}
893+
});
894+
}
859895
} else {
860896
addNavigationToCurrentPage();
861897
}
862898

863-
// Handle hash navigation for direct links
864-
window.addEventListener('load', () => {
865-
const hash = window.location.hash.substring(1);
866-
if (hash === 'leetcode' || hash === 'kattis') {
867-
setTimeout(() => showProblems(hash), 1000);
899+
// Handle browser back/forward navigation
900+
window.addEventListener('hashchange', () => {
901+
const hash = window.location.hash;
902+
if (hash.startsWith('#view/')) {
903+
const problemPath = decodeURIComponent(hash.substring(6));
904+
const [platform, problemName] = problemPath.split('/');
905+
renderProblemPage(platform, problemName);
906+
} else if (hash === '') {
907+
location.reload(); // Reload to show main page
868908
}
869909
});
870910
</script>

0 commit comments

Comments
 (0)