ProtoRune.RichText (proto_rune v0.5.1)

Copy Markdown

Builder for AT Protocol rich text with facets.

Handles byte offset calculation and facet generation for mentions, links, and hashtags.

Facets use snake_case atom keys internally (byte_start, byte_end, :"$type"). The XRPC client camelizes them to the wire format (byteStart, byteEnd, "$type") when posting.

Examples

# Build rich text with mentions and links
{:ok, rt} =
  RichText.new()
  |> RichText.text("Hello ")
  |> RichText.mention("alice.bsky.social")
  |> RichText.text("! Check out ")
  |> RichText.link("this project", "https://example.com")
  |> RichText.text(" ")
  |> RichText.hashtag("elixir")
  |> RichText.build()

# Use in post
{:ok, post} = ProtoRune.Bsky.post(session, rt)

# Get plain text
plain = RichText.to_plain_text(rt)
# => "Hello @alice.bsky.social! Check out this project #elixir"

Summary

Functions

Builds the rich text, returning a map suitable for use in posts.

Gets the facets from rich text.

Appends a hashtag to the builder.

Appends a link to the builder.

Appends a mention to the builder.

Creates a new rich text builder.

Appends plain text to the builder.

Converts rich text back to plain text.

Types

t()

@type t() :: %ProtoRune.RichText{facets: [map()], text: String.t()}

Functions

build(rt)

@spec build(t()) :: {:ok, map()}

Builds the rich text, returning a map suitable for use in posts.

Examples

{:ok, post_data} =
  RichText.new()
  |> RichText.text("Hello ")
  |> RichText.mention("alice.bsky.social")
  |> RichText.build()

# Use with Bsky.post
{:ok, post} = ProtoRune.Bsky.post(session, post_data)

facets(arg1)

@spec facets(t() | map()) :: [map()]

Gets the facets from rich text.

Examples

facets = RichText.facets(rt)

hashtag(rt, tag)

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

Appends a hashtag to the builder.

The hashtag will be formatted as #tag in the text, and a facet will be created.

Examples

rt = RichText.new() |> RichText.hashtag("elixir")
# => "#elixir" in text with tag facet

link(rt, link_text, url)

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

Appends a link to the builder.

The link text will appear in the final text, and a facet will be created pointing to the URL.

Examples

rt = RichText.new() |> RichText.link("click here", "https://example.com")

mention(rt, handle)

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

Appends a mention to the builder.

The mention will be formatted as @handle in the text. If DID resolution succeeds, a mention facet will be created. If resolution fails, the text is added without a facet (plain text mention).

Examples

rt = RichText.new() |> RichText.mention("alice.bsky.social")
# => "@alice.bsky.social" in text with mention facet (if DID resolves)

new()

@spec new() :: t()

Creates a new rich text builder.

Examples

rt = RichText.new()

text(rt, content)

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

Appends plain text to the builder.

Examples

rt = RichText.new() |> RichText.text("Hello world")

to_plain_text(arg1)

@spec to_plain_text(t() | map()) :: String.t()

Converts rich text back to plain text.

Examples

plain = RichText.to_plain_text(rt)