Permalink
Browse files

Support for :slug in permalinks (#317)

* support :slug in permalink

* support for custom slug from front matter in permalinks
  • Loading branch information...
1 parent cc3b456 commit 8ffc7d64443e10ce2a0e2336afc65fdc4b9ea065 @thoemmi thoemmi committed with laedit Jan 7, 2017
@@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
+using Pretzel.Logic.Extensibility.Extensions;
namespace Pretzel.Logic.Templating.Context
{
@@ -52,6 +53,7 @@ public string EvaluatePermalink(string permalink, Page page)
permalink = permalink.Replace(":short_year", page.Date.ToString("yy"));
permalink = permalink.Replace(":i_month", page.Date.Month.ToString());
permalink = permalink.Replace(":i_day", page.Date.Day.ToString());
+ permalink = permalink.Replace(":slug", SlugifyFilter.Slugify(GetSlugFromFrontMatter(page) ?? GetTitle(page.File)));
if (permalink.Contains(":category"))
{
@@ -113,5 +115,12 @@ private string GetPageTitle(string file)
{
return Path.GetFileNameWithoutExtension(file);
}
+
+ private string GetSlugFromFrontMatter(Page page)
+ {
+ object slug;
+ page.Bag.TryGetValue("slug", out slug);
+ return slug as string;
+ }
}
}
@@ -69,14 +69,16 @@ public void EvaluateLink_url_is_well_formatted(string filePath, string expectedU
[InlineData("/:category3/:title.html", "/foobar-baz.html", "cat1,cat2")]
[InlineData("/:categories/:title/", "/cat1/cat2/foobar-baz/", "cat1,cat2")]
[InlineData("/:categories/:title", "/cat1/cat2/foobar-baz/", "cat1,cat2")]
+ [InlineData("/:year-:month-:day/:slug.html", "/2015-03-09/foobar-baz.html", "")]
[Theory]
public void EvaluatePermalink_url_is_well_formatted(string permalink, string expectedUrl, string categories)
{
var page = new Page
{
Categories = categories == null ? Enumerable.Empty<string>() : categories.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
Date = new DateTime(2015, 03, 09),
- File = @"C:\temp\2015-03-09-foobar-baz.md"
+ File = @"C:\temp\2015-03-09-foobar-baz.md",
+ Title = "Foobar baz"
};
Assert.Equal(expectedUrl, LinkHelper.EvaluatePermalink(permalink, page));
@@ -1129,6 +1129,7 @@ public void permalink_with_folder_categories_frontmatter_only()
[InlineData("/:category1/:title.html", "/cat1/foobar-baz.html", "cat1,cat2")]
[InlineData("/:category2/:title.html", "/cat2/foobar-baz.html", "cat1,cat2")]
[InlineData("/:category3/:title.html", "/foobar-baz.html", "cat1,cat2")]
+ [InlineData("/:year-:month-:day/:slug.html", "/2015-03-09/foobar-baz.html", "")]
[Theory]
public void permalink_is_well_formatted(string permalink, string expectedUrl, string categories)
{
@@ -1143,6 +1144,20 @@ public void permalink_is_well_formatted(string permalink, string expectedUrl, st
Assert.Equal(expectedUrl, firstPost.Url);
}
+ [Fact]
+ public void permalink_supports_custom_slug()
+ {
+ var generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new ConfigurationMock(string.Format("permalink: {0}", "/:year-:month-:day/:slug.html")));
+ fileSystem.AddFile(@"C:\TestSite\_posts\2015-03-09-foobar-baz.md", new MockFileData(@"---
+slug: my-slug
+---# Title"));
+
+ // act
+ var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false);
+ var firstPost = siteContext.Posts.First();
+ Assert.Equal("/2015-03-09/my-slug.html", firstPost.Url);
+ }
+
private class BeforeProcessingTransformMock : IBeforeProcessingTransform
{
public int PostCount = 0;
@@ -1984,6 +1984,7 @@ public void The_File_Should_Display_The_Next_Page_Url()
[InlineData("/:category1/:title.html", @"cat1/\foobar-baz.html", "cat1,cat2")]
[InlineData("/:category2/:title.html", @"cat2\foobar-baz.html", "cat1,cat2")]
[InlineData("/:category3/:title.html", @"foobar-baz.html", "cat1,cat2")]
+ [InlineData("/:year-:month-:day/:slug.html", @"2015-03-09\foobar-baz.html", "")]
[Theory]
public void Permalink_generates_expected_file_path(string permalink, string expectedUrl, string categories)
{

0 comments on commit 8ffc7d6

Please sign in to comment.