From 6b3e8d06df6dd46ad7cc31e374cc55c862ee4e2b Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 24 Feb 2024 00:43:14 +0000 Subject: [PATCH] fix: strip Mastodon's botched formatting in links --- OliverBooth/OliverBooth.csproj | 1 + OliverBooth/Services/MastodonService.cs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/OliverBooth/OliverBooth.csproj b/OliverBooth/OliverBooth.csproj index 1c6ddab..6edb58b 100644 --- a/OliverBooth/OliverBooth.csproj +++ b/OliverBooth/OliverBooth.csproj @@ -11,6 +11,7 @@ + diff --git a/OliverBooth/Services/MastodonService.cs b/OliverBooth/Services/MastodonService.cs index 52ce479..fdc7486 100644 --- a/OliverBooth/Services/MastodonService.cs +++ b/OliverBooth/Services/MastodonService.cs @@ -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(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; } }