Skip to content

Commit

Permalink
feat: add RSS support for Blog entries (#19)
Browse files Browse the repository at this point in the history
* Add RSS support for Blog entries

* Limit the number of posts in the feed

* Add feeds generation to static build command

* Update resources/views/feeds/blog.edge

Co-authored-by: Romain Lanz <[email protected]>

* Update resources/views/feeds/blog.edge

Co-authored-by: Romain Lanz <[email protected]>

* Update resources/views/feeds/blog.edge

Co-authored-by: Romain Lanz <[email protected]>

---------

Co-authored-by: Romain Lanz <[email protected]>
  • Loading branch information
msurdi and RomainLanz authored Oct 30, 2024
1 parent 2314adf commit 2c2b2b9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/controllers/blog_feed_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { HttpContext } from '@adonisjs/core/http'
import BlogPosts from '../collections/blog_posts.js'

export default class BlogFeedController {
async handle({ view, response }: HttpContext) {
response.append('Content-Type', 'text/xml')

const publishedBlogPosts = await new BlogPosts().published()

// Only publish the most recent 20 posts
const feedBlogPosts = publishedBlogPosts.slice(0, 20)

return view.render('feeds/blog', {
blogPosts: feedBlogPosts,
})
}
}
18 changes: 18 additions & 0 deletions commands/build_static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { CommandOptions } from '@adonisjs/core/types/ace'
import { HttpContextFactory } from '@adonisjs/core/factories/http'
import SupportProgramController from '#controllers/support_program_controller'
import CaseStudiesController from '#controllers/case_studies_controller'
import BlogFeedController from '#controllers/blog_feed_controller'

export default class BuildStatic extends BaseCommand {
static commandName = 'build:static'
Expand Down Expand Up @@ -47,6 +48,22 @@ export default class BuildStatic extends BaseCommand {
this.logger.success('created dist/contact.html')
}

/**
* Creates a static copy of the RSS feeds.
*/
protected async buildFeeds() {
const blogFeedController = new BlogFeedController()
const ctx = new HttpContextFactory().create()
await mkdir(app.makePath('dist/feeds'), { recursive: true })

/**
* Create blog feed
*/
const feed = await blogFeedController.handle(ctx)
await writeFile(app.makePath('dist/feeds/blog.xml'), feed)
this.logger.success('created dist/feeds/blog.xml')
}

/**
* Creates a static copy of the blog listing and
* individual blog posts.
Expand Down Expand Up @@ -126,6 +143,7 @@ export default class BuildStatic extends BaseCommand {
await this.buildAboutPage()
await this.buildContactPage()
await this.buildBlog()
await this.buildFeeds()
// await this.buildCaseStudies()
}
}
1 change: 1 addition & 0 deletions resources/views/components/layouts/main.edge
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<title>{{ page.title }}</title>
<meta name="description" content="{{ page.description }}">

<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="/feeds/blog.xml"/>
<link rel="apple-touch-icon" sizes="180x180" href="/icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/icons/favicon-16x16.png">
Expand Down
27 changes: 27 additions & 0 deletions resources/views/feeds/blog.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>AdonisJS Framework Blog</title>
<link>https://adonisjs.com/</link>
<description>All the latest AdonisJS Framework news.</description>
<lastBuildDate>{{new Date()}}</lastBuildDate>
<docs>https://validator.w3.org/feed/docs/rss2.html</docs>
<language>en</language>
<image>
<title>AdonisJS Framework Blog</title>
<url>https://adonisjs.com/icons/favicon-32x32.png</url>
<link>https://adonisjs.com/blog</link>
</image>
<atom:link href="https://adonisjs.com/feeds/blog.xml" rel="self" type="application/rss+xml"/>
@each(post in blogPosts)
<item>
<title><![CDATA[{{ post.title }}]]></title>
<link>{{ route('blog.show', [post.slug]) }}</link>
<guid>{{ post.slug }}</guid>
<pubDate>{{ post.publishedAt }}</pubDate>
<description><![CDATA[ {{{ post.contentHtml.summary }}} ]]></description>
<content:encoded><![CDATA[ {{{ post.contentHtml.contents }}} ]]></content:encoded>
</item>
@end
</channel>
</rss>
2 changes: 2 additions & 0 deletions start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import router from '@adonisjs/core/services/router'

const BlogController = () => import('#controllers/blog_controller')
const BlogFeedController = () => import('#controllers/blog_feed_controller')
const HomeController = () => import('#controllers/home_controller')
const AboutController = () => import('#controllers/about_controller')
const SupportProgramController = () => import('#controllers/support_program_controller')

router.get('/', [HomeController])
router.get('/contact', [SupportProgramController])
router.get('/about', [AboutController])
router.get('feeds/blog.xml', [BlogFeedController])

router.resource('blog', BlogController).params({ blog: 'slug' }).only(['index', 'show'])

0 comments on commit 2c2b2b9

Please sign in to comment.