-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from EveSunMaple/dev
✨ Add RSS Full Text
- Loading branch information
Showing
4 changed files
with
36 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,39 @@ | ||
import rss from "@astrojs/rss"; | ||
import { getCollection } from "astro:content"; | ||
import { SITE_TITLE, SITE_DESCRIPTION } from "../consts"; | ||
import { SITE_TITLE, SITE_DESCRIPTION, SITE_LANG } from "../consts"; | ||
import { marked } from "marked"; | ||
|
||
export async function GET(context) { | ||
const posts = await getCollection("blog"); | ||
const siteUrl = context.site; | ||
|
||
function replacePath(content, siteUrl) { | ||
return content.replace(/(src|img|r|l)="([^"]+)"/g, (match, attr, src) => { | ||
if (!src.startsWith("http") && !src.startsWith("//") && !src.startsWith("data:")) { | ||
return `${attr}="${new URL(src, siteUrl).toString()}"`; | ||
} | ||
return match; | ||
}); | ||
} | ||
|
||
return rss({ | ||
title: SITE_TITLE, | ||
description: SITE_DESCRIPTION, | ||
site: context.site, | ||
items: posts.map((post) => ({ | ||
...post.data, | ||
link: `/blog/${post.slug}/`, | ||
})), | ||
site: siteUrl, | ||
language: SITE_LANG, | ||
generator: "Astro RSS Generator", | ||
items: posts.map((post) => { | ||
const { body, data, slug } = post; | ||
const content = body | ||
? `<![CDATA[${replacePath(marked(body), siteUrl)}]]>` | ||
: "No content available."; | ||
return { | ||
title: data.title || "Untitled", | ||
pubDate: new Date(data.pubDate).toUTCString(), | ||
link: `/blog/${slug}/`, | ||
description: data.description || "No description provided.", | ||
content, | ||
}; | ||
}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters