Quillon.Mark (Quillon v0.1.0)

Copy Markdown View Source

Utilities for working with Quillon marks.

Marks apply formatting to text nodes. They come in two forms:

  • Simple marks: atoms like :bold, :italic
  • Attributed marks: tuples like {:link, %{href: "..."}}

Summary

Functions

Add a mark to a marks list if not already present.

Check if a mark is an attributed (tuple) mark.

Get a mark of a given type from a marks list. Returns nil if not found.

Check if a marks list contains a mark of a given type.

Check if a value is a valid mark (simple or attributed).

Get the attrs of a mark. Returns empty map for simple marks.

Get the type of a mark.

Compare two marks lists for equality, ignoring order.

Remove a mark of a given type from a marks list.

Check if a mark is a simple (atom) mark.

Toggle a mark in a marks list (add if absent, remove if present).

Functions

add_mark(marks, mark)

@spec add_mark(list(), atom() | {atom(), map()}) :: list()

Add a mark to a marks list if not already present.

Examples

iex> Quillon.Mark.add_mark([:bold], :italic)
[:bold, :italic]

iex> Quillon.Mark.add_mark([:bold], :bold)
[:bold]

iex> Quillon.Mark.add_mark([], {:link, %{href: "/"}})
[{:link, %{href: "/"}}]

attributed?(arg1)

@spec attributed?(any()) :: boolean()

Check if a mark is an attributed (tuple) mark.

Examples

iex> Quillon.Mark.attributed?({:link, %{href: "/page"}})
true

iex> Quillon.Mark.attributed?(:bold)
false

iex> Quillon.Mark.attributed?({:unknown, %{}})
false

get_mark(marks, type)

@spec get_mark(list(), atom()) :: atom() | {atom(), map()} | nil

Get a mark of a given type from a marks list. Returns nil if not found.

Examples

iex> Quillon.Mark.get_mark([:bold, :italic], :bold)
:bold

iex> Quillon.Mark.get_mark([{:link, %{href: "/page"}}], :link)
{:link, %{href: "/page"}}

iex> Quillon.Mark.get_mark([:bold], :link)
nil

has_mark?(marks, type)

@spec has_mark?(list(), atom()) :: boolean()

Check if a marks list contains a mark of a given type.

Examples

iex> Quillon.Mark.has_mark?([:bold, :italic], :bold)
true

iex> Quillon.Mark.has_mark?([:bold], :italic)
false

iex> Quillon.Mark.has_mark?([{:link, %{href: "/"}}], :link)
true

iex> Quillon.Mark.has_mark?([], :bold)
false

mark?(mark)

@spec mark?(any()) :: boolean()

Check if a value is a valid mark (simple or attributed).

Examples

iex> Quillon.Mark.mark?(:bold)
true

iex> Quillon.Mark.mark?({:link, %{href: "/"}})
true

iex> Quillon.Mark.mark?("bold")
false

iex> Quillon.Mark.mark?({:unknown, %{}})
false

mark_attrs(mark)

@spec mark_attrs(atom() | {atom(), map()}) :: map()

Get the attrs of a mark. Returns empty map for simple marks.

Examples

iex> Quillon.Mark.mark_attrs(:bold)
%{}

iex> Quillon.Mark.mark_attrs({:link, %{href: "/page", title: "Link"}})
%{href: "/page", title: "Link"}

mark_type(mark)

@spec mark_type(atom() | {atom(), map()}) :: atom()

Get the type of a mark.

Examples

iex> Quillon.Mark.mark_type(:bold)
:bold

iex> Quillon.Mark.mark_type({:link, %{href: "/page"}})
:link

marks_equal?(marks1, marks2)

@spec marks_equal?(list(), list()) :: boolean()

Compare two marks lists for equality, ignoring order.

Examples

iex> Quillon.Mark.marks_equal?([:bold, :italic], [:italic, :bold])
true

iex> Quillon.Mark.marks_equal?([:bold], [:bold, :italic])
false

iex> Quillon.Mark.marks_equal?([], [])
true

remove_mark(marks, type)

@spec remove_mark(list(), atom()) :: list()

Remove a mark of a given type from a marks list.

Examples

iex> Quillon.Mark.remove_mark([:bold, :italic], :bold)
[:italic]

iex> Quillon.Mark.remove_mark([{:link, %{href: "/"}}], :link)
[]

iex> Quillon.Mark.remove_mark([:bold], :italic)
[:bold]

simple?(mark)

@spec simple?(any()) :: boolean()

Check if a mark is a simple (atom) mark.

Examples

iex> Quillon.Mark.simple?(:bold)
true

iex> Quillon.Mark.simple?(:italic)
true

iex> Quillon.Mark.simple?({:link, %{href: "/"}})
false

iex> Quillon.Mark.simple?(:unknown)
false

toggle_mark(marks, mark)

@spec toggle_mark(list(), atom() | {atom(), map()}) :: list()

Toggle a mark in a marks list (add if absent, remove if present).

Examples

iex> Quillon.Mark.toggle_mark([:bold], :italic)
[:bold, :italic]

iex> Quillon.Mark.toggle_mark([:bold, :italic], :bold)
[:italic]