fix: strip Mastodon's botched formatting in links

This commit is contained in:
Oliver Booth 2024-02-24 00:43:14 +00:00
parent 0aa9754714
commit 6b3e8d06df
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
2 changed files with 13 additions and 1 deletions

View File

@ -11,6 +11,7 @@
<PackageReference Include="Alexinea.Extensions.Configuration.Toml" Version="7.0.0"/>
<PackageReference Include="AspNetCore.ReCaptcha" Version="1.7.0"/>
<PackageReference Include="BCrypt.Net-Core" Version="1.6.0"/>
<PackageReference Include="HtmlAgilityPack" Version="1.11.59"/>
<PackageReference Include="Humanizer.Core" Version="2.14.1"/>
<PackageReference Include="MailKit" Version="4.3.0"/>
<PackageReference Include="MailKitSimplified.Sender" Version="2.8.0"/>

View File

@ -1,5 +1,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using HtmlAgilityPack;
using OliverBooth.Data.Mastodon;
namespace OliverBooth.Services;
@ -11,6 +12,7 @@ internal sealed class MastodonService : IMastodonService
Converters = { new JsonStringEnumConverter() },
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
private readonly HttpClient _httpClient;
public MastodonService(HttpClient httpClient)
@ -30,6 +32,15 @@ internal sealed class MastodonService : IMastodonService
using HttpResponseMessage response = _httpClient.Send(request);
using var stream = response.Content.ReadAsStream();
var statuses = JsonSerializer.Deserialize<MastodonStatus[]>(stream, JsonSerializerOptions);
return statuses?[0]!;
MastodonStatus status = statuses?[0]!;
var document = new HtmlDocument();
document.LoadHtml(status.Content);
HtmlNodeCollection links = document.DocumentNode.SelectNodes("//a");
foreach (HtmlNode link in links) link.InnerHtml = link.InnerText;
status.Content = document.DocumentNode.OuterHtml;
return status;
}
}