Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/NoteBookmark.Api.Tests/Endpoints/PostHtmlExtractionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using FluentAssertions;
using HtmlAgilityPack;
using Xunit;

namespace NoteBookmark.Api.Tests.Endpoints;

public class PostHtmlExtractionTests
{
private static NoteBookmark.Domain.Post Extract(string html)
{
var doc = new HtmlDocument();
doc.LoadHtml(html);
return PostEndpoints.BuildPostFromHtml(doc, "https://example.com/a");
}

[Fact]
public void Title_DecodesNumericEntities()
{
var post = Extract("<html><head><title>Your Skills Don&#39;t Count If Agents Can&#39;t Find Them</title></head></html>");
post.Title.Should().Be("Your Skills Don't Count If Agents Can't Find Them");
}

[Fact]
public void Metadata_DecodesNamedDecimalAndHexEntities()
{
var post = Extract("<html><head><title>A &amp; B &quot;q&quot; &apos;x&apos; &rsquo; &mdash; &#8217; &#x27; &#x2019;</title>" +
"<meta name=\"author\" content=\"Tom &amp; Jerry &#39;s\"><meta name=\"description\" content=\"It&rsquo;s &#x27;ok&#x27;\"></head></html>");
post.Title.Should().Be("A & B \"q\" 'x' ’ — ’ ' ’");
post.Author.Should().Be("Tom & Jerry 's");
post.Excerpt.Should().Be("It’s 'ok'");
}

[Fact]
public void Metadata_CollapsesWhitespace()
{
var post = Extract("<html><head><title>\n Hello&nbsp;&nbsp; \t World \n</title></head></html>");
post.Title.Should().Be("Hello World");
}

[Fact]
public void Metadata_PreservesAngleBrackets()
{
var post = Extract("<html><head><title>Using &lt;T&gt; here</title><meta name=\"description\" content=\"List<string> and &lt;T&gt;\"></head></html>");
post.Title.Should().Be("Using <T> here");
post.Excerpt.Should().Contain("&lt;T&gt;".Replace("&lt;", "<").Replace("&gt;", ">"));
}

[Fact]
public void Title_DecodesOnlyOnce()
{
var post = Extract("<html><head><title>a &amp;#39; b</title></head></html>");
post.Title.Should().Be("a &#39; b");
}

[Fact]
public void MissingNodes_ProduceNull()
{
var post = Extract("<html><head></head></html>");
post.Title.Should().BeNull();
post.Author.Should().BeNull();
post.Excerpt.Should().BeNull();
}
}
20 changes: 17 additions & 3 deletions src/NoteBookmark.Api/PostEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ static Results<Ok<Post>, NotFound, BadRequest> PatchPost(string id, Post post, T
Console.WriteLine($"An error Loading the page URL={url} /n Error: {docloadEx.Message}");
}

return BuildPostFromHtml(doc, url);
}

public static Post BuildPostFromHtml(HtmlDocument doc, string url)
{
var titleNode = doc.DocumentNode.SelectSingleNode("//head/title");
var authorNode = doc.DocumentNode.SelectSingleNode("//meta[@name='author']");
var descriptionNode = doc.DocumentNode.SelectSingleNode("//meta[@name='description']");
Expand All @@ -214,9 +218,9 @@ static Results<Ok<Post>, NotFound, BadRequest> PatchPost(string id, Post post, T
{
Url = url,
Domain = new Uri(url).Host,
Title = titleNode?.InnerText,
Author = authorNode?.GetAttributeValue("content", string.Empty),
Excerpt = descriptionNode?.GetAttributeValue("content", string.Empty),
Title = NormalizeText(titleNode?.InnerText),
Author = NormalizeText(authorNode?.GetAttributeValue("content", string.Empty)),
Excerpt = NormalizeText(descriptionNode?.GetAttributeValue("content", string.Empty)),
PartitionKey = DateTime.UtcNow.ToString("yyyy-MM"),
Date_published = publicationDate.ToString("yyyy-MM-ddTHH:mm:ssZ"),
is_read = false,
Expand All @@ -226,6 +230,16 @@ static Results<Ok<Post>, NotFound, BadRequest> PatchPost(string id, Post post, T
};
return post;
}

private static string? NormalizeText(string? text)
{
if (text is null)
{
return null;
}
var decoded = System.Net.WebUtility.HtmlDecode(text);
return System.Text.RegularExpressions.Regex.Replace(decoded, @"\s+", " ").Trim();
}
}

public record ExtractPostRequest(string url, string? tags = null, string? category = null);
Expand Down