Skip to content

Commit 0e733e4

Browse files
committed
patch oss page script
1 parent 2e300a7 commit 0e733e4

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed

custom/layouts/_scripts/index.njk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
{{- next_js('motion.js') }}
66
{%- endif %}
77

8-
{%- if theme.scheme === 'Muse' or theme.scheme === 'Mist' %}
9-
{{- next_js('schemes/muse.js') }}
10-
{%- endif %}
11-
128
{{- next_js('next-boot.js') }}
139
{%- if theme.bookmark.enable %}
1410
{{- next_js('bookmark.js') }}

custom/layouts/_scripts/pjax.njk

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,70 @@ document.addEventListener('pjax:success', () => {
2424
.add(NexT.motion.middleWares.postList)
2525
.bootstrap();
2626
}
27-
const hasTOC = document.querySelector('.post-toc');
28-
document.querySelector('.sidebar-inner').classList.toggle('sidebar-nav-active', hasTOC);
29-
document.querySelector(hasTOC ? '.sidebar-nav-toc' : '.sidebar-nav-overview').click();
3027
NexT.utils.updateSidebarPosition();
3128
});
29+
30+
// Create a persistent cache object for OSS page repos
31+
if (!window.githubDataCache) {
32+
window.githubDataCache = {};
33+
}
34+
35+
// A hook for fetching and loading data for OSS page
36+
// Function to update project information from GitHub API
37+
function updateProjectsFromGitHub() {
38+
// Check if we're on the Open Source page
39+
if (document.title !== "Open Source") {
40+
return;
41+
}
42+
43+
const projects = document.querySelectorAll('.project');
44+
45+
projects.forEach(project => {
46+
const githubUrl = project.getAttribute('data-repo');
47+
if (!githubUrl) return;
48+
49+
// Construct the GitHub API URL
50+
const apiUrl = githubUrl.replace('https://github.com/', 'https://api.github.com/repos/');
51+
52+
// Use project name or the full URL as a cache key
53+
const cacheKey = githubUrl;
54+
55+
if (window.githubDataCache[cacheKey]) {
56+
// Use cached data if available
57+
updateProjectDOM(project, window.githubDataCache[cacheKey]);
58+
} else {
59+
// Fetch from API if not in cache
60+
fetch(apiUrl)
61+
.then(response => response.json())
62+
.then(data => {
63+
// Store in cache
64+
window.githubDataCache[cacheKey] = data;
65+
// Update DOM
66+
updateProjectDOM(project, data);
67+
})
68+
.catch(error => console.error('Error fetching GitHub data:', error));
69+
}
70+
});
71+
}
72+
73+
// Helper function to update the DOM for a project
74+
function updateProjectDOM(project, data) {
75+
// Use project name to generate IDs
76+
const idPrefix = data.name.toLowerCase().replace(/ /g, '-');
77+
78+
const languageElement = document.getElementById(`${idPrefix}-language`);
79+
const starsElement = document.getElementById(`${idPrefix}-stars`);
80+
const forksElement = document.getElementById(`${idPrefix}-forks`);
81+
82+
// Only update if elements exist
83+
if (languageElement) languageElement.textContent = data.language || 'N/A';
84+
if (starsElement) starsElement.textContent = data.stargazers_count || 0;
85+
if (forksElement) forksElement.textContent = data.forks_count || 0;
86+
}
87+
88+
// Hook into pjax:success event
89+
document.addEventListener('pjax:success', updateProjectsFromGitHub);
90+
91+
// Also run on initial page load to handle direct navigation
92+
updateProjectsFromGitHub();
3293
</script>

custom/layouts/open-source.njk

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,4 @@
145145
</section>
146146
{% endfor %}
147147

148-
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script> <!-- FontAwesome Kit -->
149-
<script>
150-
document.addEventListener('DOMContentLoaded', function () {
151-
const projects = document.querySelectorAll('.project');
152-
153-
projects.forEach(project => {
154-
const githubUrl = project.getAttribute('data-repo');
155-
if (githubUrl) {
156-
// Construct the GitHub API URL by inserting "api." after "https://"
157-
const apiUrl = githubUrl.replace('https://github.com/', 'https://api.github.com/repos/');
158-
159-
fetch(apiUrl)
160-
.then(response => response.json())
161-
.then(data => {
162-
// Use project name to generate IDs
163-
const idPrefix = data.name.toLowerCase().replace(/ /g, '-');
164-
165-
document.getElementById(`${idPrefix}-language`).textContent = data.language || 'N/A';
166-
document.getElementById(`${idPrefix}-stars`).textContent = data.stargazers_count || 0;
167-
document.getElementById(`${idPrefix}-forks`).textContent = data.forks_count || 0;
168-
})
169-
.catch(error => console.error('Error fetching GitHub data:', error));
170-
}
171-
});
172-
});
173-
</script>
174148
{% endblock %}

0 commit comments

Comments
 (0)