diff --git a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs index 944e4b11..a14ebca5 100644 --- a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs @@ -57,12 +57,12 @@ public static class ServiceExtensions .AddScoped() .AddScoped() .AddScoped() - .AddScoped(); + .AddScoped() + .AddScoped(); // Singleton = instantiated once across application lifetime services .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs b/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs index d9a3de34..7f9431e1 100644 --- a/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs +++ b/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs @@ -17,6 +17,8 @@ namespace Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion; public class MfmConverter(IOptions config) { + public bool SupportsHtmlFormatting { private get; set; } = true; + public static async Task FromHtmlAsync(string? html, List? mentions = null) { if (html == null) return null; @@ -77,7 +79,8 @@ public class MfmConverter(IOptions 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) } 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) } 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) } 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) { 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); + } } \ No newline at end of file diff --git a/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs b/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs index 74f957a7..81121e69 100644 --- a/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs +++ b/Iceshrimp.Backend/Core/Middleware/AuthenticationMiddleware.cs @@ -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 {