Markup.Djot.Attributes (Markup.Djot v0.1.0)

Copy Markdown View Source

Helpers for node attribute lists.

Attributes are an ordered list of {key, value} string pairs. Order and duplicates are preserved; multiple "class" entries are joined when rendered.

Summary

Functions

Appends a class.

Removes every entry for key.

Returns the value for key.

Replaces every entry for key with a single {key, value} at the end.

Types

t()

@type t() :: [{String.t(), String.t()}]

Functions

add_class(attrs, class)

@spec add_class(t(), String.t()) :: t()

Appends a class.

delete(attrs, key)

@spec delete(t(), String.t()) :: t()

Removes every entry for key.

get(attrs, key)

@spec get(t(), String.t()) :: String.t() | nil

Returns the value for key.

For "class", all class values joined with a space. For other keys, the last value. Returns nil when the key is absent.

iex> Markup.Djot.Attributes.get([{"class", "a"}, {"id", "x"}, {"class", "b"}], "class")
"a b"
iex> Markup.Djot.Attributes.get([{"id", "x"}, {"id", "y"}], "id")
"y"

put(attrs, key, value)

@spec put(t(), String.t(), String.t()) :: t()

Replaces every entry for key with a single {key, value} at the end.