Search documentation of phoenix_kit

# PhoenixKit.Pages.Metadata (phoenix_kit v1.6.19)

View Source

Metadata management for Pages module.

Handles parsing and serialization of metadata stored in HTML comment blocks with YAML content.

## Format

    <!-- METADATA
    status: published
    title: My Page Title
    description: A brief description
    slug: custom-url
    author: John Doe
    created_at: 2025-01-15T10:00:00Z
    updated_at: 2025-01-15T10:00:00Z
    -->

## Fields

- `status` - `draft` | `published` | `archived` (default: `draft`)

- `title` - Page title (optional, defaults to filename)
- `description` - SEO description (optional)
- `slug` - Custom URL slug (optional)
- `author` - Content author (optional)
- `created_at` - ISO 8601 datetime (auto-generated)
- `updated_at` - ISO 8601 datetime (auto-updated)

# Summary

## Types

metadata()

## Functions

default_metadata()

Returns default metadata for new files.

merge_metadata(user_data, defaults)

Merges user-provided metadata with defaults.

parse(content)

Parses metadata from file content.

serialize(metadata)

Serializes metadata to HTML comment format.

strip_metadata(content)

Strips metadata block from content.

update_metadata(content, metadata)

Updates metadata in content.

# Types

# metadata()

    @type metadata() :: %{
      status: String.t(),
      title: String.t() | nil,
      description: String.t() | nil,
      slug: String.t() | nil,
      author: String.t() | nil,
      created_at: DateTime.t(),
      updated_at: DateTime.t()
    }

# Functions

# default_metadata()

    @spec default_metadata() :: metadata()

Returns default metadata for new files.

## Examples

    iex> metadata = Metadata.default_metadata()
    iex> metadata.status
    "draft"

# merge_metadata(user_data, defaults)

    @spec merge_metadata(map(), metadata()) :: metadata()

Merges user-provided metadata with defaults.

Preserves custom fields that aren't in the standard set.

## Examples

    iex> user_data = %{"status" => "published", "custom_field" => "value"}
    iex> metadata = Metadata.merge_metadata(user_data, default_metadata())
    iex> metadata.status
    "published"

# parse(content)

    @spec parse(String.t()) :: {:ok, metadata(), String.t()} | {:error, :no_metadata}

Parses metadata from file content.

Searches for metadata block in first 20 lines (optimization), then searches entire file if not found.

Returns metadata map and content without metadata block.

## Examples

    iex> content = """
    ...> <!-- METADATA
    ...> status: published
    ...> title: My Page
    ...> -->
    ...> # Content
    ...> """
    iex> {:ok, metadata, content} = Metadata.parse(content)
    iex> metadata.status
    "published"

# serialize(metadata)

    @spec serialize(metadata()) :: String.t()

Serializes metadata to HTML comment format.

## Examples

    iex> metadata = %{
    ...>   status: "published",
    ...>   title: "My Page",
    ...>   created_at: ~U[2025-01-15 10:00:00Z],
    ...>   updated_at: ~U[2025-01-15 10:00:00Z]
    ...> }
    iex> Metadata.serialize(metadata)
    "<!-- METADATA\nstatus: published\n...\n-->"

# strip_metadata(content)

    @spec strip_metadata(String.t()) :: String.t()

Strips metadata block from content.

Returns content without the metadata block.

## Examples

    iex> content = "<!-- METADATA\nstatus: draft\n-->\n\n# Content"
    iex> Metadata.strip_metadata(content)
    "# Content"

# update_metadata(content, metadata)

    @spec update_metadata(String.t(), metadata()) :: String.t()

Updates metadata in content.

If metadata exists, replaces it. If not, prepends it.

## Examples

    iex> content = "# Content"
    iex> metadata = default_metadata()
    iex> updated = Metadata.update_metadata(content, metadata)
    iex> updated =~ "<!-- METADATA"
    true