[frontend/mfm] Implement ruby fn node

This commit is contained in:
pancakes 2024-11-11 17:39:33 +10:00 committed by Laura Hausmann
parent e29a3083f4
commit 90f6456e30
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -1,6 +1,7 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using AngleSharp; using AngleSharp;
using AngleSharp.Dom; using AngleSharp.Dom;
using AngleSharp.Text;
using Iceshrimp.Parsing; using Iceshrimp.Parsing;
using Iceshrimp.Shared.Schemas.Web; using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -79,7 +80,7 @@ public static partial class MfmRenderer
}; };
// @formatter:on // @formatter:on
if (node.Children.Length > 0) if (node.Children.Length > 0 && rendered.ChildNodes.Length == 0)
{ {
foreach (var childNode in node.Children) foreach (var childNode in node.Children)
{ {
@ -277,7 +278,7 @@ public static partial class MfmRenderer
"fg" => MfmFnFg(args, document), "fg" => MfmFnFg(args, document),
"bg" => MfmFnBg(args, document), "bg" => MfmFnBg(args, document),
"border" => MfmFnBorder(args, document), "border" => MfmFnBorder(args, document),
"ruby" => throw new NotImplementedException($"{node.Name}"), "ruby" => MfmFnRuby(node, document),
"unixtime" => throw new NotImplementedException($"{node.Name}"), "unixtime" => throw new NotImplementedException($"{node.Name}"),
_ => throw new NotImplementedException($"{node.Name}") _ => throw new NotImplementedException($"{node.Name}")
}; };
@ -483,4 +484,40 @@ public static partial class MfmRenderer
return el; return el;
} }
private static string? GetNodeText(MfmNodeTypes.MfmNode node)
{
return node switch
{
MfmNodeTypes.MfmTextNode mfmTextNode => mfmTextNode.Text,
_ => null,
};
}
private static INode MfmFnRuby(MfmNodeTypes.MfmFnNode node, IDocument document)
{
var el = document.CreateElement("ruby");
if (node.Children.Length != 1) return el;
var childText = GetNodeText(node.Children[0]);
if (childText == null) return el;
var split = childText.SplitSpaces();
if (split.Length < 2) return el;
el.TextContent = split[0];
var rp1 = document.CreateElement("rp");
rp1.TextContent = "(";
el.AppendChild(rp1);
var rt = document.CreateElement("rt");
rt.TextContent = split[1];
el.AppendChild(rt);
var rp2 = document.CreateElement("rp");
rp1.TextContent = ")";
el.AppendChild(rp2);
return el;
}
} }