Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/news/feed.json.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import LRU from "lru-cache";67import { get } from "@cocalc/server/news/get";8import getCustomize from "@cocalc/database/settings/customize";9import { slugURL } from "@cocalc/util/news";10import { NewsItem } from "@cocalc/util/types/news";11import { renderMarkdown } from "lib/news";12import { GetServerSideProps } from "next";13import IconLogo from "public/logo/icon.svg";1415const cache = new LRU<"feed", any>({ max: 10, ttl: 60 * 1000 });1617export default function RSS() {18return null;19}2021export const getServerSideProps: GetServerSideProps = async ({ res }) => {22if (!res) return { props: {} };2324try {25res.setHeader("Content-Type", "application/feed+json");26res.setHeader("Cache-Control", "public, max-age=3600");27res.write(JSON.stringify(await feed()));28res.end();29} catch (err) {30console.error(err);31res.statusCode = 500;32res.write(JSON.stringify({ error: `${err.message}` }));33res.end();34}3536return {37props: {},38};39};4041async function feed() {42const cached = cache.get("feed");43if (cached) return cached;44const data = await get();45const feed = await getFeed(data);4647cache.set("feed", feed);48return feed;49}5051function getItems(data: NewsItem[], dns): object[] {52return data.map((n) => {53const { id, text, title, date, url } = n;54const date_published = (55typeof date === "number" ? new Date(date * 1000) : date56).toISOString();57const selfURL = `https://${dns}/${slugURL(n)}`;5859return {60id,61url: selfURL,62external_url: url,63title,64content_html: renderMarkdown(text),65date_published,66};67});68}6970// This follows https://www.jsonfeed.org/version/1.1/71async function getFeed(data: NewsItem[]): Promise<object> {72const { siteName, dns } = await getCustomize();73const icon_url = IconLogo.src;74const home_page_url = `https://${dns}/news`;75const feed_url = `https://${dns}/feed.json`;7677const feed = {78version: "https://jsonfeed.org/version/1.1",79title: `${siteName} News`,80home_page_url,81description: `News about ${siteName} – also available at https://${dns}/news`,82icon: icon_url,83favicon: icon_url,84feed_url,85items: getItems(data, dns),86};8788return feed;89}909192