From 5d6338ee344ccc1d986d059f9e41c8b9e6ec0c33 Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 26 Jul 2026 21:38:01 +0800 Subject: [PATCH] feat(route): add Forward Future routes (daily newsletter + originals) (#22827) * feat: add Forward Future routes (daily newsletter + originals) * feat(route): add lang * feat: add cache.tryGet to Forward Future routes * fix: address review feedback - remove cache.tryGet from list requests, use JSON.parse for unescaping, fix description/pubDate/extraction --- lib/routes/forwardfuture/daily.ts | 58 ++++++++++++ lib/routes/forwardfuture/namespace.ts | 7 ++ lib/routes/forwardfuture/originals.ts | 122 ++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 lib/routes/forwardfuture/daily.ts create mode 100644 lib/routes/forwardfuture/namespace.ts create mode 100644 lib/routes/forwardfuture/originals.ts diff --git a/lib/routes/forwardfuture/daily.ts b/lib/routes/forwardfuture/daily.ts new file mode 100644 index 000000000..a4449367d --- /dev/null +++ b/lib/routes/forwardfuture/daily.ts @@ -0,0 +1,58 @@ +import type { Context } from 'hono'; + +import type { Data, DataItem, Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +interface NewsletterPost { + title: string; + summary: string; + url: string; + date: string; + thumbnail: string; + bodyPreview: string; + author: string; +} + +interface NewsletterResponse { + posts: NewsletterPost[]; +} + +export const route: Route = { + name: 'Daily Newsletter', + categories: ['other'], + path: '/daily', + example: '/forwardfuture/daily', + radar: [ + { + source: ['forwardfuture.com/newsletter/daily', 'forwardfuture.com/'], + }, + ], + handler, + maintainers: ['ovo-Tim'], + description: 'Daily AI newsletter from Forward Future.', +}; + +async function handler(ctx: Context): Promise { + const limit = ctx.req.query('limit') ? Number(ctx.req.query('limit')) : 30; + + const response = await ofetch('https://forwardfuture.com/api/newsletter'); + const posts = response.posts.slice(0, limit); + + const items: DataItem[] = posts.map((post) => ({ + title: post.title, + description: post.summary, + link: `https://forwardfuture.com${post.url}`, + pubDate: parseDate(post.date, 'MMM D, YYYY'), + author: post.author, + image: post.thumbnail, + })); + + return { + title: 'Forward Future - Daily Newsletter', + link: 'https://forwardfuture.com/newsletter/daily', + description: "The Future Today - Forward Future's free daily AI newsletter.", + item: items, + image: 'https://forwardfuture.com/images/logos/ff-icon.svg', + }; +} diff --git a/lib/routes/forwardfuture/namespace.ts b/lib/routes/forwardfuture/namespace.ts new file mode 100644 index 000000000..42f373933 --- /dev/null +++ b/lib/routes/forwardfuture/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Forward Future', + url: 'forwardfuture.com', + lang: 'en', +}; diff --git a/lib/routes/forwardfuture/originals.ts b/lib/routes/forwardfuture/originals.ts new file mode 100644 index 000000000..0cb11ebc8 --- /dev/null +++ b/lib/routes/forwardfuture/originals.ts @@ -0,0 +1,122 @@ +import type { Context } from 'hono'; + +import type { Data, DataItem, Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +interface OriginalPost { + id: string; + title: string; + summary: string; + url: string; + date: string; + dateUnix: number; + thumbnail: string; + authors: string[]; + category: string; + categories: string[]; +} + +function extractPostsArray(rscPayload: string): OriginalPost[] | null { + let searchStart = 0; + while (searchStart < rscPayload.length) { + const postsIdx = rscPayload.indexOf('"posts":', searchStart); + if (postsIdx === -1) { + return null; + } + + const arrayStart = postsIdx + '"posts":'.length; + if (rscPayload[arrayStart] !== '[') { + searchStart = arrayStart; + continue; + } + + let depth = 0; + let inString = false; + let escape = false; + let i = arrayStart; + for (; i < rscPayload.length; i++) { + const c = rscPayload[i]; + if (escape) { + escape = false; + } else if (c === '\\' && inString) { + escape = true; + } else if (c === '"' && !escape) { + inString = !inString; + } else if (!inString) { + if (c === '[') { + depth++; + } else if (c === ']') { + depth--; + if (depth === 0) { + return JSON.parse(rscPayload.slice(arrayStart, i + 1)) as OriginalPost[]; + } + } + } + } + + searchStart = arrayStart + 1; + } + return null; +} + +export const route: Route = { + name: 'Originals', + categories: ['other'], + path: '/originals', + example: '/forwardfuture/originals', + radar: [ + { + source: ['forwardfuture.com/originals', 'forwardfuture.com/'], + }, + ], + handler, + maintainers: ['ovo-Tim'], + description: 'Original essays, columns, and analysis on AI from Forward Future contributors.', +}; + +async function handler(ctx: Context): Promise { + const limit = ctx.req.query('limit') ? Number(ctx.req.query('limit')) : 30; + + const html = await ofetch('https://forwardfuture.com/originals'); + + const pushRegex = /__next_f\.push\(\[1,"((?:[^"\\]|\\.)*)"\]\)/g; + let match: RegExpExecArray | null; + let posts: OriginalPost[] = []; + + while ((match = pushRegex.exec(html)) !== null) { + const payload = match[1]; + if (!payload.includes('posts')) { + continue; + } + + const decoded = JSON.parse(`"${payload}"`); + const extracted = extractPostsArray(decoded); + if (extracted) { + posts = extracted; + break; + } + } + + if (posts.length === 0) { + throw new Error('Failed to extract posts from the Next.js RSC payload'); + } + + const items: DataItem[] = posts.slice(0, limit).map((post) => ({ + title: post.title, + description: post.summary || undefined, + link: post.url, + pubDate: parseDate(post.dateUnix, 'X'), + author: post.authors.join(', '), + image: post.thumbnail, + category: post.categories.length > 0 ? post.categories : post.category === 'General' ? undefined : [post.category], + })); + + return { + title: 'Forward Future - Originals', + link: 'https://forwardfuture.com/originals', + description: 'Original essays, columns, and analysis on AI from Forward Future contributors.', + item: items, + image: 'https://forwardfuture.com/images/logos/ff-icon.svg', + }; +}