Skip to content

Commit

Permalink
Merge pull request #444 from poppastring/301-redirects-url-names
Browse files Browse the repository at this point in the history
Just in case you love ASPX extensions

Fix for #439
  • Loading branch information
poppastring authored Aug 10, 2020
2 parents 64170c0 + b44f231 commit aabc55a
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 49 deletions.
2 changes: 2 additions & 0 deletions source/DasBlog.Services/ConfigFile/Interfaces/ISiteConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ public interface ISiteConfig

string CheesySpamA { get; set; }

bool UseAspxExtension { get; set; }

[XmlAnyElement]
XmlElement[] anyElements { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions source/DasBlog.Services/ConfigFile/SiteConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,7 @@ public SiteConfig() { }

[XmlElement]
public ValidCommentTags [] ValidCommentTags { get; set; }

public bool UseAspxExtension { get; set; }
}
}
5 changes: 4 additions & 1 deletion source/DasBlog.Services/IDasBlogSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using DasBlog.Core.Security;
using DasBlog.Services.ConfigFile.Interfaces;
using newtelligence.DasBlog.Runtime;
using NodaTime;
using Org.BouncyCastle.Crypto.Agreement.Srp;

namespace DasBlog.Services
{
Expand All @@ -26,7 +28,7 @@ public interface IDasBlogSettings
string RelativeToRoot(string relative);
string GetBaseUrl();
string GetPermaLinkUrl(string entryId);
string GetPermaTitle(string title);
string GetPermaTitle(string titleurl);
string GetTrackbackUrl(string entryId);
string GetEntryCommentsRssUrl(string entryId);
string GetCommentViewUrl(string entryId);
Expand All @@ -41,5 +43,6 @@ public interface IDasBlogSettings
bool AreCommentsPermitted(DateTime blogpostdate);
string CompressTitle(string title);
bool IsAdmin(string gravatarhash);
string GeneratePostUrl(Entry entry);
}
}
23 changes: 22 additions & 1 deletion source/DasBlog.Tests/UnitTests/DasBlogSettingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NodaTime;
using System.Xml.Serialization;
using DasBlog.Services;
using newtelligence.DasBlog.Runtime;

namespace DasBlog.Tests.UnitTests
{
Expand Down Expand Up @@ -216,12 +217,32 @@ public bool AreCommentsPermitted(DateTime blogpostdate)

public string CompressTitle(string title)
{
throw new NotImplementedException();
string titlePermalink = title.Trim().ToLower();

titlePermalink = titlePermalink.Replace("+", SiteConfiguration.TitlePermalinkSpaceReplacement);

return titlePermalink;
}

public bool IsAdmin(string gravatarhash)
{
throw new NotImplementedException();
}

public string GeneratePostUrl(Entry entry)
{
string link;

if (SiteConfiguration.EnableTitlePermaLinkUnique)
{
link = GetPermaTitle(entry.CompressedTitleUnique);
}
else
{
link = GetPermaTitle(entry.CompressedTitle);
}

return link;
}
}
}
1 change: 1 addition & 0 deletions source/DasBlog.Tests/UnitTests/SiteConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public class SiteConfigTest : ISiteConfig
public XmlElement[] anyElements { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public XmlAttribute[] anyAttributes { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool ShowItemSummaryInAggregatedViews { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool UseAspxExtension { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
ValidCommentTags[] ISiteConfig.ValidCommentTags { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
}
5 changes: 3 additions & 2 deletions source/DasBlog.Web.Repositories/BlogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public BlogManager( ILogger<BlogManager> logger, IDasBlogSettings dasBlogSetting
/// <param name="dt">if non-null then the post must be dated on that date</param>
public Entry GetBlogPost(string posttitle, DateTime? dt)
{
posttitle = posttitle.Replace(dasBlogSettings.SiteConfiguration.TitlePermalinkSpaceReplacement,string.Empty);
posttitle = posttitle.Replace(dasBlogSettings.SiteConfiguration.TitlePermalinkSpaceReplacement,string.Empty)
.Replace(".aspx", string.Empty);

if (dt == null)
{
Expand All @@ -52,7 +53,7 @@ public Entry GetBlogPost(string posttitle, DateTime? dt)
{
var entries = dataService.GetEntriesForDay(dt.Value, null, null, 1, 10, null);

return entries.FirstOrDefault(e => dasBlogSettings.GetPermaTitle(e.CompressedTitle)
return entries.FirstOrDefault(e => dasBlogSettings.GeneratePostUrl(e)
.Replace(dasBlogSettings.SiteConfiguration.TitlePermalinkSpaceReplacement, string.Empty) == posttitle);
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/DasBlog.Web.Repositories/SiteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public urlset GetGoogleSiteMap()
}

//then add permalinks
var permaPage = new url(dasBlogSettings.RelativeToRoot(dasBlogSettings.GetPermaTitle(e.CompressedTitle)), e.CreatedLocalTime, freq, 0.9M);
var permaPage = new url(dasBlogSettings.RelativeToRoot(dasBlogSettings.GeneratePostUrl(e)), e.CreatedLocalTime, freq, 0.9M);
root.url.Add(permaPage);
}
}
Expand Down
6 changes: 3 additions & 3 deletions source/DasBlog.Web.Repositories/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private RssRoot GetRssCore(string category, int maxDayCount, int maxEntryCount)
item.Guid = new DasBlog.Services.Rss.Rss20.Guid();
item.Guid.IsPermaLink = false;
item.Guid.Text = dasBlogSettings.GetPermaLinkUrl(entry.EntryId);
item.Link = dasBlogSettings.RelativeToRoot(dasBlogSettings.GetPermaTitle(entry.CompressedTitle));
item.Link = dasBlogSettings.RelativeToRoot(dasBlogSettings.GeneratePostUrl(entry));
User user = dasBlogSettings.GetUser(entry.Author);

XmlElement trackbackPing = doc2.CreateElement("trackback", "ping", "http://madskills.com/public/xml/rss/module/trackback/");
Expand Down Expand Up @@ -226,7 +226,7 @@ private RssRoot GetRssCore(string category, int maxDayCount, int maxEntryCount)
if (entry.AllowComments)
{
XmlElement commentApi = doc2.CreateElement("wfw", "comment", "http://wellformedweb.org/CommentAPI/");
commentApi.InnerText = dasBlogSettings.GetCommentViewUrl(entry.CompressedTitle.Replace("+", dasBlogSettings.SiteConfiguration.TitlePermalinkSpaceReplacement));
commentApi.InnerText = dasBlogSettings.GetCommentViewUrl(dasBlogSettings.GeneratePostUrl(entry));
anyElements.Add(commentApi);
}

Expand All @@ -242,7 +242,7 @@ private RssRoot GetRssCore(string category, int maxDayCount, int maxEntryCount)
slashComments.InnerText = commentsCount.ToString();
anyElements.Add(slashComments);
}
item.Comments = dasBlogSettings.GetCommentViewUrl(entry.CompressedTitle.Replace("+",dasBlogSettings.SiteConfiguration.TitlePermalinkSpaceReplacement));
item.Comments = dasBlogSettings.GetCommentViewUrl(dasBlogSettings.GeneratePostUrl(entry));
}
item.Language = entry.Language;

Expand Down
2 changes: 2 additions & 0 deletions source/DasBlog.Web.UI/Config/site.Development.config
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@

<ShowCommentsWhenViewingEntry>true</ShowCommentsWhenViewingEntry>
<TitlePermalinkSpaceReplacement>-</TitlePermalinkSpaceReplacement>
<UseAspxExtension>false</UseAspxExtension>


<EntryEditControl>TinyMce</EntryEditControl>
<!-- TinyMce, NicEdit, TextArea or Froala -->
Expand Down
1 change: 1 addition & 0 deletions source/DasBlog.Web.UI/Config/site.config
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@

<ShowCommentsWhenViewingEntry>true</ShowCommentsWhenViewingEntry>
<TitlePermalinkSpaceReplacement>-</TitlePermalinkSpaceReplacement>
<UseAspxExtension>false</UseAspxExtension>

<EntryEditControl>TinyMce</EntryEditControl>
<!-- TinyMce, NicEdit, TextArea or Froala -->
Expand Down
2 changes: 1 addition & 1 deletion source/DasBlog.Web.UI/Controllers/BlogPostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public IActionResult Post(string posttitle, string day, string month, string yea
};
pvm.Comments = lcvm;

if (httpContextAccessor.HttpContext.Request.Path.Value.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
if (!dasBlogSettings.SiteConfiguration.UseAspxExtension && httpContextAccessor.HttpContext.Request.Path.Value.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
{
return RedirectPermanent(pvm.PermaLink);
}
Expand Down
2 changes: 1 addition & 1 deletion source/DasBlog.Web.UI/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private CategoryListViewModel GetCategoryListFromCategoryManager(string category

var categoryTile = categoryManager.GetCategoryTitle(category);

var viewModel = CategoryListViewModel.Create(entryList, categoryTile);
var viewModel = CategoryListViewModel.Create(entryList, dasBlogSettings, categoryTile);

DefaultPage(CATEGORY);
return viewModel;
Expand Down
18 changes: 1 addition & 17 deletions source/DasBlog.Web.UI/Mappers/ProfilePost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ProfilePost(IDasBlogSettings dasBlogSettings)
.ForMember(dest => dest.AllowComments, opt => opt.MapFrom(src => src.AllowComments))
.ForMember(dest => dest.IsPublic, opt => opt.MapFrom(src => src.IsPublic))
.ForMember(dest => dest.Syndicated, opt => opt.MapFrom(src => src.Syndicated))
.ForMember(dest => dest.PermaLink, opt => opt.MapFrom(src => MakePermaLink(src)))
.ForMember(dest => dest.PermaLink, opt => opt.MapFrom(src => _dasBlogSettings.GeneratePostUrl(src)))
.ForMember(dest => dest.ImageUrl, opt => opt.MapFrom(src => src.Content.FindFirstImage()))
.ForMember(dest => dest.VideoUrl, opt => opt.MapFrom(src => src.Content.FindFirstYouTubeVideo()))
.ForMember(dest => dest.CreatedDateTime, opt => opt.MapFrom(src => src.CreatedLocalTime))
Expand Down Expand Up @@ -105,21 +105,5 @@ private IList<CategoryViewModel> ConvertCategory(string category)
CategoryUrl = _dasBlogSettings.CompressTitle(c) })
.ToList();
}

private string MakePermaLink(Entry entry)
{
string link;

if (_dasBlogSettings.SiteConfiguration.EnableTitlePermaLinkUnique)
{
link = _dasBlogSettings.GetPermaTitle(entry.CompressedTitleUnique);
}
else
{
link = _dasBlogSettings.GetPermaTitle(entry.CompressedTitle);
}

return link;
}
}
}
4 changes: 4 additions & 0 deletions source/DasBlog.Web.UI/Models/AdminViewModels/SiteViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ public class SiteViewModel
[Description("")]
public bool ShowCommentCount { get; set; }

[DisplayName("Use ASPX extensions (will not 301 redirect)")]
[Description("")]
public bool UseAspxExtension { get; set; }


public bool EntryTitleAsLink { get; set; }
public bool ObfuscateEmail { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using DasBlog.Services;
using newtelligence.DasBlog.Runtime;

namespace DasBlog.Web.Models.BlogViewModels
Expand All @@ -7,7 +8,7 @@ public class CategoryListViewModel
{
public SortedDictionary<string, List<CategoryPostItem>> Categories { get; protected set; } = new SortedDictionary<string, List<CategoryPostItem>>();

public static CategoryListViewModel Create(EntryCollection entries, string categoryName = "")
public static CategoryListViewModel Create(EntryCollection entries, IDasBlogSettings dasBlogSettings, string categoryName = "")
{
var viewModel = new CategoryListViewModel();
foreach (var entry in entries)
Expand All @@ -24,7 +25,7 @@ public static CategoryListViewModel Create(EntryCollection entries, string categ

foreach (var category in categories)
{
var archiveItem = CategoryPostItem.CreateFromEntry(entry);
var archiveItem = CategoryPostItem.CreateFromEntry(entry, dasBlogSettings);
archiveItem.Category = category;
if (viewModel.Categories.ContainsKey(category))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DasBlog.Services;
using newtelligence.DasBlog.Runtime;

namespace DasBlog.Web.Models.BlogViewModels
Expand All @@ -16,13 +17,13 @@ public class CategoryPostItem

public DateTime Date { get; set; }

public static CategoryPostItem CreateFromEntry(Entry entry)
public static CategoryPostItem CreateFromEntry(Entry entry, IDasBlogSettings dasBlogSettings)
{
return new CategoryPostItem
{
Category = entry.GetSplitCategories().FirstOrDefault(),
BlogTitle = entry.Title,
BlogId = entry.CompressedTitle,
BlogId = dasBlogSettings.GeneratePostUrl(entry),
Date = entry.CreatedLocalTime

};
Expand Down
51 changes: 34 additions & 17 deletions source/DasBlog.Web.UI/Settings/DasBlogSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,6 @@ public static string GetPermaLinkUrl(string entryId, string root)
return RelativeToRoot("post/" + entryId, root);
}

public string GetPermaTitle(string title)
{
string titlePermalink = title.Trim().ToLower();

titlePermalink = titlePermalink.Replace("+", SiteConfiguration.TitlePermalinkSpaceReplacement);

return titlePermalink;
}

public static string GetPermaTitle(string title, string permaLinkSpaceReplacement)
{
string titlePermalink = title.Trim().ToLower();

titlePermalink = titlePermalink.Replace("+", permaLinkSpaceReplacement);

return titlePermalink;
}
public string GetCommentViewUrl(string entryId)
{
return RelativeToRoot(entryId) + $"/comments#{Constants.CommentsStartId}";
Expand Down Expand Up @@ -280,11 +263,45 @@ public static string GetRsdUrl(string root)
return RelativeToRoot("feed/rsd", root);
}

public string GetPermaTitle(string titleurl)
{
var titlePermalink = titleurl.Trim();

if (!SiteConfiguration.UseAspxExtension)
{
titlePermalink = titlePermalink.ToLower();

titlePermalink = titlePermalink.Replace("+", SiteConfiguration.TitlePermalinkSpaceReplacement);
}
else
{
titlePermalink = string.Format("{0}.aspx", titlePermalink.Replace("+", string.Empty));
}

return titlePermalink;
}

public string CompressTitle(string title)
{
return Entry.InternalCompressTitle(title, SiteConfiguration.TitlePermalinkSpaceReplacement).ToLower();
}

public string GeneratePostUrl(Entry entry)
{
string link;

if (SiteConfiguration.EnableTitlePermaLinkUnique)
{
link = GetPermaTitle(entry.CompressedTitleUnique);
}
else
{
link = GetPermaTitle(entry.CompressedTitle);
}

return link;
}

public bool IsAdmin(string gravatarhashid)
{
return (Utils.GetGravatarHash(SecurityConfiguration.Users.First().EmailAddress) == gravatarhashid);
Expand Down
7 changes: 7 additions & 0 deletions source/DasBlog.Web.UI/Views/Admin/Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@

</div>

<div class="dbc-form-check row">

@Html.LabelFor(m => @Model.SiteConfig.UseAspxExtension, null, new { @class = "dbc-col-form-label col-3" })
@Html.CheckBoxFor(m => @Model.SiteConfig.UseAspxExtension, new { @class = "dbc-form-check-input" })

</div>

<h3>Internal</h3>

<div class="dbc-form-group row">
Expand Down
2 changes: 1 addition & 1 deletion source/DasBlog.Web.UI/Views/Category/Category.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@foreach (var post in category.Value)
{
<div>
<a href="@dasBlogSettings.RelativeToRoot(@dasBlogSettings.GetPermaTitle(post.BlogId))">@post.BlogTitle</a><br />
<a href="@dasBlogSettings.RelativeToRoot(post.BlogId)">@post.BlogTitle</a><br />
</div>
}
}
Expand Down

0 comments on commit aabc55a

Please sign in to comment.