BeaconCMS/beacon

`generate_page_path` lifecycle

Open

#236 opened on May 4, 2023

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Elixir (120 forks)batch import
area:contentenhancementgood first issue

Repository metrics

Stars
 (1,194 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Add a Lifecycle Stage named :generate_page_path to allow generating page.path with custom rules.

Users can already input a page's path at https://github.com/BeaconCMS/beacon/blob/7790eb72769a026c0bdfc3167aab394a9b73ce91/lib/beacon_web/live/admin/page_live/page_editor_live.html.heex#L22

but inputting long urls manually is not viable for eg: "/blog/2023/01/31/looking-back-at-the-first-year-of-creating-the-dockyard-academy". It's better to provide a button to generate such paths automatically, which would call Beacon.Lifecycle.generate_path(page) which has a default implementation, similar to how it's done here https://github.com/BeaconCMS/beacon/blob/7790eb72769a026c0bdfc3167aab394a9b73ce91/lib/beacon/config.ex#L140

A good start point for that function is:

def generate_path(%Page{title: title}) when is_binary(title) do
  title
  |> String.downcase()
  |> remove_non_printable()
  |> remove_special_characters()
  |> remove_leading_trailing_delimiter()
end

def generate_path(_page), do: ""

defp remove_non_printable(field), do: String.replace(field, ~r/[^\x00-\x7f]+/, "")
defp remove_special_characters(field), do: String.replace(field, ~r/[^\w]+/, "-")
defp remove_special_characters(field), do: String.replace(field, ~r/[^\w]+/, "-") 

Note that button should be hidden if page.status == :published

Contributor guide