]> git.ardis.lu - ardislu.dev/commitdiff
Add JSON Feed
authorArdis Lu <ardisglu@gmail.com>
Mon, 4 Nov 2024 04:57:11 +0000 (20:57 -0800)
committerArdis Lu <ardisglu@gmail.com>
Mon, 4 Nov 2024 04:57:11 +0000 (20:57 -0800)
functions/feed.json.js [new file with mode: 0644]
index.html

diff --git a/functions/feed.json.js b/functions/feed.json.js
new file mode 100644 (file)
index 0000000..fe6e9a0
--- /dev/null
@@ -0,0 +1,32 @@
+import { getGoogleAuthToken } from './auth.js';
+
+export async function onRequestGet({ env }) {
+  const token = await getGoogleAuthToken(env.EMAIL, env.PRIVATE_KEY, 'https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/documents.readonly');
+  const sheetUrl = 'https://sheets.googleapis.com/v4/spreadsheets/1pfGF8yBu3D0GPTezygLuzu3Cif8SkjhtG98nL-czlhc/values/B3:I';
+  const googleSheet = await fetch(sheetUrl, { headers: { Authorization: `Bearer ${token}` } }).then(r => r.json());
+  const feed = {
+    version: 'https://jsonfeed.org/version/1.1',
+    title: 'ardislu.dev',
+    home_page_url: 'https://ardislu.dev/',
+    feed_url: 'https://ardislu.dev/feed.json',
+    description: 'Notes on web development, crypto, self-hosting, and tech in general.',
+    icon: 'https://ardislu.dev/logo.svg',
+    favicon: 'https://ardislu.dev/logo.svg',
+    authors: [{ name: 'Ardis Lu', url: 'mailto:ardis@ardislu.dev' }],
+    language: 'en-US',
+    items: []
+  }
+  for (const article of googleSheet.values) {
+    feed.items.push({
+      id: `urn:google:${article[2]}`,
+      url: `https://ardislu.dev${article[4]}`,
+      title: article[0],
+      content_html: `<p>${article[1]}</p><a href="https://ardislu.dev${article[4]}">${article[3]}</a>`,
+      date_published: new Date(article[5]).toISOString(),
+      date_modified: new Date(article[6]).toISOString(),
+      tags: article[7].split(',').map(s => s.trim())
+    });
+  }
+
+  return new Response(JSON.stringify(feed), { headers: { 'Content-Type': 'application/feed+json' } });
+}
\ No newline at end of file
index 9fb43c051c1c7a7573b47f4f51b125904d90894e..f16a3fab72a45a9a72e8c982fb2c115c2c815cc6 100644 (file)
@@ -25,6 +25,7 @@
   <link rel="manifest" href="/manifest.webmanifest">
   <link rel="search" href="https://ardislu.dev/opensearch.osdx" type="application/opensearchdescription+xml" title="ardislu.dev">
   <link rel="alternate" type="application/atom+xml" title="ardislu.dev" href="/atom.xml">
+  <link rel="alternate" type="application/feed+json" title="ardislu.dev" href="/feed.json">
   <style>
     @layer fonts, base, print, hljs;
 
This page took 0.042759 seconds and 4 git commands to generate.