@@ -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 >
0 commit comments