Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tilt feed component
import { renderMarkdown, useCollection, useFiles, usePage, useSite, xml } from "../packages/tilt/index.js";
export default function Feed(props, children) {
const site = useSite()
const posts = useCollection('posts')
const buildDate = (new Date()).toUTCString()
return xml`<?xml version="1.0" encoding="UTF-8"?>
<rss
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>
<![CDATA[${site.title}]]>
</title>
<description>
<![CDATA[${renderMarkdown(site.description)}]]>
</description>
<link>${site.url}</link>
<generator>tilt</generator>
<lastBuildDate>${buildDate}</lastBuildDate>
<atom:link href="${site.url}/feed.xml" rel="self" type="application/rss+xml"/>
<author>
<![CDATA[${site.author}]]>
</author>
${posts.slice(0, 5).map(post => xml`
${Item({ ...post, author: site.author, category: getCategory(post) })}
`)}
</channel>
</rss>
`
}
function Item({ title, description, permalink, category, date, author }, children) {
date = date instanceof Date ? date.toUTCString() : date
return xml`
<item>
<title>
<![CDATA[${title}]]>
</title>
<description>
<![CDATA[${renderMarkdown(description)}]]>
</description>
<link>${permalink}</link>
<guid isPermaLink="true">${permalink}</guid>
<category>
<![CDATA[${category}]]>
</category>
<dc:creator>
<![CDATA[${author}]]>
</dc:creator>
<pubDate>${date}</pubDate>
</item>
`
}
const getCategory = (post) =>
(post.categories[0] || []).map(c => c.name).join('/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.