ssg: StaticSitePost: rename GetNewSafeSlug to GetNewSlug, add safe parameter

This commit is contained in:
Nick Vella 2019-07-16 23:25:54 +10:00
parent 0e36c274e0
commit 8359290301
1 changed files with 6 additions and 2 deletions

View File

@ -123,7 +123,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
/// <returns>The current or new Slug.</returns> /// <returns>The current or new Slug.</returns>
public string EnsureSafeSlug() public string EnsureSafeSlug()
{ {
if (_safeSlug == null || _safeSlug == string.Empty) Slug = GetNewSafeSlug(BlogPost.Slug); if (_safeSlug == null || _safeSlug == string.Empty) Slug = GetNewSlug(BlogPost.Slug, safe: true);
return Slug; return Slug;
} }
@ -150,8 +150,10 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
/// <summary> /// <summary>
/// Generate a slug for this post based on it's title or a preferred slug /// Generate a slug for this post based on it's title or a preferred slug
/// </summary> /// </summary>
/// <param name="preferredSlug">The text to base the preferred slug off of. default: post title</param>
/// <param name="safe">Safe mode; eif true </param>
/// <returns>A safe, on-disk slug for this post</returns> /// <returns>A safe, on-disk slug for this post</returns>
private string GetNewSafeSlug(string preferredSlug) private string GetNewSlug(string preferredSlug, bool safe)
{ {
// Try the filename without a duplicate identifier, then duplicate identifiers up until 999 before throwing an exception // Try the filename without a duplicate identifier, then duplicate identifiers up until 999 before throwing an exception
for(int i = 0; i < 1000; i++) for(int i = 0; i < 1000; i++)
@ -160,6 +162,8 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
string slug = StaticSiteClient.WEB_UNSAFE_CHARS string slug = StaticSiteClient.WEB_UNSAFE_CHARS
.Replace((preferredSlug == string.Empty ? BlogPost.Title : preferredSlug).ToLower(), "") .Replace((preferredSlug == string.Empty ? BlogPost.Title : preferredSlug).ToLower(), "")
.Replace(" ", "-"); .Replace(" ", "-");
if (!safe) return slug; // If unsafe mode, return the generated slug immediately.
if (i > 0) slug += $"-{i}"; if (i > 0) slug += $"-{i}";
if (!File.Exists(GetFilePathForProvidedSlug(slug))) return slug; if (!File.Exists(GetFilePathForProvidedSlug(slug))) return slug;
} }