[backend/mfm] Only use div as root element for public preview in MfmConverter.ToHtmlAsync

This commit is contained in:
Laura Hausmann 2024-09-07 01:56:53 +02:00
parent 63eabbd8d0
commit 37707a0712
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 9 additions and 7 deletions

View file

@ -59,13 +59,13 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
}
public async Task<string> ToHtmlAsync(
IEnumerable<MfmNode> nodes, List<Note.MentionedUser> mentions, string? host,
string? quoteUri = null, bool quoteInaccessible = false, bool replyInaccessible = false
IEnumerable<MfmNode> nodes, List<Note.MentionedUser> 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.InstanceSection> config)
public async Task<string> ToHtmlAsync(
string mfm, List<Note.MentionedUser> 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(

View file

@ -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);
}
}