Skip to content

Commit

Permalink
fix!: improve slug generation (#51)
Browse files Browse the repository at this point in the history
- will not remove periods from the permalink
- will turn underscores into dashes
- day and month will be padded with 0
  • Loading branch information
mhanberg committed Dec 30, 2023
1 parent b4e15e2 commit bbc702d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/tableau/extensions/page_extension/pages/page.ex
Expand Up @@ -50,7 +50,8 @@ defmodule Tableau.PageExtension.Pages.Page do
path
|> String.replace(Map.keys(vars), &to_string(Map.fetch!(vars, &1)))
|> String.replace(" ", "-")
|> String.replace(~r/[^[:alnum:]\/\-]/, "")
|> String.replace("_", "-")
|> String.replace(~r/[^[:alnum:]\/\-.]/, "")
|> String.downcase()
end
end
7 changes: 4 additions & 3 deletions lib/tableau/extensions/post_extension/posts/post.ex
Expand Up @@ -51,15 +51,16 @@ defmodule Tableau.PostExtension.Posts.Post do
attrs
|> Map.new(fn {k, v} -> {":#{k}", v} end)
|> Map.merge(%{
":day" => attrs.date.day,
":month" => attrs.date.month,
":day" => attrs.date.day |> to_string() |> String.pad_leading(2, "0"),
":month" => attrs.date.month |> to_string() |> String.pad_leading(2, "0"),
":year" => attrs.date.year
})

path
|> String.replace(Map.keys(vars), &to_string(Map.fetch!(vars, &1)))
|> String.replace(" ", "-")
|> String.replace(~r/[^[:alnum:]\/\-]/, "")
|> String.replace("_", "-")
|> String.replace(~r/[^[:alnum:]\/\-.]/, "")
|> String.downcase()
end
end
6 changes: 3 additions & 3 deletions test/tableau/extensions/post_extension/posts/post_test.exs
Expand Up @@ -26,16 +26,16 @@ defmodule Tableau.PostExtension.Posts.PostTest do
Post.build(
"some/file/name.md",
%{
title: "foo man chu",
title: "foo man chu_foo.js",
type: "articles",
permalink: "/:year/:month/:day/:title",
layout: Some.Layout,
date: "2023-10-31 00:01:00"
date: "2023-02-01 00:01:00"
},
"hi"
)

assert %{permalink: "/2023/10/31/foo-man-chu"} = actual
assert %{permalink: "/2023/02/01/foo-man-chu-foo.js"} = actual
end
end
end

0 comments on commit bbc702d

Please sign in to comment.