Added robots.txt and rss.xml generation.
This commit is contained in:
parent
296b0bde79
commit
e5da7e6491
2 changed files with 70 additions and 0 deletions
18
src/pages/robots.txt.ts
Normal file
18
src/pages/robots.txt.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import type { APIRoute } from 'astro';
|
||||||
|
|
||||||
|
const getRobotsTxt = (sitemapURL: URL) => `
|
||||||
|
User-agent: *
|
||||||
|
Allow: /
|
||||||
|
Disallow: /tags/
|
||||||
|
|
||||||
|
Sitemap: ${sitemapURL.href}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const GET: APIRoute = ({ site }) => {
|
||||||
|
const sitemapURL = new URL('sitemap-index.xml', site);
|
||||||
|
return new Response(getRobotsTxt(sitemapURL).trimStart(), {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'text/plain; charset=utf-8',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
52
src/pages/rss.xml.js
Normal file
52
src/pages/rss.xml.js
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
import rss from '@astrojs/rss';
|
||||||
|
import sanitizeHtml from 'sanitize-html';
|
||||||
|
import { experimental_AstroContainer as AstroContainer } from 'astro/container';
|
||||||
|
import { getCollection, render } from 'astro:content';
|
||||||
|
|
||||||
|
export async function GET(context) {
|
||||||
|
const notes = (await getCollection('notes')).sort((a, b) =>
|
||||||
|
a.data.date > b.data.date ? 1 : -1
|
||||||
|
).filter((note) => (note.data.published));
|
||||||
|
const container = await AstroContainer.create();
|
||||||
|
//const notes = await getCollection('notes');
|
||||||
|
const items = await Promise.all(
|
||||||
|
notes.map(async (note) => {
|
||||||
|
const { Content } = await render(note);
|
||||||
|
const rawHTML = await container.renderToString(Content);
|
||||||
|
const content = sanitizeHtml(rawHTML, {
|
||||||
|
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['a']),
|
||||||
|
allowedAttributes: {
|
||||||
|
...sanitizeHtml.defaults.allowedAttributes,
|
||||||
|
a: ['href', 'name', 'class'],
|
||||||
|
'*': ['id'],
|
||||||
|
},
|
||||||
|
transformTags: {
|
||||||
|
a: (tagName, attribs) => ({
|
||||||
|
tagName,
|
||||||
|
attribs: {
|
||||||
|
...attribs,
|
||||||
|
href: attribs.href?.startsWith('/')
|
||||||
|
? new URL(attribs.href, context.site).toString()
|
||||||
|
: attribs.href,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
title: note.data.title,
|
||||||
|
pubDate: note.data.date,
|
||||||
|
description: note.data.title,
|
||||||
|
link: `/notes/${note.id}`,
|
||||||
|
content,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return rss({
|
||||||
|
title: "It's Just Us",
|
||||||
|
description: 'Just Us Class Notes and More',
|
||||||
|
site: context.site,
|
||||||
|
items,
|
||||||
|
customData: '<language>en-us</language>',
|
||||||
|
});
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue