-
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #40 from NielsPilgaard/fix-feed-validation-errors
Add additional required feed nodes
- Loading branch information
Showing
2 changed files
with
49 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
using Microsoft.SyndicationFeed; | ||
using System.Xml; | ||
using Microsoft.SyndicationFeed.Atom; | ||
using Pilgaard.Blog.Features.BlogPost; | ||
using Pilgaard.Blog.Features.SEO; | ||
using System.Xml; | ||
|
||
namespace Pilgaard.Blog.Features.Feed; | ||
|
||
public static class FeedApi | ||
{ | ||
public static WebApplication MapRssFeed(this WebApplication app) | ||
{ | ||
app.MapGet("/feed.xml", async (HttpContext context) => | ||
app.MapGet(FeedConstants.FeedRoute, async (HttpContext context) => | ||
{ | ||
context.Response.ContentType = "application/xml"; | ||
|
@@ -24,40 +24,9 @@ public static WebApplication MapRssFeed(this WebApplication app) | |
var writer = new AtomFeedWriter(xmlWriter); | ||
await writer.WriteTitle(DefaultMetadata.Title); | ||
var lastUpdated = BlogPostData | ||
.AllBlogPostSeries | ||
.SelectMany(blogPostSeries => blogPostSeries.BlogPosts.Select(blogPost => blogPost.LastUpdated)) | ||
.Max(); | ||
await WriteHeader(writer); | ||
await writer.WriteUpdated(lastUpdated); | ||
await writer.Write(new SyndicationLink(new Uri(FeedConstants.BlogUrl))); | ||
foreach (var blogPostSeries in BlogPostData.AllBlogPostSeries) | ||
{ | ||
foreach (var blogPost in blogPostSeries.BlogPosts) | ||
{ | ||
var link = $"{FeedConstants.BlogUrl}/{blogPostSeries.GetRelativePath(blogPost)}"; | ||
var item = new SyndicationItem | ||
{ | ||
Id = link, | ||
Title = blogPost.Title, | ||
Description = blogPost.Description, | ||
Published = blogPost.PublishDate, | ||
LastUpdated = blogPost.LastUpdated, | ||
}; | ||
item.AddLink(new SyndicationLink(new Uri(link))); | ||
item.AddContributor(new SyndicationPerson("Niels Pilgaard Grøndahl", "[email protected]")); | ||
foreach (var blogPostTag in blogPost.Tags) | ||
{ | ||
item.AddCategory(new SyndicationCategory(blogPostTag)); | ||
} | ||
await writer.Write(item); | ||
} | ||
} | ||
await WriteBody(writer); | ||
// This closes the <content> element | ||
await xmlWriter.WriteEndElementAsync(); | ||
|
@@ -69,4 +38,47 @@ public static WebApplication MapRssFeed(this WebApplication app) | |
|
||
return app; | ||
} | ||
|
||
private static async Task WriteBody(ISyndicationFeedWriter writer) | ||
{ | ||
foreach (var blogPostSeries in BlogPostData.AllBlogPostSeries) | ||
{ | ||
foreach (var blogPost in blogPostSeries.BlogPosts) | ||
{ | ||
var link = $"{FeedConstants.BlogUrl}/{blogPostSeries.GetRelativePath(blogPost)}"; | ||
var item = new SyndicationItem | ||
{ | ||
Id = link, | ||
Title = blogPost.Title, | ||
Description = blogPost.Description, | ||
Published = blogPost.PublishDate, | ||
LastUpdated = blogPost.LastUpdated, | ||
}; | ||
|
||
item.AddLink(new SyndicationLink(new Uri(link))); | ||
item.AddContributor(new SyndicationPerson("Niels Pilgaard Grøndahl", "[email protected]")); | ||
foreach (var blogPostTag in blogPost.Tags) | ||
{ | ||
item.AddCategory(new SyndicationCategory(blogPostTag)); | ||
} | ||
|
||
await writer.Write(item); | ||
} | ||
} | ||
} | ||
|
||
private static async Task WriteHeader(AtomFeedWriter writer) | ||
{ | ||
await writer.WriteTitle(DefaultMetadata.Title); | ||
|
||
var lastUpdated = BlogPostData | ||
.AllBlogPostSeries | ||
.SelectMany(blogPostSeries => blogPostSeries.BlogPosts.Select(blogPost => blogPost.LastUpdated)) | ||
.Max(); | ||
|
||
await writer.WriteUpdated(lastUpdated); | ||
await writer.WriteId(FeedConstants.FeedUrl); | ||
await writer.Write(new SyndicationLink(new Uri(FeedConstants.FeedUrl), "self")); | ||
await writer.Write(new SyndicationLink(new Uri(FeedConstants.BlogUrl), "alternate")); | ||
} | ||
} |
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