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

Minicorefixer #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/Controllers/BlogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ public async Task<IActionResult> UpdatePost(Post post)
}

var existing = await this.blog.GetPostById(post.ID).ConfigureAwait(false) ?? post;
var existingPostWithSameSlug = await this.blog.GetPostBySlug(existing.Slug).ConfigureAwait(true);

if (existingPostWithSameSlug != null && existingPostWithSameSlug.ID != post.ID)
{

existing.Slug = Models.Post.CreateSlug(post.Title + DateTime.UtcNow.ToString("yyyyMMddHHmm"),50);
}
string categories = this.Request.Form[Constants.categories];
string tags = this.Request.Form[Constants.tags];

Expand Down
9 changes: 8 additions & 1 deletion src/Models/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ public class Post
public string Title { get; set; } = string.Empty;

[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "The slug should be lower case.")]
public static string CreateSlug(string title)
public static string CreateSlug(string title, int maxLength = 50)
{

title = title?.ToLowerInvariant().Replace(
Constants.Space, Constants.Dash, StringComparison.OrdinalIgnoreCase) ?? string.Empty;
title = RemoveDiacritics(title);
title = RemoveReservedUrlCharacters(title);

// control of character limitations
if (title.Length > maxLength)
{
title = title.Substring(0, maxLength);
}

return title.ToLowerInvariant();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Views/Blog/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<br />

<label asp-for="@Model.Slug" class="label">Slug</label>
<input asp-for="@Model.Slug" placeholder="The URL name" aria-describedby="desc_slug" />
<input asp-for="@Model.Slug" placeholder="The URL name" maxlength="50" aria-describedby="desc_slug" />
<span class="desc" id="desc_slug">The part of the URL that identifies this blog post</span>
<br />

Expand Down