PhiaUi.Components.Collab.MentionHelpers (phia_ui v0.1.17)

Copy Markdown View Source

Helper functions for @mention parsing and rendering.

Mentions follow the format @[Display Name](user_id) in raw text. This module provides utilities to parse, render, and extract user IDs from mention markup.

Summary

Functions

Extract unique user IDs mentioned in text.

Parse mentions from text.

Replace mention markup with display names prefixed by @.

Functions

mentioned_user_ids(text)

@spec mentioned_user_ids(String.t() | nil) :: [String.t()]

Extract unique user IDs mentioned in text.

Examples

iex> mentioned_user_ids("Hey @[Alice](u1) and @[Bob](u2) and @[Alice](u1)")
["u1", "u2"]

iex> mentioned_user_ids("No mentions")
[]

iex> mentioned_user_ids(nil)
[]

parse_mentions(text)

@spec parse_mentions(String.t() | nil) :: [map()]

Parse mentions from text.

Returns a list of maps with :start, :end, :user_id, and :name keys.

Mention format: @[Display Name](user_id)

Examples

iex> parse_mentions("Hello @[Alice](user-1) and @[Bob](user-2)")
[
  %{start: 6, end: 26, user_id: "user-1", name: "Alice"},
  %{start: 31, end: 49, user_id: "user-2", name: "Bob"}
]

iex> parse_mentions("No mentions here")
[]

iex> parse_mentions(nil)
[]

render_mentions(text, users_map \\ %{})

@spec render_mentions(String.t(), map()) :: String.t()

Replace mention markup with display names prefixed by @.

Transforms @[Display Name](user_id) into @Display Name.

Examples

iex> render_mentions("Hello @[Alice](user-1)!")
"Hello @Alice!"

iex> render_mentions("No mentions")
"No mentions"