[backend/masto-client] Respect SupportsHtmlFormatting setting (ISH-88)
This commit is contained in:
parent
b979605470
commit
f07217b390
3 changed files with 40 additions and 12 deletions
|
@ -57,12 +57,12 @@ public static class ServiceExtensions
|
|||
.AddScoped<NotificationRenderer>()
|
||||
.AddScoped<ActivityPubController>()
|
||||
.AddScoped<FollowupTaskService>()
|
||||
.AddScoped<InstanceService>();
|
||||
.AddScoped<InstanceService>()
|
||||
.AddScoped<MfmConverter>();
|
||||
|
||||
// Singleton = instantiated once across application lifetime
|
||||
services
|
||||
.AddSingleton<HttpClient, CustomHttpClient>()
|
||||
.AddSingleton<MfmConverter>()
|
||||
.AddSingleton<HttpRequestService>()
|
||||
.AddSingleton<CronService>()
|
||||
.AddSingleton<QueueService>()
|
||||
|
|
|
@ -17,6 +17,8 @@ namespace Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion;
|
|||
|
||||
public class MfmConverter(IOptions<Config.InstanceSection> config)
|
||||
{
|
||||
public bool SupportsHtmlFormatting { private get; set; } = true;
|
||||
|
||||
public static async Task<string?> FromHtmlAsync(string? html, List<Note.MentionedUser>? mentions = null)
|
||||
{
|
||||
if (html == null) return null;
|
||||
|
@ -77,7 +79,8 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
|
|||
{
|
||||
case MfmBoldNode:
|
||||
{
|
||||
var el = document.CreateElement("b");
|
||||
var el = CreateInlineFormattingElement(document, "b");
|
||||
AddHtmlMarkup(node, "**");
|
||||
AppendChildren(el, document, node, mentions, host);
|
||||
return el;
|
||||
}
|
||||
|
@ -89,21 +92,23 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
|
|||
}
|
||||
case MfmStrikeNode:
|
||||
{
|
||||
var el = document.CreateElement("del");
|
||||
var el = CreateInlineFormattingElement(document, "del");
|
||||
AddHtmlMarkup(node, "~~");
|
||||
AppendChildren(el, document, node, mentions, host);
|
||||
return el;
|
||||
}
|
||||
case MfmItalicNode:
|
||||
case MfmFnNode:
|
||||
{
|
||||
var el = document.CreateElement("i");
|
||||
var el = CreateInlineFormattingElement(document, "i");
|
||||
AddHtmlMarkup(node, "*");
|
||||
AppendChildren(el, document, node, mentions, host);
|
||||
return el;
|
||||
}
|
||||
case MfmCodeBlockNode codeBlockNode:
|
||||
{
|
||||
var el = document.CreateElement("pre");
|
||||
var inner = document.CreateElement("code");
|
||||
var el = CreateInlineFormattingElement(document, "pre");
|
||||
var inner = CreateInlineFormattingElement(document, "code");
|
||||
inner.TextContent = codeBlockNode.Code;
|
||||
el.AppendNodes(inner);
|
||||
return el;
|
||||
|
@ -132,19 +137,19 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
|
|||
}
|
||||
case MfmInlineCodeNode inlineCodeNode:
|
||||
{
|
||||
var el = document.CreateElement("code");
|
||||
var el = CreateInlineFormattingElement(document, "code");
|
||||
el.TextContent = inlineCodeNode.Code;
|
||||
return el;
|
||||
}
|
||||
case MfmMathInlineNode mathInlineNode:
|
||||
{
|
||||
var el = document.CreateElement("code");
|
||||
var el = CreateInlineFormattingElement(document, "code");
|
||||
el.TextContent = mathInlineNode.Formula;
|
||||
return el;
|
||||
}
|
||||
case MfmMathBlockNode mathBlockNode:
|
||||
{
|
||||
var el = document.CreateElement("code");
|
||||
var el = CreateInlineFormattingElement(document, "code");
|
||||
el.TextContent = mathBlockNode.Formula;
|
||||
return el;
|
||||
}
|
||||
|
@ -188,7 +193,8 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
|
|||
}
|
||||
case MfmQuoteNode:
|
||||
{
|
||||
var el = document.CreateElement("blockquote");
|
||||
var el = CreateInlineFormattingElement(document, "blockquote");
|
||||
AddHtmlMarkupStartOnly(node, "> ");
|
||||
AppendChildren(el, document, node, mentions, host);
|
||||
return el;
|
||||
}
|
||||
|
@ -247,4 +253,23 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
|
|||
{
|
||||
foreach (var node in parent.Children) element.AppendNodes(FromMfmNode(document, node, mentions, host));
|
||||
}
|
||||
|
||||
private IElement CreateInlineFormattingElement(IDocument document, string name)
|
||||
{
|
||||
return document.CreateElement(SupportsHtmlFormatting ? name : "span");
|
||||
}
|
||||
|
||||
private void AddHtmlMarkup(MfmNode node, string chars)
|
||||
{
|
||||
if (SupportsHtmlFormatting) return;
|
||||
var markupNode = new MfmTextNode { Text = chars };
|
||||
node.Children = node.Children.Prepend(markupNode).Append(markupNode);
|
||||
}
|
||||
|
||||
private void AddHtmlMarkupStartOnly(MfmNode node, string chars)
|
||||
{
|
||||
if (SupportsHtmlFormatting) return;
|
||||
var markupNode = new MfmTextNode { Text = chars };
|
||||
node.Children = node.Children.Prepend(markupNode);
|
||||
}
|
||||
}
|
|
@ -2,12 +2,13 @@ using Iceshrimp.Backend.Controllers.Mastodon.Attributes;
|
|||
using Iceshrimp.Backend.Core.Database;
|
||||
using Iceshrimp.Backend.Core.Database.Tables;
|
||||
using Iceshrimp.Backend.Core.Helpers;
|
||||
using Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion;
|
||||
using Iceshrimp.Backend.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Iceshrimp.Backend.Core.Middleware;
|
||||
|
||||
public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) : IMiddleware
|
||||
public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc, MfmConverter mfmConverter) : IMiddleware
|
||||
{
|
||||
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
|
||||
{
|
||||
|
@ -58,6 +59,8 @@ public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) :
|
|||
|
||||
userSvc.UpdateOauthTokenMetadata(oauthToken);
|
||||
ctx.SetOauthToken(oauthToken);
|
||||
|
||||
mfmConverter.SupportsHtmlFormatting = oauthToken.SupportsHtmlFormatting;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue