Skip to content

Commit

Permalink
feat: get inspired page
Browse files Browse the repository at this point in the history
  • Loading branch information
sznowicki committed Jan 3, 2024
1 parent 913be8b commit d7e200c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 2 deletions.
11 changes: 11 additions & 0 deletions dist/static/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ body.stats {
}
}

body.inspire {
height: 100%;
display: grid;

}
.inspire-entry {
margin: 2rem 0;
width: var(--width-max-content);
box-shadow: -.5rem 0 0 1px var(--color-background), calc(-.5rem - 3px) 0 0 0
}

.jump-nav {
position: sticky;
top: 0;
Expand Down
2 changes: 1 addition & 1 deletion functions/about/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Mustache from 'mustache';
import template from './template.html';
import { getDefaultViewData } from '../../lib/view.js';
import {emitPageView} from '../../lib/plausible.js';
import {renderHtml} from "../../lib/sso-render.js";
import {renderHtml} from '../../lib/sso-render.js';

export const onRequestGet = async (context) => {
emitPageView(context);
Expand Down
1 change: 1 addition & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const onRequestGet = async (context) => {
title: 'kukei.eu',
results,
hasQuery,
noQuery: !hasQuery,
mainClass,
noResults: !hasResults,
hasResults,
Expand Down
29 changes: 29 additions & 0 deletions functions/inspire/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Mustache from 'mustache';
import template from './template.html';
import { getDefaultViewData } from '../../lib/view.js';
import {emitPageView} from '../../lib/plausible.js';
import {renderHtml} from '../../lib/sso-render.js';
import {getRandomBlog} from "../../lib/search.js";

export const onRequestGet = async (context) => {
emitPageView(context);
const { env } = context;

const entry = await getRandomBlog(env);

console.log(entry);
const viewDefaults = await getDefaultViewData(env);
const view = {
...viewDefaults,
mainClass: 'body inspire',
title: 'About kukei.eu',
entries: [entry],
};
const html = await renderHtml(template, view);

return new Response(html, {
headers: {
'content-type': 'text/html',
},
});
};
18 changes: 18 additions & 0 deletions functions/inspire/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{{ before }}}
<main>
<h1><a href="/">Kukei.eu <br/><span>get inspired</span></a></h1>
<p>Every page load shows one random blog link. <br/> Have fun! Be inspired!</p>
<div class="inspire-entry">
{{#entries}}
<h4>{{ title }}</h4>
{{#excerpt}}
<p>{{ excerpt }}</p>
{{/excerpt}}
{{#excerptSafe}}
<p>{{{ excerptSafe }}}</p>
{{/excerptSafe}}
<a href="{{ url }}" target="_blank" rel="noopener">{{ url }}</a>
{{/entries}}
</div>
</main>
{{{ after }}}
2 changes: 1 addition & 1 deletion functions/status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import template from './template.html';
import { getDefaultViewData } from '../../lib/view.js';
import {emitPageView} from '../../lib/plausible.js';
import {getUnchecked, getIndexStats, getCrawlHistory} from '../../lib/mongo.js';
import {renderHtml} from "../../lib/sso-render.js";
import {renderHtml} from '../../lib/sso-render.js';

export const onRequestGet = async (context) => {
emitPageView(context);
Expand Down
3 changes: 3 additions & 0 deletions functions/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ <h1>
<input type="search" name="q" value="{{ q }}" aria-label="Search" placeholder="e.g.: snowflakes lang:en" autofocus />
<button type="submit">Search</button>
</div>
{{#noQuery}}
<p>or <a href="/inspire">get inspired</a></p>
{{/noQuery}}
{{#hasQuery}}
<div class="results">
<h2>Results <span>generated in {{ doneIn }}ms</span></h2>
Expand Down
13 changes: 13 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,16 @@ export const getFacets = async (env) => {
lang,
};
};

export const getRandomBlog = async (env) => {
const client = await getMeiliClient(env);
const { numberOfDocuments: blogPages} = await getMeiliClient(env).index('kukei-blogs').getStats();
// Random offset between 0 and blogPages
const offset = Math.floor(Math.random() * blogPages);
const { results } = await client.index('kukei-blogs').getDocuments({
offset,
limit: 1,
});

return results?.[0] ?? null;
};

0 comments on commit d7e200c

Please sign in to comment.