diff --git a/app/controllers/blog_feed_controller.ts b/app/controllers/blog_feed_controller.ts new file mode 100644 index 0000000..9b8f421 --- /dev/null +++ b/app/controllers/blog_feed_controller.ts @@ -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, + }) + } +} diff --git a/commands/build_static.ts b/commands/build_static.ts index 53c720d..8f9ee8b 100644 --- a/commands/build_static.ts +++ b/commands/build_static.ts @@ -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' @@ -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. @@ -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() } } diff --git a/resources/views/components/layouts/main.edge b/resources/views/components/layouts/main.edge index e411213..190c560 100644 --- a/resources/views/components/layouts/main.edge +++ b/resources/views/components/layouts/main.edge @@ -7,6 +7,7 @@ {{ page.title }} + diff --git a/resources/views/feeds/blog.edge b/resources/views/feeds/blog.edge new file mode 100644 index 0000000..942020d --- /dev/null +++ b/resources/views/feeds/blog.edge @@ -0,0 +1,27 @@ + + + + AdonisJS Framework Blog + https://adonisjs.com/ + All the latest AdonisJS Framework news. + {{new Date()}} + https://validator.w3.org/feed/docs/rss2.html + en + + AdonisJS Framework Blog + https://adonisjs.com/icons/favicon-32x32.png + https://adonisjs.com/blog + + + @each(post in blogPosts) + + <![CDATA[{{ post.title }}]]> + {{ route('blog.show', [post.slug]) }} + {{ post.slug }} + {{ post.publishedAt }} + + + + @end + + diff --git a/start/routes.ts b/start/routes.ts index de319ba..e9f7c7f 100644 --- a/start/routes.ts +++ b/start/routes.ts @@ -10,6 +10,7 @@ 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') @@ -17,5 +18,6 @@ const SupportProgramController = () => import('#controllers/support_program_cont 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'])