BeaconCMS/beacon

`generate_page_path` lifecycle

Aberta

#236 aberto em 4 de mai. de 2023

 (0 comentário) (0 reação) (0 responsável)Elixir (120 forks)batch import
area:contentenhancementgood first issue

Métricas do repositório

Stars
 (1.194 estrelas)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 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

Guia do colaborador