BeaconCMS/beacon

`generate_page_path` lifecycle

开放

#236 创建于 2023年5月4日

 (0 条评论) (0 个反应) (0 位负责人)Elixir (120 个派生)batch import
area:contentenhancementgood first issue

仓库指标

星标
 (1,194 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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

贡献者指南