Add JSONfeed

This commit is contained in:
Laura Hausmann 2022-11-26 03:34:13 +01:00
parent 2cbfca8117
commit 0ae856ca61
Signed by: zotan
GPG key ID: D044E84C5BE01605
3 changed files with 49 additions and 6 deletions

View file

@ -13,6 +13,7 @@
<title>zotan.pw >> @ViewData["title"]</title>
}
<link rel="stylesheet" href="~/css/site.css?v=@Utils.LinkVersion"/>
@await RenderSectionAsync("head", false)
</head>
<body>
<header>
@ -35,7 +36,7 @@
</span>
}
</h1>
@await RenderSectionAsync("head", false)
@await RenderSectionAsync("postheader", false)
</div>
</nav>
</header>

View file

@ -5,6 +5,10 @@
ViewData["desc"] = "Blog of a disabled neurodivergent queer person unhappy with the state of the world.";
}
@section head {
<link rel="alternate" title="zotan.pw >> blog" type="application/json" href="http://localhost:5073/blog/feed.json"/>
}
<p>Hey there, welcome to the blog of a disabled neurodivergent queer person unhappy with the state of the world.</p>
<p>This is where I post about things that make it somewhat fun, things that help me with life in general or just things I felt like sharing.</p>
<h1 id="posts">Posts</h1>

View file

@ -1,10 +1,48 @@
using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;
using Microsoft.AspNetCore.Mvc;
namespace zotanpw_web.Pages.blog;
namespace zotanpw_web.Pages.blog;
[ApiController, Route("/blog/feed.json")]
public class JsonFeed : Controller {
// GET
public IActionResult Index() {
return View();
public static readonly JsonFeedObject Feedobj = new JsonFeedObject {
Items = BlogModel.Posts.Take(10).Select(post => new JsonFeedItem(post.Shorthand, $"https://zotan.pw/blog/{post.Shorthand}", post.Title, post.Content,
post.PublishedOn.ToDateTime(TimeOnly.MinValue).ToString("O")))
.ToList()
};
[HttpGet]
public JsonFeedObject Get() {
Response.ContentType = "application/json";
return Feedobj;
}
}
public class JsonFeedObject {
[J("version")] public string Version => "https://jsonfeed.org/version/1.1";
[J("language")] public string Language => "en";
[J("title")] public string Title => "zotan.pw >> blog";
[J("description")] public string Description => "Blog of a disabled neurodivergent queer person unhappy with the state of the world";
[J("home_page_url")] public string HomeUrl => "https://zotan.pw/blog";
[J("feed_url")] public string FeedUrl => "https://zotan.pw/blog/feed.json";
// TODO: favicon/icon
[J("items")] public List<JsonFeedItem> Items { get; init; } = new();
}
public class JsonFeedItem {
public JsonFeedItem(string id, string url, string title, string contentHtml, string datePublished) {
_id = id;
_url = url;
_title = title;
_contentHtml = contentHtml;
_datePublished = datePublished;
}
[J("id")] public string _id { get; }
[J("url")] public string _url { get; }
[J("title")] public string _title { get; }
[J("content_html")] public string _contentHtml { get; }
[J("date_published")] public string _datePublished { get; }
}
}