fixed Issues with article slug generation and article lookup

This commit is contained in:
Mia Rose Winter 2024-04-30 13:26:02 +02:00
parent 5c7bf8be04
commit 0ea1251150
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 13 additions and 8 deletions

View file

@ -186,7 +186,7 @@
if (Id is not null) {
Article = query.AsSingleQuery().FirstOrDefault(a => a.Id == Id);
} else if (Date is { } date && Title is { } title) {
string? slug = TitleEncoded == null ? null : Uri.EscapeDataString(TitleEncoded);
string? slug = TitleEncoded == null ? null : Uri.EscapeDataString(TitleEncoded.Replace("-", " ")).Replace("%20", "-");
Article = query.AsSingleQuery().FirstOrDefault(a =>
a.PublishDate.Date == date.Date
&& (slug != null && a.Slug == slug || a.Title.ToLower() == title));

View file

@ -188,13 +188,18 @@
Article.PublishDate = Model.PublishDate.Value;
if (Article.Status is ArticleStatus.Published && Article.PublishDate < DateTimeOffset.Now) {
// can't change slugs when the article is public
} else if (!string.IsNullOrWhiteSpace(Model.Slug)) {
Article.Slug = WebUtility.UrlEncode(Model.Slug);
} else if (string.IsNullOrWhiteSpace(Article.Slug)) {
Article.Slug = Uri.EscapeDataString(Article.Title.ToLowerInvariant())
.Replace("-", "+")
.Replace("%20", "-");
Article.Slug = Article.Slug[..Math.Min(64, Article.Slug.Length)];
} else if (!string.IsNullOrWhiteSpace(Model.Slug) || string.IsNullOrWhiteSpace(Article.Slug)) {
string baseSlug = Model.Slug ?? Article.Title;
baseSlug = baseSlug.ToLowerInvariant()[..Math.Min(64, baseSlug.Length)];
string slug = Uri.EscapeDataString(baseSlug).Replace("-", "+").Replace("%20", "-");
// if our escaping increases the slug length, there is a chance it ends with an escape
// character, so if this overshoot is not divisible by 3, then we risk cutting of the
// escape character, so we need to remove it in it's entirely if that's the case
int escapeTrimOvershoot = Math.Max(0, 3 - (slug.Length - baseSlug.Length) % 3);
// if the slug already fits 64 character, there will be no cutoff in the next operation anyway,
// so we don't need to fix what is described in the previous comment
if (slug.Length <= 64) escapeTrimOvershoot = 0;
Article.Slug = slug[..Math.Min(slug.Length, 64 - escapeTrimOvershoot)];
Model.Slug = Article.Slug;
}