Sayfa.Blocks.Helpers (Sayfa v0.5.0)

Copy Markdown View Source

Shared helper functions for blocks.

Extracts common patterns that would otherwise be duplicated across multiple block modules (language filtering, URL prefix generation, etc.).

Summary

Functions

Filters contents to only those matching the given language.

Returns the language prefix path segment for a given language.

Functions

filter_by_lang(contents, lang)

@spec filter_by_lang([Sayfa.Content.t()], atom() | nil) :: [Sayfa.Content.t()]

Filters contents to only those matching the given language.

Returns the full list when lang is nil.

Examples

iex> contents = [%Sayfa.Content{title: "A", body: "", lang: :en}, %Sayfa.Content{title: "B", body: "", lang: :tr}]
iex> Sayfa.Blocks.Helpers.filter_by_lang(contents, :en) |> length()
1

iex> Sayfa.Blocks.Helpers.filter_by_lang(contents, nil) |> length()
2

lang_prefix_path(lang, site)

@spec lang_prefix_path(atom() | nil, map()) :: String.t()

Returns the language prefix path segment for a given language.

Returns "" when the language matches the site default.

Examples

iex> Sayfa.Blocks.Helpers.lang_prefix_path(nil, %{})
""