Skip to content

Commit

Permalink
feat: add magazines index, order indexes, format stats number
Browse files Browse the repository at this point in the history
  • Loading branch information
sznowicki committed Dec 9, 2023
1 parent a00730d commit 5c275da
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
19 changes: 19 additions & 0 deletions dist/static/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class FormatNumber extends HTMLElement {
connectedCallback() {
this.innerHTML = this.formatNumber(this.innerHTML);
}

formatNumber(number) {
const parsed = parseFloat(number);;
if (isNaN(parsed)) {
return number;
}
return parsed.toLocaleString();
}
}

const main = () => {
customElements.define('format-number', FormatNumber);
};

main();
16 changes: 13 additions & 3 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ export const onRequestGet = async (context) => {
const [
blogPages,
docsPages,
magazinesPages,
] = await stats(env);
const doneIn = Date.now() - startTime;
const hasBlogs = result?.hits.blogs.length > 0;
const hasDocs = result?.hits.docs.length > 0;
const hasMagazines = result?.hits.magazines.length > 0;
const results = [];
if (hasDocs) {
results.push({
name: 'Docs',
hits: result.hits.docs,
});
}
if (hasBlogs) {
results.push({
name: 'Blogs',
hits: result.hits.blogs,
});
}
if (hasDocs) {
if (hasMagazines) {
results.push({
name: 'Docs',
hits: result.hits.docs,
name: 'Magazines',
hits: result.hits.magazines,
});
}

Expand All @@ -49,6 +57,8 @@ export const onRequestGet = async (context) => {
hash,
blogPages,
docsPages,
magazinesPages,
totalPages: blogPages + docsPages + magazinesPages,
};

const html = Mustache.render(template, view);
Expand Down
4 changes: 3 additions & 1 deletion functions/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="/static/variables.css?v={{ hash }}" />
<link rel="stylesheet" href="/static/layout.css?v={{ hash }}" />
<link rel="stylesheet" href="/static/search.css?v={{ hash }}" />
<script type="module" src="/static/modules.js?v={{ hash }}"></script>
</head>
<body class="{{ mainClass }}">
<main>
Expand Down Expand Up @@ -70,7 +71,8 @@ <h4>{{ title }}</h4>
<h2 class="visually-hidden">About</h2>
<p class="about__brand">Kukey.eu - curated search for web developers</p>
<p>Super MVP, contact me on <a href="https://social.nowicki.io/@hey">Mastodon</a> or check out the <a href="https://github.com/Kukei-eu">source code</a></p>
<p>So far indexed {{ blogPages }} pages of blogs and {{ docsPages }} pages of docs. See all sources <a href="https://github.com/Kukei-eu/spider/blob/main/index-sources.json">on GitHub</a>. Edits welcomed!</p>
<p>So far indexed <format-number>{{totalPages}}</format-number> pages, including <format-number>{{ blogPages }}</format-number> pages of blogs, <format-number>{{ magazinesPages}}</format-number> pages of magazines and <format-number>{{ docsPages }}</format-number> pages of docs.</p>
<p>See all sources <a href="https://github.com/Kukei-eu/spider/blob/main/index-sources.json">on GitHub</a>. Edits welcomed!</p>
</footer>
<!-- Cloudflare Web Analytics -->
<script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "f9dd156aed044831bc76da02f87e4619"}'></script><!-- End Cloudflare Web Analytics -->
Expand Down
5 changes: 4 additions & 1 deletion lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,21 @@ export const search = async (env, q, p = 0) => {
hits: {
'blogs': [],
'docs': [],
'magazines': [],
},
};

await doSearch(result, env, 'blogs', q, p);
await doSearch(result, env, 'docs', q, p);
await doSearch(result, env, 'magazines', q, p);

return result;
};

export const stats = async (env) => {
const { numberOfDocuments: blogPages} = await getMeiliClient(env).index('blogs').getStats();
const { numberOfDocuments: docsPages} = await getMeiliClient(env).index('docs').getStats();
const { numberOfDocuments: magazinesPages} = await getMeiliClient(env).index('magazines').getStats();

return [blogPages, docsPages];
return [blogPages, docsPages, magazinesPages];
};

0 comments on commit 5c275da

Please sign in to comment.