let widgetsInitialized = new Set(); function initializeGitHubWidgets() { const widgets = document.querySelectorAll('.github-widget'); widgets.forEach(widget => { // Skip if already initialized const widgetId = widget.dataset.repo + '-' + Math.random().toString(36).substr(2, 9); if (widget.dataset.initialized) return; widget.dataset.initialized = 'true'; const repo = widget.dataset.repo; // Auto-generate data file path from repo name const dataFile = `/assets/repo-data/${repo.replace('/', '-')}.json`; const showDescription = widget.dataset.showDescription !== 'false'; const showLanguage = widget.dataset.showLanguage !== 'false'; const showLastUpdate = widget.dataset.showLastUpdate !== 'false'; // Show loading state widget.innerHTML = `
Loading ${repo}...
`; // Fetch repository data from pre-generated JSON file fetch(dataFile) .then(response => { if (!response.ok) { throw new Error(`Could not load data for ${repo}`); } return response.json(); }) .then(data => { const lastUpdate = new Date(data.updated_at).toLocaleDateString(); const language = data.language || 'Not specified'; widget.innerHTML = `
${data.stars_count.toLocaleString()} ${data.forks_count.toLocaleString()} ${data.open_issues_count.toLocaleString()}
${showDescription && data.description ? `
${data.description}
` : ''}
`; }) .catch(error => { console.error('Error loading repository data:', error); widget.innerHTML = `
Failed to load ${repo} ${error.message}
`; }); }); } // Initialize on DOM ready document.addEventListener('DOMContentLoaded', initializeGitHubWidgets); // Watch for DOM changes (MkDocs Material dynamic content) const observer = new MutationObserver((mutations) => { let shouldReinitialize = false; mutations.forEach((mutation) => { if (mutation.type === 'childList') { // Check if any github-widget elements were added mutation.addedNodes.forEach((node) => { if (node.nodeType === 1 && // Element node (node.classList?.contains('github-widget') || node.querySelector?.('.github-widget'))) { shouldReinitialize = true; } }); } }); if (shouldReinitialize) { // Small delay to ensure DOM is stable setTimeout(initializeGitHubWidgets, 100); } }); // Start observing observer.observe(document.body, { childList: true, subtree: true }); // Language color mapping (simplified version) function getLanguageColor(language) { const colors = { 'JavaScript': '#f1e05a', 'TypeScript': '#2b7489', 'Python': '#3572A5', 'Java': '#b07219', 'C++': '#f34b7d', 'C': '#555555', 'C#': '#239120', 'PHP': '#4F5D95', 'Ruby': '#701516', 'Go': '#00ADD8', 'Rust': '#dea584', 'Swift': '#ffac45', 'Kotlin': '#F18E33', 'Scala': '#c22d40', 'Shell': '#89e051', 'HTML': '#e34c26', 'CSS': '#563d7c', 'Vue': '#2c3e50', 'React': '#61dafb', 'Dockerfile': '#384d54' }; return colors[language] || '#586069'; }