Skip to content

Commit

Permalink
Merge pull request #40 from NielsPilgaard/fix-feed-validation-errors
Browse files Browse the repository at this point in the history
Add additional required feed nodes
  • Loading branch information
NielsPilgaard committed May 26, 2023
2 parents 9823497 + 1dbed15 commit 1c4961d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
82 changes: 47 additions & 35 deletions src/Pilgaard.Blog/Features/Feed/FeedApi.cs
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";
Expand All @@ -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();
Expand All @@ -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"));
}
}
2 changes: 2 additions & 0 deletions src/Pilgaard.Blog/Features/Feed/FeedConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
internal static class FeedConstants
{
internal const string BlogUrl = "https://pilgaard-blog.azurewebsites.net";
internal const string FeedRoute = "/feed.xml";
internal const string FeedUrl = BlogUrl + FeedRoute;
}

0 comments on commit 1c4961d

Please sign in to comment.