Skip to content

Commit

Permalink
feat: trying a funky way of doing RSS
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Mar 16, 2024
1 parent e27550a commit 40d4d18
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
16 changes: 16 additions & 0 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import expressiveCode from "astro-expressive-code";
import { defineConfig } from "astro/config";

import markdoc from "@astrojs/markdoc";
import { rename } from "fs/promises";

// https://astro.build/config
export default defineConfig({
Expand All @@ -24,5 +25,20 @@ export default defineConfig({
markdoc({
allowHTML: true,
}),
{
name: "RSS Generator",
hooks: {
"astro:build:done": async (options) => {
const rssPages = options.pages.filter((page) => page.pathname.includes("rss"));

for (const { pathname: rssPage } of rssPages) {
await rename(
new URL(`${rssPage}index.html`, options.dir),
new URL(`${rssPage}index.xml`, options.dir),
);
}
},
},
},
],
});
52 changes: 52 additions & 0 deletions src/pages/rss/blog.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
export const partial = true;
import { getCollection } from "astro:content";
import { getBaseSiteURL, getURLFromEntry } from "$utils";
const articles = await getCollection("blog");
function makeIntro() {
return `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My Blog</title>
<link>https://astro.build/blog</link>
<description>My blog about Astro</description>
<language>en</language>
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
`.trim();
}
function makeOutro() {
return `
</channel>
</rss>
`.trim();
}
---

<Fragment set:html={makeIntro()} />
{
articles.map(async (article) => {
const start = `\t<item>
<title>${article.data.title}</title>
<link>${getBaseSiteURL().slice(0, -1)}${getURLFromEntry(article)}</link>
<description>${article.data.tagline}</description>
<pubDate>${new Date(article.data.date).toUTCString()}</pubDate>
<content:encoded><![CDATA[`.trim();
const end = `]]></content:encoded>
</item>\n`.trim();

const { Content } = await article.render();

return (
<>
<Fragment set:html={start} />
<Content />
<Fragment set:html={end} />
</>
);
})
}
<Fragment set:html={makeOutro()} />

0 comments on commit 40d4d18

Please sign in to comment.