Extend the ability of the hosted page markdown parser

This commit is contained in:
2025-11-22 17:40:17 +08:00
parent 0e44d9c514
commit 7b7a6c9218
8 changed files with 53 additions and 16 deletions

View File

@@ -2,14 +2,14 @@ using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry;
using DysonNetwork.Zone.Publication;
using Markdig;
// Add this using statement
using Microsoft.AspNetCore.Mvc.RazorPages;
using NodaTime;
namespace DysonNetwork.Zone.Pages;
public class AboutModel(RemoteAccountService ras) : PageModel
public class AboutModel(RemoteAccountService ras, MarkdownConverter markdownConverter) : PageModel
{
public SnPublicationSite? Site { get; set; }
public Account? UserAccount { get; set; }
@@ -32,8 +32,7 @@ public class AboutModel(RemoteAccountService ras) : PageModel
if (UserAccount?.Profile?.Bio != null)
{
var pipeline = new MarkdownPipelineBuilder().Build();
HtmlBio = Markdown.ToHtml(UserAccount.Profile.Bio, pipeline);
HtmlBio = markdownConverter.ToHtml(UserAccount.Profile.Bio);
}
}
}

View File

@@ -2,7 +2,7 @@ using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry;
using DysonNetwork.Zone.Publication;
using Markdig;
// Add this using statement
using Microsoft.AspNetCore.Mvc.RazorPages;
using NodaTime;
@@ -11,9 +11,11 @@ namespace DysonNetwork.Zone.Pages;
public class IndexModel(
PostService.PostServiceClient postClient,
RemotePublisherService rps,
RemoteAccountService ras
RemoteAccountService ras,
MarkdownConverter markdownConverter // Inject MarkdownConverter
) : PageModel
{
private readonly MarkdownConverter _markdownConverter = markdownConverter; // Store the injected service
public SnPublicationSite? Site { get; set; }
public SnPublisher? Publisher { get; set; }
public Account? UserAccount { get; set; }
@@ -60,7 +62,7 @@ public class IndexModel(
foreach (
var post in FeaturedPosts.Where(post => !string.IsNullOrEmpty(post.Content))
)
post.Content = Markdown.ToHtml(post.Content!);
post.Content = _markdownConverter.ToHtml(post.Content!);
}
}
}

View File

@@ -1,14 +1,20 @@
using Markdig;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry;
using DysonNetwork.Zone.Publication;
// Add this using statement
using Microsoft.AspNetCore.Mvc.RazorPages;
using PostType = DysonNetwork.Shared.Models.PostType;
namespace DysonNetwork.Zone.Pages;
public class PostsModel(PostService.PostServiceClient postClient, RemotePublisherService rps) : PageModel
public class PostsModel(
PostService.PostServiceClient postClient,
RemotePublisherService rps,
MarkdownConverter markdownConverter // Inject MarkdownConverter
) : PageModel
{
private readonly MarkdownConverter _markdownConverter = markdownConverter; // Store the injected service
public SnPublicationSite? Site { get; set; }
public SnPublisher? Publisher { get; set; }
public List<SnPost> Posts { get; set; } = [];
@@ -43,7 +49,7 @@ public class PostsModel(PostService.PostServiceClient postClient, RemotePublishe
// Convert the markdown content to HTML
foreach (var post in Posts.Where(post => !string.IsNullOrEmpty(post.Content)))
post.Content = Markdown.ToHtml(post.Content!);
post.Content = _markdownConverter.ToHtml(post.Content!, softBreaks: post.Type != PostType.Article);
}
}
}

View File

@@ -1,14 +1,15 @@
using Markdig;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Zone.Publication;
// Add this using statement
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace DysonNetwork.Zone.Pages.Posts;
public class DetailsModel(PostService.PostServiceClient postClient) : PageModel
public class DetailsModel(PostService.PostServiceClient postClient, MarkdownConverter markdownConverter) : PageModel
{
private readonly MarkdownConverter _markdownConverter = markdownConverter;
[FromRoute] public string Slug { get; set; } = null!;
public SnPublicationSite? Site { get; set; }
@@ -35,7 +36,7 @@ public class DetailsModel(PostService.PostServiceClient postClient) : PageModel
// Convert markdown content to HTML
if (Post != null && !string.IsNullOrEmpty(Post.Content))
Post.Content = Markdown.ToHtml(Post.Content);
Post.Content = _markdownConverter.ToHtml(Post.Content);
return Page();
}

View File

@@ -0,0 +1,23 @@
using Markdig;
namespace DysonNetwork.Zone.Publication;
public class MarkdownConverter
{
private readonly MarkdownPipeline _pipelineSoftBreak = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseSoftlineBreakAsHardlineBreak()
.Build();
private readonly MarkdownPipeline _pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.Build();
public string ToHtml(string markdown, bool softBreaks = true)
{
var procMarkdown = markdown.Replace("solian://files/", "/drive/files");
return string.IsNullOrEmpty(procMarkdown)
? string.Empty
: Markdown.ToHtml(markdown, softBreaks ? _pipelineSoftBreak : _pipeline);
}
}

View File

@@ -2,13 +2,14 @@ using System.ServiceModel.Syndication;
using System.Xml;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Markdig;
using DysonNetwork.Zone.Publication;
// Add this using statement
using Microsoft.AspNetCore.Mvc;
namespace DysonNetwork.Zone.SEO;
[ApiController]
public class RssController(PostService.PostServiceClient postClient) : ControllerBase
public class RssController(PostService.PostServiceClient postClient, MarkdownConverter markdownConverter) : ControllerBase
{
[HttpGet("rss")]
[Produces("application/rss+xml")]
@@ -42,7 +43,7 @@ public class RssController(PostService.PostServiceClient postClient) : Controlle
var item = new SyndicationItem(
post.Title,
post.Content is not null ? Markdown.ToHtml(post.Content!) : "No content", // Convert Markdown to HTML
post.Content is not null ? markdownConverter.ToHtml(post.Content!) : "No content", // Convert Markdown to HTML
new Uri(postUrl),
post.Id.ToString(),
post.EditedAt?.ToDateTimeOffset() ??

View File

@@ -7,6 +7,8 @@ using DysonNetwork.Zone.Publication;
using NodaTime;
using NodaTime.Serialization.SystemTextJson;
// Add this using statement
namespace DysonNetwork.Zone.Startup;
public static class ServiceCollectionExtensions
@@ -19,6 +21,7 @@ public static class ServiceCollectionExtensions
services.AddSingleton<IClock>(SystemClock.Instance);
services.AddHttpContextAccessor();
services.AddSingleton<ICacheService, CacheServiceRedis>();
services.AddSingleton<MarkdownConverter>();
services.AddHttpClient();