Skip to content

Commit

Permalink
feat: adds a social link to Starlight for the blog RSS feed
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo committed May 11, 2024
1 parent 343e9fa commit c506bfd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
18 changes: 15 additions & 3 deletions packages/starlight-blog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export default function starlightBlogPlugin(userConfig?: StarlightBlogUserConfig
name: 'starlight-blog-plugin',
hooks: {
setup({ addIntegration, astroConfig, config: starlightConfig, logger, updateConfig: updateStarlightConfig }) {
const rssLink = astroConfig.site
? `${stripTrailingSlash(astroConfig.site)}${stripTrailingSlash(astroConfig.base)}/${stripLeadingSlash(
stripTrailingSlash(config.prefix),
)}/rss.xml`
: undefined

updateStarlightConfig({
components: {
...starlightConfig.components,
Expand All @@ -29,9 +35,7 @@ export default function starlightBlogPlugin(userConfig?: StarlightBlogUserConfig
{
tag: 'link' as const,
attrs: {
href: `${stripTrailingSlash(astroConfig.site)}${stripTrailingSlash(
astroConfig.base,
)}/${stripLeadingSlash(stripTrailingSlash(config.prefix))}/rss.xml`,
href: rssLink,
rel: 'alternate',
title: config.title,
type: 'application/rss+xml',
Expand All @@ -40,6 +44,14 @@ export default function starlightBlogPlugin(userConfig?: StarlightBlogUserConfig
]
: []),
],
social: {
...starlightConfig.social,
...(astroConfig.site && rssLink && !starlightConfig.social?.rss
? {
rss: rssLink,
}
: {}),
},
})

addIntegration({
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight-blog/tests/e2e/blog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ test('should not list draft blog posts in production', async ({ blogPage }) => {
test('should add a link to the RSS feed in the sidebar', async ({ blogPage }) => {
await blogPage.goto()

const link = blogPage.page.getByRole('link', { name: 'RSS' })
const link = blogPage.page.getByLabel('Main').getByRole('link', { name: 'RSS' })

await expect(link).toBeVisible()
expect(await link.getAttribute('href')).toBe('/blog/rss.xml')
Expand Down
7 changes: 7 additions & 0 deletions packages/starlight-blog/tests/e2e/rss.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from './test'

test('should add a Starlight social link to the RSS feed', async ({ blogPage }) => {
await blogPage.goto()

await expect(blogPage.page.getByRole('banner').getByRole('link', { name: 'RSS' })).toBeVisible()
})

0 comments on commit c506bfd

Please sign in to comment.