[backend/masto-client] Render quote uris inline (ISH-177)
This commit is contained in:
parent
abf93138e1
commit
e65c76ca39
2 changed files with 33 additions and 9 deletions
|
@ -29,11 +29,15 @@ public class NoteRenderer(
|
||||||
? await RenderAsync(note.Renote, user, data, --recurse)
|
? await RenderAsync(note.Renote, user, data, --recurse)
|
||||||
: null;
|
: null;
|
||||||
var text = note.Text;
|
var text = note.Text;
|
||||||
|
string? quoteUri = null;
|
||||||
|
|
||||||
if (note is { Renote: not null, IsQuote: true } && text != null)
|
if (note is { Renote: not null, IsQuote: true } && text != null)
|
||||||
{
|
{
|
||||||
var quoteUri = note.Renote?.Url ?? note.Renote?.Uri ?? note.Renote?.GetPublicUriOrNull(config.Value);
|
var qUri = note.Renote?.Url ?? note.Renote?.Uri ?? note.Renote?.GetPublicUriOrNull(config.Value);
|
||||||
if (quoteUri != null)
|
var alt = note.Renote?.Uri;
|
||||||
text += $"\n\nRE: {quoteUri}"; //TODO: render as inline quote
|
|
||||||
|
if (qUri != null && !text.Contains(qUri) && (alt == null || qUri == alt || !text.Contains(alt)))
|
||||||
|
quoteUri = qUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
var liked = data?.LikedNotes?.Contains(note.Id) ??
|
var liked = data?.LikedNotes?.Contains(note.Id) ??
|
||||||
|
@ -69,7 +73,7 @@ public class NoteRenderer(
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
var content = text != null && data?.Source != true
|
var content = text != null && data?.Source != true
|
||||||
? await mfmConverter.ToHtmlAsync(text, mentionedUsers, note.UserHost)
|
? await mfmConverter.ToHtmlAsync(text, mentionedUsers, note.UserHost, quoteUri)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
var account = data?.Accounts?.FirstOrDefault(p => p.Id == note.UserId) ??
|
var account = data?.Accounts?.FirstOrDefault(p => p.Id == note.UserId) ??
|
||||||
|
|
|
@ -54,7 +54,9 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
|
||||||
return parser.Mentions;
|
return parser.Mentions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> ToHtmlAsync(IEnumerable<MfmNode> nodes, List<Note.MentionedUser> mentions, string? host)
|
public async Task<string> ToHtmlAsync(
|
||||||
|
IEnumerable<MfmNode> nodes, List<Note.MentionedUser> mentions, string? host, string? quoteUri = null
|
||||||
|
)
|
||||||
{
|
{
|
||||||
var context = BrowsingContext.New();
|
var context = BrowsingContext.New();
|
||||||
var document = await context.OpenNewAsync();
|
var document = await context.OpenNewAsync();
|
||||||
|
@ -62,15 +64,33 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
|
||||||
|
|
||||||
foreach (var node in nodes) element.AppendNodes(FromMfmNode(document, node, mentions, host));
|
foreach (var node in nodes) element.AppendNodes(FromMfmNode(document, node, mentions, host));
|
||||||
|
|
||||||
|
if (quoteUri != null)
|
||||||
|
{
|
||||||
|
var a = document.CreateElement("a");
|
||||||
|
a.SetAttribute("href", quoteUri);
|
||||||
|
a.TextContent = quoteUri.StartsWith("https://") ? quoteUri[8..] : quoteUri[7..];
|
||||||
|
var quote = document.CreateElement("span");
|
||||||
|
quote.ClassList.Add("quote-inline");
|
||||||
|
quote.AppendChild(document.CreateElement("br"));
|
||||||
|
quote.AppendChild(document.CreateElement("br"));
|
||||||
|
var re = document.CreateElement("span");
|
||||||
|
re.TextContent = "RE: ";
|
||||||
|
quote.AppendChild(re);
|
||||||
|
quote.AppendChild(a);
|
||||||
|
element.AppendChild(quote);
|
||||||
|
}
|
||||||
|
|
||||||
await using var sw = new StringWriter();
|
await using var sw = new StringWriter();
|
||||||
await element.ToHtmlAsync(sw);
|
await element.ToHtmlAsync(sw);
|
||||||
return sw.ToString();
|
return sw.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> ToHtmlAsync(string mfm, List<Note.MentionedUser> mentions, string? host)
|
public async Task<string> ToHtmlAsync(
|
||||||
|
string mfm, List<Note.MentionedUser> mentions, string? host, string? quoteUri = null
|
||||||
|
)
|
||||||
{
|
{
|
||||||
var nodes = MfmParser.Parse(mfm);
|
var nodes = MfmParser.Parse(mfm);
|
||||||
return await ToHtmlAsync(nodes, mentions, host);
|
return await ToHtmlAsync(nodes, mentions, host, quoteUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
private INode FromMfmNode(IDocument document, MfmNode node, List<Note.MentionedUser> mentions, string? host)
|
private INode FromMfmNode(IDocument document, MfmNode node, List<Note.MentionedUser> mentions, string? host)
|
||||||
|
|
Loading…
Add table
Reference in a new issue