Fix blog feeds

This commit is contained in:
Jade Ellis 2024-11-24 04:36:05 +00:00
parent ea72d1ce42
commit c73b7baaf5
No known key found for this signature in database
GPG key ID: 8705A2A3EBF77BD2
6 changed files with 176 additions and 149 deletions

View file

@ -1,72 +1,13 @@
import { pages } from '../../posts'
import type Feed from '@json-feed-types/1_1'
import {
SITE_DEFAULT_DESCRIPTION,
SITE_TITLE,
SITE_URL,
RSS_DEFAULT_POSTS_PER_PAGE
} from '$lib/metadata';
import { error } from '@sveltejs/kit'
// import { base } from '$app/paths';
// export const prerender = true;
export async function GET({ params, url}) {
const dateParts = params.date.split(/[\/-]/).filter((s)=>s.length !== 0).map((p) => Number.parseInt(p, 10))
if (dateParts.length > 3) {
throw error(404, 'Feed not found (bad date)')
}
const selectedPages = dateParts.length ? pages
.filter((post) => {
const date = new Date(post.date)
return (
(!dateParts[0] || date.getFullYear() == dateParts[0]) &&
(!dateParts[1] || date.getMonth()+1 == dateParts[1]) &&
(!dateParts[2] || date.getDate() == dateParts[2])
)
}) : pages;
const headers = {
'Cache-Control': 'max-age=0, s-maxage=3600',
'Content-Type': 'application/feed+json'
};
return new Response(await getJsonFeed(url.href, selectedPages), { headers });
}
const AUTHOR = "Jade Ellis"
// prettier-ignore
async function getJsonFeed(selfUrl: string, pages: any[]): Promise<string> {
const feed: Feed = {
version: 'https://jsonfeed.org/version/1.1',
title: SITE_TITLE,
icon: `${SITE_URL}/android-chrome-256x256.png`,
home_page_url: SITE_URL,
description: SITE_DEFAULT_DESCRIPTION,
feed_url: selfUrl,
authors: [{ name: AUTHOR }],
items: [
],
}
for await (const post of pages) {
const title = post.title;
const pubDate = post.date
const postUrl = SITE_URL + "/blog/" + post.canonical
// const postHtml =
const summary = post.description;
const item: typeof feed.items[number] = {
id: post.postUrl,
title,
url: postUrl,
date_published: pubDate,
summary,
content_text: "",
}
feed.items.push(item)
}
return JSON.stringify(feed)
}
// Redirect to the rss feed with the date from the route
let date = params.date;
let newUrl = new URL("/blog/feed.json", url);
newUrl.searchParams.set("date", date);
return new Response(null, {
status: 301,
headers: {
location: newUrl.toString(),
},
});
}

View file

@ -1,80 +1,13 @@
import { pages } from '../../posts'
import {
SITE_DEFAULT_DESCRIPTION,
SITE_TITLE,
SITE_URL,
RSS_DEFAULT_POSTS_PER_PAGE
} from '$lib/metadata';
import rssStyle from "./rss-style.xsl?url"
import rssStyleCss from "./styles.css?url"
import { create } from 'xmlbuilder2';
import { error } from '@sveltejs/kit'
// import { base } from '$app/paths';
// export const prerender = true;
export async function GET({ url, params }) {
const dateParts = params.date.split(/[\/-]/).filter((s)=>s.length !== 0).map((p) => Number.parseInt(p, 10))
if (dateParts.length > 3) {
throw error(404, 'Feed not found (bad date)')
}
const selectedPages = dateParts.length ? pages
.filter((post) => {
console.log("filtering")
const date = new Date(post.date)
return (
(!dateParts[0] || date.getFullYear() == dateParts[0]) &&
(!dateParts[1] || date.getMonth()+1 == dateParts[1]) &&
(!dateParts[2] || date.getDate() == dateParts[2])
)
}) : pages;
const headers = {
'Cache-Control': 'max-age=0, s-maxage=3600',
'Content-Type': 'application/xml'
};
url.search = "";
return new Response(await getRssXml(url.href, selectedPages), { headers });
}
const AUTHOR = "Jade Ellis"
// prettier-ignore
async function getRssXml(selfUrl: string, pages: any[]): Promise<string> {
// const rssUrl = `${SITE_URL}/rss.xml`;
const root = create({ version: '1.0', encoding: 'utf-8' })
.ins('xml-stylesheet', `type="text/xsl" href="${rssStyle}"`)
.ele('feed', {
xmlns: 'http://www.w3.org/2005/Atom',
"xmlns:jade": 'http://jade.ellis.link',
})
.ele('jade:link', { rel:"stylesheet", href: rssStyleCss }).up()
.ele('title').txt(SITE_TITLE).up()
.ele('link', { href: SITE_URL }).up()
.ele('link', { rel: 'self', href: selfUrl }).up()
.ele('updated').txt(new Date().toISOString()).up()
.ele('id').txt(SITE_URL).up()
.ele('author')
.ele('name').txt(AUTHOR).up()
.up()
.ele('subtitle').txt(SITE_DEFAULT_DESCRIPTION).up()
for await (const post of pages) {
const title = post.title;
const pubDate = post.date
const postUrl = SITE_URL + "/blog/" + post.canonical
// const postHtml =
const summary = post.description;
root.ele('entry')
.ele('title').txt(title).up()
.ele('link', { href: postUrl }).up()
.ele('published').txt(pubDate).up()
.ele('id').txt(postUrl).up()
// .ele('content', { type: 'html' }).txt(postHtml).up()
.ele('summary').txt(summary).up()
.up();
}
return root.end()
}
export async function GET({ params, url}) {
// Redirect to the rss feed with the date from the route
let date = params.date;
let newUrl = new URL("/blog/rss.xml", url);
newUrl.searchParams.set("date", date);
return new Response(null, {
status: 301,
headers: {
location: newUrl.toString(),
},
});
}

View file

@ -0,0 +1,72 @@
import { pages } from '../posts'
import type Feed from '@json-feed-types/1_1'
import {
SITE_DEFAULT_DESCRIPTION,
SITE_TITLE,
SITE_URL,
RSS_DEFAULT_POSTS_PER_PAGE
} from '$lib/metadata';
import { error } from '@sveltejs/kit'
// import { base } from '$app/paths';
// export const prerender = true;
export async function GET({ params, url}) {
const dateParts = url.searchParams.get("date")?.split(/[\/-]/).filter((s)=>s.length !== 0).map((p) => Number.parseInt(p, 10))
if (dateParts?.length &&dateParts?.length > 3) {
throw error(404, 'Feed not found (bad date)')
}
const selectedPages = dateParts?.length ? pages
.filter((post) => {
const date = new Date(post.date)
return (
(!dateParts[0] || date.getFullYear() == dateParts[0]) &&
(!dateParts[1] || date.getMonth()+1 == dateParts[1]) &&
(!dateParts[2] || date.getDate() == dateParts[2])
)
}) : pages;
const headers = {
'Cache-Control': 'max-age=0, s-maxage=3600',
'Content-Type': 'application/feed+json'
};
return new Response(await getJsonFeed(url.href, selectedPages), { headers });
}
const AUTHOR = "Jade Ellis"
// prettier-ignore
async function getJsonFeed(selfUrl: string, pages: any[]): Promise<string> {
const feed: Feed = {
version: 'https://jsonfeed.org/version/1.1',
title: SITE_TITLE,
icon: `${SITE_URL}/android-chrome-256x256.png`,
home_page_url: SITE_URL,
description: SITE_DEFAULT_DESCRIPTION,
feed_url: selfUrl,
authors: [{ name: AUTHOR }],
items: [
],
}
for await (const post of pages) {
const title = post.title;
const pubDate = post.date
const postUrl = SITE_URL + "/blog/" + post.canonical
// const postHtml =
const summary = post.description;
const item: typeof feed.items[number] = {
id: post.postUrl,
title,
url: postUrl,
date_published: pubDate,
summary,
content_text: "",
}
feed.items.push(item)
}
return JSON.stringify(feed)
}

View file

@ -0,0 +1,81 @@
import { pages } from '../posts'
import {
SITE_DEFAULT_DESCRIPTION,
SITE_TITLE,
SITE_URL,
RSS_DEFAULT_POSTS_PER_PAGE
} from '$lib/metadata';
import rssStyle from "./rss-style.xsl?url"
import rssStyleCss from "./styles.css?url"
import { create } from 'xmlbuilder2';
import { error } from '@sveltejs/kit'
// import { base } from '$app/paths';
// export const prerender = true;
export async function GET({ url, params }) {
const dateParts = url.searchParams.get("date")?.split(/[\/-]/).filter((s)=>s.length !== 0).map((p) => Number.parseInt(p, 10))
if (dateParts?.length && dateParts?.length > 3) {
throw error(404, 'Feed not found (bad date)')
}
const selectedPages = dateParts?.length ? pages
.filter((post) => {
console.log("filtering")
const date = new Date(post.date)
return (
(!dateParts[0] || date.getFullYear() == dateParts[0]) &&
(!dateParts[1] || date.getMonth()+1 == dateParts[1]) &&
(!dateParts[2] || date.getDate() == dateParts[2])
)
}) : pages;
const headers = {
'Cache-Control': 'max-age=0, s-maxage=3600',
'Content-Type': 'application/xml'
};
url.search = "";
return new Response(await getRssXml(url.href, selectedPages), { headers });
}
const AUTHOR = "Jade Ellis"
// prettier-ignore
async function getRssXml(selfUrl: string, pages: any[]): Promise<string> {
// const rssUrl = `${SITE_URL}/rss.xml`;
const root = create({ version: '1.0', encoding: 'utf-8' })
.ins('xml-stylesheet', `type="text/xsl" href="${rssStyle}"`)
.ele('feed', {
xmlns: 'http://www.w3.org/2005/Atom',
"xmlns:jade": 'http://jade.ellis.link',
})
.ele('jade:link', { rel:"stylesheet", href: rssStyleCss }).up()
.ele('title').txt(SITE_TITLE).up()
.ele('link', { href: SITE_URL }).up()
.ele('link', { rel: 'self', href: selfUrl }).up()
.ele('updated').txt(new Date().toISOString()).up()
.ele('id').txt(SITE_URL).up()
.ele('author')
.ele('name').txt(AUTHOR).up()
.up()
.ele('subtitle').txt(SITE_DEFAULT_DESCRIPTION).up()
for await (const post of pages) {
const title = post.title;
const pubDate = post.date
const postUrl = SITE_URL + "/blog/" + post.canonical
// const postHtml =
const summary = post.description;
root.ele('entry')
.ele('title').txt(title).up()
.ele('link', { href: postUrl }).up()
.ele('published').txt(pubDate).up()
.ele('id').txt(postUrl).up()
// .ele('content', { type: 'html' }).txt(postHtml).up()
.ele('summary').txt(summary).up()
.up();
}
return root.end()
}