|
371 | 371 | animation: spin 1s ease-in-out infinite; |
372 | 372 | } |
373 | 373 |
|
| 374 | + .loading-text { |
| 375 | + font-size: 1.5rem; |
| 376 | + opacity: 0.8; |
| 377 | + animation: pulse 2s ease-in-out infinite; |
| 378 | + } |
| 379 | + |
374 | 380 | @keyframes spin { |
375 | 381 | to { transform: rotate(360deg); } |
376 | 382 | } |
377 | 383 |
|
| 384 | + @keyframes pulse { |
| 385 | + 0%, 100% { opacity: 0.8; } |
| 386 | + 50% { opacity: 0.4; } |
| 387 | + } |
| 388 | + |
378 | 389 | /* Utilities */ |
379 | 390 | .sr-only { |
380 | 391 | position: absolute; |
@@ -691,11 +702,195 @@ <h3>Resources</h3> |
691 | 702 | }); |
692 | 703 | }); |
693 | 704 |
|
694 | | - // Dynamic stats loading (placeholder) |
695 | | - function updateStats() { |
696 | | - // This could fetch real data from GitHub API |
697 | | - document.getElementById('total-snippets').textContent = '950+'; |
698 | | - document.getElementById('total-contributors').textContent = 'Open Source'; |
| 705 | + // GitHub API configuration |
| 706 | + const GITHUB_API_BASE = 'https://api.github.com'; |
| 707 | + const REPO = 'ServiceNowDevProgram/code-snippets'; |
| 708 | + const BRANCH = 'main'; |
| 709 | + |
| 710 | + // Folders to exclude from counting |
| 711 | + const EXCLUDED_FOLDERS = [ |
| 712 | + '.github', |
| 713 | + '.git', |
| 714 | + 'node_modules', |
| 715 | + '.vscode', |
| 716 | + '.idea', |
| 717 | + 'assets' |
| 718 | + ]; |
| 719 | + |
| 720 | + // Root files to exclude (these are typically config/documentation files) |
| 721 | + const EXCLUDED_ROOT_FILES = [ |
| 722 | + 'README.md', |
| 723 | + 'CONTRIBUTING.md', |
| 724 | + 'CLAUDE.md', |
| 725 | + 'PAGES.md', |
| 726 | + 'LICENSE', |
| 727 | + '.gitignore', |
| 728 | + 'package.json', |
| 729 | + 'package-lock.json', |
| 730 | + '_config.yml', |
| 731 | + 'sitemap.xml', |
| 732 | + 'index.html', |
| 733 | + 'core-apis.html', |
| 734 | + 'server-side-components.html', |
| 735 | + 'client-side-components.html', |
| 736 | + 'modern-development.html', |
| 737 | + 'integration.html', |
| 738 | + 'specialized-areas.html' |
| 739 | + ]; |
| 740 | + |
| 741 | + // Function to fetch directory contents from GitHub API |
| 742 | + async function fetchGitHubDirectory(path) { |
| 743 | + try { |
| 744 | + const encodedPath = path ? path.split('/').map(encodeURIComponent).join('/') : ''; |
| 745 | + const url = path ? |
| 746 | + `${GITHUB_API_BASE}/repos/${REPO}/contents/${encodedPath}?ref=${BRANCH}` : |
| 747 | + `${GITHUB_API_BASE}/repos/${REPO}/contents?ref=${BRANCH}`; |
| 748 | + |
| 749 | + const response = await fetch(url); |
| 750 | + if (!response.ok) { |
| 751 | + throw new Error(`HTTP ${response.status}: ${response.statusText}`); |
| 752 | + } |
| 753 | + return await response.json(); |
| 754 | + } catch (error) { |
| 755 | + console.error('Error fetching directory:', path, error); |
| 756 | + return []; |
| 757 | + } |
| 758 | + } |
| 759 | + |
| 760 | + // Check if a folder should be excluded |
| 761 | + function shouldExcludeFolder(folderName, isRoot = false) { |
| 762 | + return EXCLUDED_FOLDERS.includes(folderName); |
| 763 | + } |
| 764 | + |
| 765 | + // Check if a file should be counted |
| 766 | + function shouldCountFile(fileName, isRoot = false) { |
| 767 | + // Exclude root-level config files |
| 768 | + if (isRoot && EXCLUDED_ROOT_FILES.includes(fileName)) { |
| 769 | + return false; |
| 770 | + } |
| 771 | + |
| 772 | + // Count all code and content files |
| 773 | + return fileName.match(/\.(js|ts|json|html|css|py|java|c|cpp|cs|php|rb|go|rs|swift|kt|md|txt|xml|sql|sh|bat|ps1|yml|yaml)$/i); |
| 774 | + } |
| 775 | + |
| 776 | + // Recursively count all relevant files |
| 777 | + async function countAllFiles(path = '', isRoot = true) { |
| 778 | + const contents = await fetchGitHubDirectory(path); |
| 779 | + let count = 0; |
| 780 | + |
| 781 | + for (const item of contents) { |
| 782 | + if (item.type === 'file') { |
| 783 | + if (shouldCountFile(item.name, isRoot)) { |
| 784 | + count++; |
| 785 | + if (isRoot) { |
| 786 | + console.log(`Root file counted: ${item.name}`); |
| 787 | + } |
| 788 | + } |
| 789 | + } else if (item.type === 'dir') { |
| 790 | + if (!shouldExcludeFolder(item.name, isRoot)) { |
| 791 | + const subPath = path ? `${path}/${item.name}` : item.name; |
| 792 | + const subCount = await countAllFiles(subPath, false); |
| 793 | + count += subCount; |
| 794 | + console.log(`${item.name}: ${subCount} files`); |
| 795 | + } else { |
| 796 | + console.log(`Excluded folder: ${item.name}`); |
| 797 | + } |
| 798 | + } |
| 799 | + } |
| 800 | + |
| 801 | + return count; |
| 802 | + } |
| 803 | + |
| 804 | + // Fetch contributor count from GitHub API |
| 805 | + async function getContributorCount() { |
| 806 | + try { |
| 807 | + const response = await fetch(`${GITHUB_API_BASE}/repos/${REPO}/contributors`); |
| 808 | + |
| 809 | + if (!response.ok) { |
| 810 | + throw new Error(`Contributors API failed: ${response.status}`); |
| 811 | + } |
| 812 | + |
| 813 | + const contributors = await response.json(); |
| 814 | + return contributors.length; |
| 815 | + |
| 816 | + } catch (error) { |
| 817 | + console.error('Error fetching contributors:', error); |
| 818 | + return null; |
| 819 | + } |
| 820 | + } |
| 821 | + |
| 822 | + // Use GitHub's search API to count files more efficiently |
| 823 | + async function countFilesWithSearch() { |
| 824 | + try { |
| 825 | + // Search for files in the repo excluding common config/system files |
| 826 | + const searchQuery = `repo:${REPO} -path:.github -path:assets -filename:README.md -filename:CONTRIBUTING.md -filename:CLAUDE.md -filename:PAGES.md -filename:LICENSE -filename:.gitignore -filename:package.json -filename:_config.yml -filename:sitemap.xml -filename:index.html -filename:core-apis.html -filename:server-side-components.html -filename:client-side-components.html -filename:modern-development.html -filename:integration.html -filename:specialized-areas.html`; |
| 827 | + |
| 828 | + const response = await fetch(`https://api.github.com/search/code?q=${encodeURIComponent(searchQuery)}&per_page=1`); |
| 829 | + |
| 830 | + if (!response.ok) { |
| 831 | + throw new Error(`Search API failed: ${response.status}`); |
| 832 | + } |
| 833 | + |
| 834 | + const data = await response.json(); |
| 835 | + return data.total_count; |
| 836 | + |
| 837 | + } catch (error) { |
| 838 | + console.error('Search API failed, falling back to manual count:', error); |
| 839 | + return await countAllFiles(); |
| 840 | + } |
| 841 | + } |
| 842 | + |
| 843 | + // Dynamic stats loading with realistic numbers |
| 844 | + async function updateStats() { |
| 845 | + const snippetsElement = document.getElementById('total-snippets'); |
| 846 | + const contributorsElement = document.getElementById('total-contributors'); |
| 847 | + |
| 848 | + // Show loading state briefly for visual effect |
| 849 | + snippetsElement.innerHTML = '<span class="loading-text">Counting...</span>'; |
| 850 | + contributorsElement.innerHTML = '<span class="loading-text">Loading...</span>'; |
| 851 | + |
| 852 | + // Simulate loading time |
| 853 | + setTimeout(async () => { |
| 854 | + try { |
| 855 | + console.log('Fetching repository statistics...'); |
| 856 | + |
| 857 | + // Fetch both stats in parallel |
| 858 | + const [fileCount, contributorCount] = await Promise.all([ |
| 859 | + countFilesWithSearch(), |
| 860 | + getContributorCount() |
| 861 | + ]); |
| 862 | + |
| 863 | + console.log(`Files found via search: ${fileCount}`); |
| 864 | + console.log(`Contributors found: ${contributorCount}`); |
| 865 | + |
| 866 | + // Handle file count |
| 867 | + let totalFiles = fileCount; |
| 868 | + if (totalFiles === 0 || totalFiles < 100) { |
| 869 | + console.log('Search API unavailable or returned low count, using estimate'); |
| 870 | + totalFiles = "1900+"; // Rounded count from 1984 local files |
| 871 | + } |
| 872 | + |
| 873 | + // Handle contributor count |
| 874 | + let contributorDisplay = '240+'; |
| 875 | + if (contributorCount !== null && contributorCount > 0) { |
| 876 | + contributorDisplay = contributorCount.toString(); |
| 877 | + } else { |
| 878 | + console.log('Contributors API unavailable, using fallback'); |
| 879 | + } |
| 880 | + |
| 881 | + // Update the display with counts |
| 882 | + snippetsElement.textContent = totalFiles; |
| 883 | + contributorsElement.textContent = contributorDisplay; |
| 884 | + |
| 885 | + console.log(`Stats displayed - Files: ${totalFiles}, Contributors: ${contributorDisplay}`); |
| 886 | + |
| 887 | + } catch (error) { |
| 888 | + console.error('Error fetching stats:', error); |
| 889 | + // Fallback to estimated numbers |
| 890 | + snippetsElement.textContent = '1900+'; |
| 891 | + contributorsElement.textContent = '240+'; |
| 892 | + } |
| 893 | + }, 800); // Small delay for better UX |
699 | 894 | } |
700 | 895 |
|
701 | 896 | // Initialize |
|
0 commit comments