From d7e200c8edf6f69fd6a3c3f01d2d71b0ec0ce96e Mon Sep 17 00:00:00 2001 From: Szymon Nowicki Date: Wed, 3 Jan 2024 20:36:59 +0100 Subject: [PATCH] feat: get inspired page --- dist/static/layout.css | 11 +++++++++++ functions/about/index.js | 2 +- functions/index.js | 1 + functions/inspire/index.js | 29 +++++++++++++++++++++++++++++ functions/inspire/template.html | 18 ++++++++++++++++++ functions/status/index.js | 2 +- functions/template.html | 3 +++ lib/search.js | 13 +++++++++++++ 8 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 functions/inspire/index.js create mode 100644 functions/inspire/template.html diff --git a/dist/static/layout.css b/dist/static/layout.css index 0e2e28f..18eb684 100644 --- a/dist/static/layout.css +++ b/dist/static/layout.css @@ -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; diff --git a/functions/about/index.js b/functions/about/index.js index fcad243..7a161d7 100644 --- a/functions/about/index.js +++ b/functions/about/index.js @@ -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); diff --git a/functions/index.js b/functions/index.js index 6392b33..ad11248 100644 --- a/functions/index.js +++ b/functions/index.js @@ -82,6 +82,7 @@ export const onRequestGet = async (context) => { title: 'kukei.eu', results, hasQuery, + noQuery: !hasQuery, mainClass, noResults: !hasResults, hasResults, diff --git a/functions/inspire/index.js b/functions/inspire/index.js new file mode 100644 index 0000000..71aedf9 --- /dev/null +++ b/functions/inspire/index.js @@ -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', + }, + }); +}; diff --git a/functions/inspire/template.html b/functions/inspire/template.html new file mode 100644 index 0000000..3bb292c --- /dev/null +++ b/functions/inspire/template.html @@ -0,0 +1,18 @@ +{{{ before }}} +
+

Kukei.eu
get inspired

+

Every page load shows one random blog link.
Have fun! Be inspired!

+
+ {{#entries}} +

{{ title }}

+ {{#excerpt}} +

{{ excerpt }}

+ {{/excerpt}} + {{#excerptSafe}} +

{{{ excerptSafe }}}

+ {{/excerptSafe}} + {{ url }} + {{/entries}} +
+
+{{{ after }}} diff --git a/functions/status/index.js b/functions/status/index.js index fcfb10f..2a3f069 100644 --- a/functions/status/index.js +++ b/functions/status/index.js @@ -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); diff --git a/functions/template.html b/functions/template.html index acce75e..0aea83d 100644 --- a/functions/template.html +++ b/functions/template.html @@ -11,6 +11,9 @@

+ {{#noQuery}} +

or get inspired

+ {{/noQuery}} {{#hasQuery}}

Results generated in {{ doneIn }}ms

diff --git a/lib/search.js b/lib/search.js index c2a71f6..e0552ec 100644 --- a/lib/search.js +++ b/lib/search.js @@ -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; +};