mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
Fix blog feeds
This commit is contained in:
@@ -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}) {
|
export async function GET({ params, url}) {
|
||||||
const dateParts = params.date.split(/[\/-]/).filter((s)=>s.length !== 0).map((p) => Number.parseInt(p, 10))
|
// Redirect to the rss feed with the date from the route
|
||||||
if (dateParts.length > 3) {
|
let date = params.date;
|
||||||
throw error(404, 'Feed not found (bad date)')
|
let newUrl = new URL("/blog/feed.json", url);
|
||||||
}
|
newUrl.searchParams.set("date", date);
|
||||||
|
return new Response(null, {
|
||||||
const selectedPages = dateParts.length ? pages
|
status: 301,
|
||||||
.filter((post) => {
|
headers: {
|
||||||
const date = new Date(post.date)
|
location: newUrl.toString(),
|
||||||
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)
|
|
||||||
}
|
|
||||||
@@ -1,80 +1,13 @@
|
|||||||
import { pages } from '../../posts'
|
|
||||||
|
|
||||||
import {
|
export async function GET({ params, url}) {
|
||||||
SITE_DEFAULT_DESCRIPTION,
|
// Redirect to the rss feed with the date from the route
|
||||||
SITE_TITLE,
|
let date = params.date;
|
||||||
SITE_URL,
|
let newUrl = new URL("/blog/rss.xml", url);
|
||||||
RSS_DEFAULT_POSTS_PER_PAGE
|
newUrl.searchParams.set("date", date);
|
||||||
} from '$lib/metadata';
|
return new Response(null, {
|
||||||
import rssStyle from "./rss-style.xsl?url"
|
status: 301,
|
||||||
import rssStyleCss from "./styles.css?url"
|
headers: {
|
||||||
import { create } from 'xmlbuilder2';
|
location: newUrl.toString(),
|
||||||
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()
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user