From a53e44d6751abe1c1fdbdd7a7b23498843d16a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Surdi?= Date: Sat, 8 Jun 2024 07:55:13 +0200 Subject: [PATCH 1/6] Add RSS support for Blog entries --- app/controllers/blog_feed_controller.ts | 12 +++++++++ resources/views/components/layouts/main.edge | 1 + resources/views/feeds/blog.edge | 27 ++++++++++++++++++++ start/routes.ts | 2 ++ 4 files changed, 42 insertions(+) create mode 100644 app/controllers/blog_feed_controller.ts create mode 100644 resources/views/feeds/blog.edge diff --git a/app/controllers/blog_feed_controller.ts b/app/controllers/blog_feed_controller.ts new file mode 100644 index 0000000..422e610 --- /dev/null +++ b/app/controllers/blog_feed_controller.ts @@ -0,0 +1,12 @@ +import type { HttpContext } from '@adonisjs/core/http' +import BlogPosts from '../collections/blog_posts.js' + +export default class BlogFeedController { + handle({ view, response }: HttpContext) { + response.append('Content-Type', 'text/xml') + + return view.render('feeds/blog', { + blogPosts: new BlogPosts(), + }) + } +} 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..3ba482a --- /dev/null +++ b/resources/views/feeds/blog.edge @@ -0,0 +1,27 @@ + + + + Adonis Framework Blog + https://adonisjs.com/ + All the latest Adonis Framework news. + {{new Date()}} + https://validator.w3.org/feed/docs/rss2.html + en + + Adonis Framework Blog + https://adonisjs.com/icons/favicon-32x32.png + https://adonisjs.com/blog + + + @each(post in await blogPosts.published()) + + <![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']) From a4632f6aa7909b0f1334aca0805905c48ee8f325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Surdi?= Date: Wed, 23 Oct 2024 17:25:36 +0200 Subject: [PATCH 2/6] Limit the number of posts in the feed --- app/controllers/blog_feed_controller.ts | 9 +++++++-- resources/views/feeds/blog.edge | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/blog_feed_controller.ts b/app/controllers/blog_feed_controller.ts index 422e610..9b8f421 100644 --- a/app/controllers/blog_feed_controller.ts +++ b/app/controllers/blog_feed_controller.ts @@ -2,11 +2,16 @@ import type { HttpContext } from '@adonisjs/core/http' import BlogPosts from '../collections/blog_posts.js' export default class BlogFeedController { - handle({ view, response }: HttpContext) { + 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: new BlogPosts(), + blogPosts: feedBlogPosts, }) } } diff --git a/resources/views/feeds/blog.edge b/resources/views/feeds/blog.edge index 3ba482a..01502ee 100644 --- a/resources/views/feeds/blog.edge +++ b/resources/views/feeds/blog.edge @@ -13,7 +13,7 @@ https://adonisjs.com/blog - @each(post in await blogPosts.published()) + @each(post in blogPosts) <![CDATA[{{ post.title }}]]> {{ route('blog.show', [post.slug]) }} From 7fa8d12c1f29835fb162cea1f376dde7f87d8353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Surdi?= Date: Thu, 24 Oct 2024 16:59:05 +0200 Subject: [PATCH 3/6] Add feeds generation to static build command --- commands/build_static.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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() } } From a4aed85bbe52ed4dbe449ff995805a6521af4dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Surdi?= Date: Sun, 27 Oct 2024 20:47:38 +0100 Subject: [PATCH 4/6] Update resources/views/feeds/blog.edge Co-authored-by: Romain Lanz <2793951+RomainLanz@users.noreply.github.com> --- resources/views/feeds/blog.edge | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/feeds/blog.edge b/resources/views/feeds/blog.edge index 01502ee..5259cf2 100644 --- a/resources/views/feeds/blog.edge +++ b/resources/views/feeds/blog.edge @@ -1,7 +1,7 @@ - Adonis Framework Blog + AdonisJS Framework Blog https://adonisjs.com/ All the latest Adonis Framework news. {{new Date()}} From 6b8c210edb21fbaa96be2e85d3faa5d2cd8a8478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Surdi?= Date: Sun, 27 Oct 2024 20:47:51 +0100 Subject: [PATCH 5/6] Update resources/views/feeds/blog.edge Co-authored-by: Romain Lanz <2793951+RomainLanz@users.noreply.github.com> --- resources/views/feeds/blog.edge | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/feeds/blog.edge b/resources/views/feeds/blog.edge index 5259cf2..16154d0 100644 --- a/resources/views/feeds/blog.edge +++ b/resources/views/feeds/blog.edge @@ -3,7 +3,7 @@ AdonisJS Framework Blog https://adonisjs.com/ - All the latest Adonis Framework news. + All the latest AdonisJS Framework news. {{new Date()}} https://validator.w3.org/feed/docs/rss2.html en From 9c9863dddb4a2a3072ce5be666d36b7bb9c6afe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Surdi?= Date: Sun, 27 Oct 2024 20:47:57 +0100 Subject: [PATCH 6/6] Update resources/views/feeds/blog.edge Co-authored-by: Romain Lanz <2793951+RomainLanz@users.noreply.github.com> --- resources/views/feeds/blog.edge | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/feeds/blog.edge b/resources/views/feeds/blog.edge index 16154d0..942020d 100644 --- a/resources/views/feeds/blog.edge +++ b/resources/views/feeds/blog.edge @@ -8,7 +8,7 @@ https://validator.w3.org/feed/docs/rss2.html en - Adonis Framework Blog + AdonisJS Framework Blog https://adonisjs.com/icons/favicon-32x32.png https://adonisjs.com/blog