PhoenixKit.Pages.Paths (phoenix_kit v1.7.38)

Copy Markdown View Source

Path utilities for Pages module.

Handles path resolution, validation, and conversion operations.

Summary

Functions

Builds the full absolute path for a relative pages path.

Converts a relative file path to a slug (without .md extension).

Joins path segments safely for pages paths.

Normalizes a slug to ensure proper formatting.

Gets the root directory path for pages.

Validates that a path is safe and doesn't attempt directory traversal.

Converts a slug to a relative file path with .md extension.

Functions

build_full_path(relative_path)

Builds the full absolute path for a relative pages path.

Includes security checks to prevent directory traversal.

Examples

iex> PhoenixKit.Pages.Paths.build_full_path("/hello.md")
"/app/priv/static/pages/hello.md"

file_path_to_slug(file_path)

Converts a relative file path to a slug (without .md extension).

Examples

iex> PhoenixKit.Pages.Paths.file_path_to_slug("/hello.md")
"/hello"

iex> PhoenixKit.Pages.Paths.file_path_to_slug("/hello")
"/hello"

join_path(segments)

Joins path segments safely for pages paths.

Normalizes the result to ensure consistent formatting.

Examples

iex> PhoenixKit.Pages.Paths.join_path("/blog", "hello.md")
"/blog/hello.md"

iex> PhoenixKit.Pages.Paths.join_path("/blog/", "/hello.md")
"/blog/hello.md"

join_path(segment1, segment2)

join_path(segment1, segment2, segment3)

normalize_slug(slug)

Normalizes a slug to ensure proper formatting.

  • Ensures slug starts with "/"
  • Removes trailing "/"
  • Returns default slug for empty or invalid inputs

Examples

iex> PhoenixKit.Pages.Paths.normalize_slug("")
"/404"

iex> PhoenixKit.Pages.Paths.normalize_slug("hello")
"/hello"

iex> PhoenixKit.Pages.Paths.normalize_slug("/hello/")
"/hello"

root_path()

Gets the root directory path for pages.

Creates the directory if it doesn't exist. Uses the parent application's directory, not PhoenixKit's dependency directory.

Examples

iex> PhoenixKit.Pages.Paths.root_path()
"/path/to/app/priv/static/pages"

safe_path?(relative_path)

Validates that a path is safe and doesn't attempt directory traversal.

Examples

iex> PhoenixKit.Pages.Paths.safe_path?("/hello")
true

iex> PhoenixKit.Pages.Paths.safe_path?("/../etc/passwd")
false

slug_to_file_path(slug)

Converts a slug to a relative file path with .md extension.

Examples

iex> PhoenixKit.Pages.Paths.slug_to_file_path("/hello")
"/hello.md"

iex> PhoenixKit.Pages.Paths.slug_to_file_path("/hello.md")
"/hello.md"