Render markdown on hosted pages

This commit is contained in:
2025-11-22 13:28:37 +08:00
parent bd1715c9a3
commit c806365a81
4 changed files with 105 additions and 84 deletions

View File

@@ -6,7 +6,7 @@
}
<div class="container mx-auto px-8 py-8">
<h1 class="text-3xl font-bold mb-8">
<h1 class="text-3xl font-bold mb-8 px-5">
<span class="mdi mdi-note-text-outline"></span> Posts
</h1>
@@ -36,7 +36,7 @@
}
</section>
}
<p>@post.Content</p>
<p>@Html.Raw(post.Content)</p>
</div>
@if (post.Attachments.Any())

View File

@@ -1,3 +1,4 @@
using Markdig;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry;
@@ -12,7 +13,7 @@ public class PostsModel(PostService.PostServiceClient postClient, RemotePublishe
public SnPublisher? Publisher { get; set; }
public List<SnPost> Posts { get; set; } = [];
public int TotalCount { get; set; }
public int CurrentPage { get; set; }
public int PageSize { get; set; } = 10;
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
@@ -21,7 +22,7 @@ public class PostsModel(PostService.PostServiceClient postClient, RemotePublishe
{
Site = HttpContext.Items[PublicationSiteMiddleware.SiteContextKey] as SnPublicationSite;
CurrentPage = currentPage;
Publisher = await rps.GetPublisher(id: Site!.PublisherId.ToString());
var request = new ListPostsRequest
@@ -39,6 +40,10 @@ public class PostsModel(PostService.PostServiceClient postClient, RemotePublishe
{
Posts = response.Posts.Select(SnPost.FromProtoValue).ToList();
TotalCount = response.TotalSize;
// Convert the markdown content to HTML
foreach (var post in Posts.Where(post => !string.IsNullOrEmpty(post.Content)))
post.Content = Markdown.ToHtml(post.Content!);
}
}
}

View File

@@ -2,102 +2,113 @@
@model DysonNetwork.Zone.Pages.Posts.DetailsModel
@{
Layout = "_LayoutContained";
const string defaultAvatar = "https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp";
}
<div class="container max-w-4xl mx-auto px-8 py-8">
@if (Model.Post != null)
{
<section class="mb-8 mt-2 px-5">
<h1 class="text-3xl font-bold">
@(Model.Post.Title ?? "Post Details")
</h1>
@if (!string.IsNullOrWhiteSpace(Model.Post.Description))
{
<p>@Model.Post.Description</p>
}
</section>
<div class="w-full flex flex-col gap-5">
<div class="card bg-base-200 shadow-md">
<div class="card-body flex flex-col gap-5">
<div class="prose max-w-none">
@if (!string.IsNullOrWhiteSpace(Model.Post.Description))
{
<p>@Model.Post.Description</p>
}
@Model.Post.Content
<article>
@if (Model.Post != null)
{
<section class="mb-8 mt-2 px-5">
<h1 class="text-3xl font-bold">
@(Model.Post.Title ?? "Post Details")
</h1>
@if (!string.IsNullOrWhiteSpace(Model.Post.Description))
{
<p>@Model.Post.Description</p>
}
<div class="mt-2 flex gap-3 flex-wrap text-sm text-base-content/60">
<div class="flex gap-1">
<span class="mdi mdi-history"></span>
<span>
Posted on @Model.Post.CreatedAt.ToDateTimeOffset().ToString("yyyy-MM-dd HH:mm")
</span>
</div>
@if (Model.Post.Attachments.Any())
@if (Model.Post.EditedAt.HasValue)
{
<div class="flex flex-col gap-4">
@foreach (var attachment in Model.Post.Attachments)
{
<div>
@if (attachment.MimeType!.StartsWith("image"))
{
<figure>
<img src="/drive/files/@attachment.Id" alt="@attachment.Name"
class="max-w-full h-auto rounded-lg"/>
</figure>
}
else if (attachment.MimeType!.StartsWith("video"))
{
<video controls src="/drive/files/@attachment.Id"
class="max-w-full h-auto rounded-lg"></video>
}
else if (attachment.MimeType!.StartsWith("audio"))
{
<audio controls src="/drive/files/@attachment.Id"
class="max-w-full h-auto rounded-lg"></audio>
}
else
{
<a href="/drive/files/@attachment.Id" target="_blank"
class="text-blue-500 hover:underline">@attachment.Name</a>
}
</div>
}
<div class="flex gap-1">
<span class="mdi mdi-pencil"></span>
<span>
Updated on @Model.Post.EditedAt.Value.ToDateTimeOffset().ToString("yyyy-MM-dd HH:mm")
</span>
</div>
}
</div>
</section>
@if (Model.Post.Categories.Any() || (Model.Post.Tags != null && Model.Post.Tags.Any()))
{
<div>
@foreach (var category in Model.Post.Categories)
<div class="w-full flex flex-col gap-5">
<div class="card bg-base-200 shadow-md">
<div class="card-body flex flex-col gap-5">
<div class="prose max-w-none">
@if (!string.IsNullOrWhiteSpace(Model.Post.Description))
{
<span class="badge badge-primary">
<span class="mdi mdi-shape"></span>
@(!string.IsNullOrEmpty(category.Name) ? category.Name : category.Slug)
</span>
<p>@Model.Post.Description</p>
}
@if (Model.Post.Tags != null)
{
@Html.Raw(Model.Post.Content)
</div>
@if (Model.Post.Attachments.Any())
{
<div class="flex flex-col gap-4">
@foreach (var attachment in Model.Post.Attachments)
{
<div>
@if (attachment.MimeType!.StartsWith("image"))
{
<figure>
<img src="/drive/files/@attachment.Id" alt="@attachment.Name"
class="max-w-full h-auto rounded-lg"/>
</figure>
}
else if (attachment.MimeType!.StartsWith("video"))
{
<video controls src="/drive/files/@attachment.Id"
class="max-w-full h-auto rounded-lg"></video>
}
else if (attachment.MimeType!.StartsWith("audio"))
{
<audio controls src="/drive/files/@attachment.Id"
class="max-w-full h-auto rounded-lg"></audio>
}
else
{
<a href="/drive/files/@attachment.Id" target="_blank"
class="text-blue-500 hover:underline">@attachment.Name</a>
}
</div>
}
</div>
}
@if (Model.Post.Categories.Any() || (Model.Post.Tags.Any()))
{
<div>
@foreach (var category in Model.Post.Categories)
{
<span class="badge badge-primary">
<span class="mdi mdi-shape"></span>
@(!string.IsNullOrEmpty(category.Name) ? category.Name : category.Slug)
</span>
}
@foreach (var tag in Model.Post.Tags)
{
<span class="badge badge-info">
<span class="mdi mdi-tag"></span>
<span class="mdi mdi-tag"></span>
@(!string.IsNullOrEmpty(tag.Name) ? tag.Name : tag.Slug)
</span>
</span>
}
}
</div>
}
<div class="card-actions justify-end items-center">
<div class="text-sm text-base-content/60">
Posted on @Model.Post.CreatedAt.ToDateTimeOffset().ToString("yyyy-MM-dd")
</div>
</div>
}
</div>
</div>
</div>
</div>
}
else
{
<div class="text-center py-16">
<p class="text-lg">Post not found.</p>
</div>
}
}
else
{
<div class="text-center py-16">
<p class="text-lg">Post not found.</p>
</div>
}
</article>
</div>

View File

@@ -1,3 +1,4 @@
using Markdig;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Zone.Publication;
@@ -31,6 +32,10 @@ public class DetailsModel(PostService.PostServiceClient postClient) : PageModel
}
Post = SnPost.FromProtoValue(response);
// Convert markdown content to HTML
if (Post != null && !string.IsNullOrEmpty(Post.Content))
Post.Content = Markdown.ToHtml(Post.Content);
return Page();
}