Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
Support for :slug in permalinks (#317)
Browse files Browse the repository at this point in the history
* support :slug in permalink

* support for custom slug from front matter in permalinks
  • Loading branch information
thoemmi authored and Jérémie Bertrand committed Jan 7, 2017
1 parent cc3b456 commit 8ffc7d6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Pretzel.Logic/Templating/Context/LinkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Pretzel.Logic.Extensibility.Extensions;

namespace Pretzel.Logic.Templating.Context
{
Expand Down Expand Up @@ -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"))
{
Expand Down Expand Up @@ -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;
}
}
}
4 changes: 3 additions & 1 deletion src/Pretzel.Tests/Templating/Context/LinkHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
15 changes: 15 additions & 0 deletions src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 8ffc7d6

Please sign in to comment.