Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/search-engine-friendly-url #317

Merged
Merged
Show file tree
Hide file tree
Changes from 10 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
34 changes: 33 additions & 1 deletion src/LinkDotNet.Blog.Domain/BlogPost.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LinkDotNet.Blog.Domain;

Expand Down Expand Up @@ -35,6 +38,35 @@ private BlogPost()

public string TagsAsString => Tags is null ? string.Empty : string.Join(", ", Tags);

public string SearchEngineFriendlyUrl => GenerateSearchEngineFriendlyUrl();

private string GenerateSearchEngineFriendlyUrl()
{
// Remove all accents and make the string lower case.
if (string.IsNullOrWhiteSpace(Title))
return Title;

Title = Title.Normalize(NormalizationForm.FormD);
char[] chars = Title
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c)
!= UnicodeCategory.NonSpacingMark).ToArray();

Title = new string(chars).Normalize(NormalizationForm.FormC);

string SearchEngineFriendlyTitle = Title.ToLower(CultureInfo.CurrentCulture);

// Remove all special characters from the string.
SearchEngineFriendlyTitle = Regex.Replace(SearchEngineFriendlyTitle, @"[^A-Za-z0-9\s-]", "");

// Remove all additional spaces in favour of just one.
SearchEngineFriendlyTitle = Regex.Replace(SearchEngineFriendlyTitle, @"\s+", " ").Trim();

// Replace all spaces with the hyphen.
SearchEngineFriendlyTitle = Regex.Replace(SearchEngineFriendlyTitle, @"\s", "-");

return $"{Id}/{SearchEngineFriendlyTitle}";
}

public static BlogPost Create(
string title,
string shortDescription,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LinkDotNet.Blog.Domain;
using LinkDotNet.Blog.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
8 changes: 4 additions & 4 deletions src/LinkDotNet.Blog.Web/Features/Archive/ArchivePage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<ul class="ps-5">
@foreach (var blogPost in yearGroup.OrderByDescending(b => b.UpdatedDate))
{
<li class="pt-1"><a href="/blogPost/@blogPost.Id">@blogPost.Title</a></li>
<li class="pt-1"><a href="/blogPost/@blogPost.SearchEngineFriendlyUrl">@blogPost.Title</a></li>
}
</ul>
}
}
</div >
</div>

@code {
private IReadOnlyCollection<IGrouping<int, BlogPostPerYear>> blogPostsPerYear;
Expand All @@ -35,7 +35,7 @@
protected override async Task OnInitializedAsync()
{
var blogPosts = await Repository.GetAllByProjectionAsync(
p => new BlogPostPerYear(p.Id, p.Title, p.UpdatedDate),
p => new BlogPostPerYear(p.Id, p.SearchEngineFriendlyUrl, p.Title, p.UpdatedDate),
p => p.IsPublished);
blogPostCount = blogPosts.Count;
blogPostsPerYear = blogPosts
Expand All @@ -44,5 +44,5 @@
.ToImmutableArray();
}

private sealed record BlogPostPerYear(string Id, string Title, DateTime UpdatedDate);
private sealed record BlogPostPerYear(string Id, string SearchEngineFriendlyUrl, string Title, DateTime UpdatedDate);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using System.Net
@using System.Net
@using System.Text.RegularExpressions
@using System.Web
@using LinkDotNet.Blog.Domain
Expand Down Expand Up @@ -43,7 +43,7 @@
<h2></h2>
<p>@MarkdownConverter.ToMarkupString(BlogPost.ShortDescription)</p>
<p class="read-more">
<a href="/blogPost/@BlogPost.Id" aria-label="@BlogPost.Title">Read the whole article</a>
<a href="/blogPost/@BlogPost.SearchEngineFriendlyUrl" aria-label="@BlogPost.Title">Read the whole article</a>
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/blogPost/{blogPostId}"
@page "/blogPost/{blogPostId}/{searchEngineFriendlyTitle?}"
@using Markdig
@using LinkDotNet.Blog.Domain
@using LinkDotNet.Blog.Infrastructure.Persistence
Expand Down Expand Up @@ -72,6 +72,9 @@ else
[Parameter]
public string BlogPostId { get; set; }

[Parameter]
public string SearchEngineFriendlyTitle { get; set; }

private string OgDataImage => BlogPost.PreviewImageUrlFallback ?? BlogPost.PreviewImageUrl;

private BlogPost BlogPost { get; set; }
Expand Down