From 37707a0712bd56b0bb20f52f7d63f6734c4be370 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 7 Sep 2024 01:56:53 +0200 Subject: [PATCH] [backend/mfm] Only use div as root element for public preview in MfmConverter.ToHtmlAsync --- .../Core/Helpers/LibMfm/Conversion/MfmConverter.cs | 10 +++++----- Iceshrimp.Backend/Pages/Note.cshtml.cs | 6 ++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs b/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs index bf10cb2c..6d3c798e 100644 --- a/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs +++ b/Iceshrimp.Backend/Core/Helpers/LibMfm/Conversion/MfmConverter.cs @@ -59,13 +59,13 @@ public class MfmConverter(IOptions config) } public async Task ToHtmlAsync( - IEnumerable nodes, List mentions, string? host, - string? quoteUri = null, bool quoteInaccessible = false, bool replyInaccessible = false + IEnumerable nodes, List mentions, string? host, string? quoteUri = null, + bool quoteInaccessible = false, bool replyInaccessible = false, bool divAsRoot = false ) { var context = BrowsingContext.New(); var document = await context.OpenNewAsync(); - var element = document.CreateElement("div"); + var element = document.CreateElement(divAsRoot ? "div" : "p"); var nodeList = nodes.ToList(); var hasContent = nodeList.Count > 0; @@ -130,11 +130,11 @@ public class MfmConverter(IOptions config) public async Task ToHtmlAsync( string mfm, List mentions, string? host, string? quoteUri = null, - bool quoteInaccessible = false, bool replyInaccessible = false + bool quoteInaccessible = false, bool replyInaccessible = false, bool divAsRoot = false ) { var nodes = MfmParser.Parse(mfm); - return await ToHtmlAsync(nodes, mentions, host, quoteUri, quoteInaccessible, replyInaccessible); + return await ToHtmlAsync(nodes, mentions, host, quoteUri, quoteInaccessible, replyInaccessible, divAsRoot); } private INode FromMfmNode( diff --git a/Iceshrimp.Backend/Pages/Note.cshtml.cs b/Iceshrimp.Backend/Pages/Note.cshtml.cs index d1bf62a0..f53e0277 100644 --- a/Iceshrimp.Backend/Pages/Note.cshtml.cs +++ b/Iceshrimp.Backend/Pages/Note.cshtml.cs @@ -67,12 +67,14 @@ public class NoteModel( if (Note is { Text: not null }) { - TextContent[Note.Id] = await mfmConverter.ToHtmlAsync(Note.Text, await GetMentions(Note), Note.UserHost); + TextContent[Note.Id] = await mfmConverter.ToHtmlAsync(Note.Text, await GetMentions(Note), Note.UserHost, + divAsRoot: true); if (Note.Renote is { Text: not null }) { TextContent[Note.Renote.Id] = await mfmConverter.ToHtmlAsync(Note.Renote.Text, await GetMentions(Note.Renote), - Note.Renote.UserHost); + Note.Renote.UserHost, + divAsRoot: true); MediaAttachments[Note.Renote.Id] = await GetAttachments(Note.Renote); } }