diff --git a/.env b/.env index 38775f9a..8204e5c3 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ +API_KEY_PLAUSIBLE= API_KEY_TRAKT= API_KEY_WEBMENTIONS_CORYD_DEV= API_BEARER_APPLE_MUSIC= diff --git a/config/filters.js b/config/filters.js index 47a5b5be..ddca9c3b 100644 --- a/config/filters.js +++ b/config/filters.js @@ -15,6 +15,16 @@ module.exports = { if (image && image !== '') return image return '/assets/img/social-card.webp' }, + getPopularPosts: (posts, analytics) => { + return posts + .filter((post) => { + if (analytics.find((p) => p.page === post.url)) return true + }) + .sort((a, b) => { + const visitors = (page) => analytics.filter((p) => p.page === page.url)[0].visitors + return visitors(b) - visitors(a) + }) + }, webmentionsByUrl: (webmentions, url) => { const allowedTypes = ['mention-of', 'in-reply-to', 'like-of', 'repost-of'] diff --git a/src/404.md b/src/404.md index ba53cccc..8f3fbd98 100644 --- a/src/404.md +++ b/src/404.md @@ -20,3 +20,5 @@ permalink: 404.html +
+{% include "popular-posts.liquid" %} diff --git a/src/_data/analytics.js b/src/_data/analytics.js new file mode 100644 index 00000000..200e3da1 --- /dev/null +++ b/src/_data/analytics.js @@ -0,0 +1,18 @@ +const EleventyFetch = require('@11ty/eleventy-fetch') + +module.exports = async function () { + const API_KEY_PLAUSIBLE = process.env.API_KEY_PLAUSIBLE + const url = + 'https://plausible.io/api/v1/stats/breakdown?site_id=coryd.dev&period=6mo&property=event:page&limit=30' + const res = EleventyFetch(url, { + duration: '1h', + type: 'json', + fetchOptions: { + headers: { + Authorization: `Bearer ${API_KEY_PLAUSIBLE}`, + }, + }, + }).catch() + const pages = await res + return pages.results.filter((p) => p.page.includes('posts')).splice(0, 5) +} diff --git a/src/_includes/forms/search.liquid b/src/_includes/forms/search.liquid deleted file mode 100644 index 3caba2f2..00000000 --- a/src/_includes/forms/search.liquid +++ /dev/null @@ -1,26 +0,0 @@ - - - - - \ No newline at end of file diff --git a/src/_includes/popular-posts.liquid b/src/_includes/popular-posts.liquid new file mode 100644 index 00000000..b6e1d19a --- /dev/null +++ b/src/_includes/popular-posts.liquid @@ -0,0 +1,16 @@ +{% assign posts = collections.posts | getPopularPosts: analytics %} +
+

+ {% heroicon "outline" "fire" "Popular" "height=28" %} +
Popular posts
+

+ +
diff --git a/src/_includes/post.liquid b/src/_includes/post.liquid index 96e36658..1fc0066e 100644 --- a/src/_includes/post.liquid +++ b/src/_includes/post.liquid @@ -14,3 +14,4 @@ layout: main {% include "post-tags.liquid" %} {% include "webmentions.liquid" %} {% include "author.liquid" %} +{% include "popular-posts.liquid" %} \ No newline at end of file diff --git a/src/search.md b/src/search.md index 41c1d934..234b5d6a 100644 --- a/src/search.md +++ b/src/search.md @@ -17,4 +17,30 @@ meta: image: src: https://coryd.dev/assets/img/avatar.webp --- -{% include "forms/search.liquid" %} + + + + + +{% include "popular-posts.liquid" %}