we'll see
This commit is contained in:
@@ -118,18 +118,12 @@ data:
|
||||
let activeIndex = 0;
|
||||
let lastQuery = '';
|
||||
|
||||
function normalize(s) {
|
||||
return (s || '').toLowerCase().replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function indexLinks() {
|
||||
const BOOKMARKS_INDEX_URL = '/assets/bookmarks.json';
|
||||
|
||||
let indexLoaded = false;
|
||||
let indexLoading = null;
|
||||
|
||||
function indexLinksFromDom() {
|
||||
// fallback: current page bookmarks only
|
||||
const anchors = document.querySelectorAll('.widget.widget-type-bookmarks a.bookmarks-link[href]');
|
||||
indexed = Array.from(anchors).map(a => ({
|
||||
title: (a.textContent || '').trim(),
|
||||
@@ -138,39 +132,41 @@ data:
|
||||
}));
|
||||
}
|
||||
|
||||
async function loadBookmarksIndex() {
|
||||
if (indexLoaded) return;
|
||||
function loadBookmarksIndex() {
|
||||
if (indexLoaded) return Promise.resolve();
|
||||
if (indexLoading) return indexLoading;
|
||||
|
||||
indexLoading = (async () => {
|
||||
try {
|
||||
const res = await fetch(BOOKMARKS_INDEX_URL, { cache: 'no-store' });
|
||||
indexLoading = fetch(BOOKMARKS_INDEX_URL, { cache: 'no-store' })
|
||||
.then(res => {
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const data = await res.json();
|
||||
|
||||
return res.json();
|
||||
})
|
||||
.then(data => {
|
||||
indexed = data.map(x => ({
|
||||
title: x.title,
|
||||
url: x.url,
|
||||
meta: [x.page, x.widget, x.group].filter(Boolean).join(' • ')
|
||||
}));
|
||||
|
||||
indexLoaded = true;
|
||||
} catch (e) {
|
||||
})
|
||||
.catch(e => {
|
||||
console.warn('Could not load bookmarks index, falling back to DOM only:', e);
|
||||
indexLinksFromDom();
|
||||
indexLoaded = true;
|
||||
}
|
||||
})();
|
||||
});
|
||||
|
||||
return indexLoading;
|
||||
}
|
||||
|
||||
// Load ASAP (works even if DOMContentLoaded already happened)
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', loadBookmarksIndex);
|
||||
document.addEventListener('DOMContentLoaded', () => loadBookmarksIndex());
|
||||
} else {
|
||||
loadBookmarksIndex();
|
||||
}
|
||||
|
||||
function normalize(s) {
|
||||
return (s || '').toLowerCase().replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function score(item, q) {
|
||||
@@ -228,8 +224,10 @@ data:
|
||||
list().innerHTML = `<div class="gql-item active"><div class="gql-title">Loading…</div></div>`;
|
||||
|
||||
// then render once index is available
|
||||
loadBookmarksIndex().then(() => render(normalize(input().value)));
|
||||
loadBookmarksIndex().then(() => {
|
||||
render(normalize(input().value));
|
||||
input().focus();
|
||||
});
|
||||
|
||||
function onInput() {
|
||||
lastQuery = input().value;
|
||||
|
||||
Reference in New Issue
Block a user