Skip to content

Commit 58e7efd

Browse files
authored
Use GitHub tree API to get more than first 1000 files
Also some CSS tweaks and autocomplete search now does term1.*term2
1 parent fe44eaf commit 58e7efd

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

mdn-timelines.html

+52-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
margin: 2rem 1rem;
1313
max-width: 800px;
1414
margin: 0 auto;
15+
line-height: 1.2;
1516
}
1617
#autocomplete-container {
1718
position: relative;
@@ -98,6 +99,8 @@
9899
margin-top: 0;
99100
color: #1f2937;
100101
font-size: 1.5rem;
102+
word-break: break-word;
103+
line-height: 1.4;
101104
}
102105
.api-info {
103106
margin: 1rem 0;
@@ -161,40 +164,60 @@ <h1>MDN Browser Support Timelines</h1>
161164
let allFiles = [];
162165
let selectedFiles = [];
163166

164-
async function fetchAllFiles(repo, path = 'api', page = 1, allFiles = []) {
165-
const token = '';
166-
const headers = token ? { 'Authorization': `token ${token}` } : {};
167-
167+
async function fetchAllFiles() {
168168
try {
169-
const url = `https://api.github.com/repos/${repo}/contents/${path}?page=${page}&per_page=100`;
170-
const response = await fetch(url, { headers });
169+
// First API call to get repository contents
170+
const contentsResponse = await fetch('https://api.github.com/repos/mdn/browser-compat-data/contents');
171171

172-
if (!response.ok) {
173-
throw new Error(`HTTP error! status: ${response.status}`);
172+
if (!contentsResponse.ok) {
173+
throw new Error(`HTTP error! status: ${contentsResponse.status}`);
174+
}
175+
176+
const contentsData = await contentsResponse.json();
177+
178+
// Find the object with name: "api"
179+
const apiObject = contentsData.find(item => item.name === "api");
180+
181+
if (!apiObject) {
182+
throw new Error('Could not find object with name "api"');
174183
}
175184

176-
const files = await response.json();
177-
allFiles.push(...files.map(file => file.path));
185+
// Extract the git URL from _links
186+
const gitUrl = apiObject._links?.git;
178187

179-
if (files.length === 100) {
180-
return fetchAllFiles(repo, path, page + 1, allFiles);
188+
if (!gitUrl) {
189+
throw new Error('Git URL not found in _links');
181190
}
182191

183-
return allFiles;
192+
// Second API call to fetch the git data
193+
const gitResponse = await fetch(gitUrl);
194+
195+
if (!gitResponse.ok) {
196+
throw new Error(`HTTP error! status: ${gitResponse.status}`);
197+
}
198+
199+
const gitData = await gitResponse.json();
200+
201+
// Return the tree array from the git data
202+
if (!gitData.tree) {
203+
throw new Error('Tree data not found in git response');
204+
}
205+
206+
return gitData.tree;
207+
184208
} catch (error) {
185-
console.error('Error fetching files:', error);
186-
return allFiles;
209+
console.error('Error fetching GitHub data:', error);
210+
throw error;
187211
}
188212
}
189213

190214
function updateUrlHash(filePath) {
191-
const cleanPath = filePath.replace(/^api\//, '');
192-
history.pushState(null, '', `#${cleanPath}`);
215+
history.pushState(null, '', `#${filePath}`);
193216
}
194217

195218
function getHashPath() {
196219
const hash = window.location.hash.slice(1);
197-
return hash ? `api/${hash}` : null;
220+
return hash ? hash : null;
198221
}
199222

200223
function setupAutocomplete() {
@@ -203,9 +226,16 @@ <h1>MDN Browser Support Timelines</h1>
203226

204227
searchInput.addEventListener('input', () => {
205228
const searchTerm = searchInput.value.toLowerCase();
229+
const regexPattern = searchTerm
230+
.split(' ')
231+
.filter(term => term.length > 0) // Remove empty strings from multiple spaces
232+
.map(term => term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) // Escape regex special chars
233+
.join('.*');
234+
const searchRegex = new RegExp(regexPattern);
235+
206236
const filteredFiles = allFiles.filter(file =>
207-
file.toLowerCase().includes(searchTerm)
208-
).slice(0, 20);
237+
searchRegex.test(file.toLowerCase())
238+
).slice(0, 20);
209239

210240
suggestionsContainer.innerHTML = '';
211241

@@ -337,7 +367,7 @@ <h1>MDN Browser Support Timelines</h1>
337367
async function fetchBrowserCompatData(filePath) {
338368
try {
339369
updateUrlHash(filePath);
340-
const url = `https://bcd.developer.mozilla.org/bcd/api/v0/current/${filePath.replace('/', '.')}`;
370+
const url = `https://bcd.developer.mozilla.org/bcd/api/v0/current/api.${filePath}.json`;
341371
const response = await fetch(url);
342372

343373
if (!response.ok) {
@@ -428,7 +458,7 @@ <h1>MDN Browser Support Timelines</h1>
428458

429459
// Initialize the app
430460
async function init() {
431-
allFiles = await fetchAllFiles('mdn/browser-compat-data', 'api');
461+
allFiles = (await fetchAllFiles('mdn/browser-compat-data', 'api')).map(file => file.path.split(".json")[0]);
432462
setupAutocomplete();
433463

434464
// Check for hash in URL and load that file if present

0 commit comments

Comments
 (0)