Add Atom 1.0 feed syndication

This commit is contained in:
pancakes 2025-09-16 13:50:08 +10:00
parent fa9500eb81
commit fb4d9fa0aa
Signed by: pancakes
SSH key fingerprint: SHA256:yrp4c4hhaPoPG07fb4QyQIgAdlbUdsJvUAydJEWnfTw
5 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,48 @@
using System.Net.Mime;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.AspNetCore.Mvc;
using PancakesWeb.Components.UI;
namespace PancakesWeb.Controllers;
[ApiController]
public class FeedController : ControllerBase
{
[HttpGet("/feed.atom")]
[Produces("application/atom+xml")]
public ContentResult GetAtomFeed()
{
var feed = new SyndicationFeed
{
Title = new TextSyndicationContent("pancakes' blog"),
Description = new TextSyndicationContent("🐈‍⬛"),
Id = "https://pancakes.gay/feed.atom",
BaseUri = new Uri("https://pancakes.gay/feed.atom"),
Authors = { new SyndicationPerson("p@pancakes.gay", "pancakes", "https://pancakes.gay") },
Items = BlogPosts.Posts.OrderByDescending(post => post.Published).Select(post =>
new SyndicationItem(post.Title, SyndicationContent.CreateHtmlContent(post.Content),
new Uri($"https://pancakes.gay/{post.Slug}"), $"https://pancakes.gay/{post.Slug}",
new DateTimeOffset(post.Edited ?? post.Published))
{
Copyright = post.Footer is BlogPosts.PostFooter.CcBy
? new TextSyndicationContent(
$"{post.Title} © {post.Published.Year} by pancakes is licensed under CC BY 4.0")
: null,
PublishDate = new DateTimeOffset(post.Published),
Summary = new TextSyndicationContent(post.Description)
}),
Links =
{
SyndicationLink.CreateSelfLink(new Uri("https://pancakes.gay/feed.atom")),
SyndicationLink.CreateAlternateLink(new Uri("https://pancakes.gay"), MediaTypeNames.Text.Html),
}
};
var stringWriter = new StringWriter();
var writer = new XmlTextWriter(stringWriter);
feed.SaveAsAtom10(writer);
return Content(stringWriter.ToString(), "application/atom+xml");
}
}

View file

@ -187,6 +187,12 @@ public static class Buttons
{ {
Image = "wii.webp", Image = "wii.webp",
Title = "Wii" Title = "Wii"
},
new Button
{
Image = "valid_atom.webp",
Title = "Valid Atom 1.0",
Href = "/feed.atom"
} }
]; ];
} }

View file

@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Markdig" Version="0.42.0" /> <PackageReference Include="Markdig" Version="0.42.0" />
<PackageReference Include="System.ServiceModel.Syndication" Version="9.0.9" />
<PackageReference Include="YamlDotNet" Version="16.3.0" /> <PackageReference Include="YamlDotNet" Version="16.3.0" />
</ItemGroup> </ItemGroup>

View file

@ -6,6 +6,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents(); .AddInteractiveServerComponents();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddControllers();
builder.Configuration.AddEnvironmentVariables(); builder.Configuration.AddEnvironmentVariables();
@ -32,5 +33,6 @@ app.UseAntiforgery();
app.MapStaticAssets(); app.MapStaticAssets();
app.MapRazorComponents<App>() app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode(); .AddInteractiveServerRenderMode();
app.MapControllers();
app.Run(); app.Run();

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB