View Source Bonfire.Common.Text (Bonfire v0.9.11-social-beta.6)

Helpers for handling plain or rich text (markdown, HTML, etc)

Summary

Functions

Checks if a string is blank.

Converts a string to CamelCase.

Highlights code using Makeup or falls back to Phoenix.HTML if unsupported.

Checks if a string contains a substring.

Checks if a string contains HTML tags.

Hashes the input using a specified algorithm.

Lists all checkboxes from the text.

Lists checked boxes from the text.

Lists unchecked boxes from the text.

Makes local links within content live.

Converts markdown checkboxes to HTML checkboxes.

Converts text to emotes if the Emote module is enabled.

Converts the input content from markdown to HTML if the markdown library is enabled. If the content starts with an HTML tag or if the markdown library is not enabled, it skips conversion.

Normalizes HTML content, handling various edge cases.

Renders templated content if the Solid library is enabled.

Sanitizes HTML content to ensure it is safe.

Converts input to snake_case.

Normalizes links in the content based on format.

Generates a random string of a given length.

Truncates a string to a maximum length, ensuring it ends on a sentence boundary.

Generates a URL-friendly slug from the given text. The text is downcased, trimmed, spaces are replaced with dashes, and it is URI-encoded.

Splits a string into lines.

Returns the length of the input based on its type.

Extracts plain text from HTML content.

Truncates a string to a maximum length, optionally adding a suffix.

Truncates the input string at the last underscore (_) if its length exceeds the given length. If the input string is shorter than or equal to the given length, it returns the string as is.

Generates a unique random integer.

Generates a unique random string.

Converts the first character of a binary to uppercase.

Converts an English conjugated verb to its infinitive form using the Verbs library. Currently only supports irregular verbs.

Functions

blank?(str_or_nil)

Checks if a string is blank.

Examples

iex> blank?(nil)
true

iex> blank?("   ")
true

iex> blank?("not blank")
false

camelise(str)

Converts a string to CamelCase.

Examples

iex> camelise("hello world")
"HelloWorld"

code_syntax(text, filename)

Highlights code using Makeup or falls back to Phoenix.HTML if unsupported.

Examples

> code_syntax("defmodule Test {}", "test.ex")
#=> "<pre><code class="highlight">defmodule Test {}</code></pre>"

contains?(string, substring)

Checks if a string contains a substring.

Examples

iex> contains?("hello world", "world")
true

iex> contains?("hello world", "foo")
false

contains_html?(string)

Checks if a string contains HTML tags.

Examples

iex> contains_html?("<div>Test</div>")
true

iex> contains_html?("Just text")
false

hash(seed, opts \\ [])

Hashes the input using a specified algorithm.

Examples

iex> hash("data", algorithm: :sha256)
"Om6weQ85rIfJTzhWst0sXREOaBFgImGpqSPTuyOtyLc"

iex> hash("data")
"jXd_OF09_siBXSD3SWAm3A"

list_checkboxes(text)

Lists all checkboxes from the text.

Examples

> list_checkboxes("* [ ] task
  • [x] done") [[" ", "task"], [" ", "done"]]

list_checked_boxes(text)

Lists checked boxes from the text.

Examples

> list_checked_boxes("* [x] done")
[["done"]]

list_unchecked_boxes(text)

Lists unchecked boxes from the text.

Examples

> list_unchecked_boxes("* [ ] task")
[["task"]]

markdown_checkboxes(text)

Converts markdown checkboxes to HTML checkboxes.

Examples

> markdown_checkboxes("* [ ] task
  • [x] done") "<ul><li><input type='checkbox'> task</li><li><input type='checkbox' checked='checked'> done</li></ul>"

maybe_emote(content, user \\ nil, custom_emoji \\ [])

Converts text to emotes if the Emote module is enabled.

Examples

iex> maybe_emote(":smile:", nil, [])
"😄"

maybe_markdown_to_html(nothing, opts \\ [])

Converts the input content from markdown to HTML if the markdown library is enabled. If the content starts with an HTML tag or if the markdown library is not enabled, it skips conversion.

> Bonfire.Common.Text.maybe_markdown_to_html("*Hello World*", [])
"<p><em>Hello World</em></p>"

iex> Bonfire.Common.Text.maybe_markdown_to_html("<p>Hello</p>", [])
"<p>Hello</p>"

> Bonfire.Common.Text.maybe_markdown_to_html("Not markdown", [])
"<p>Not markdown</p>"

maybe_normalize_html(html_string)

Normalizes HTML content, handling various edge cases.

Examples

iex> maybe_normalize_html("<p>Test</p>")
"Test"

iex> maybe_normalize_html("<p><br/></p>")
""

maybe_other_custom_emoji(text, user)

maybe_render_templated(templated_content, data)

Renders templated content if the Solid library is enabled.

Examples

> maybe_render_templated("Hello {{name}}", %{name: "World"})
"Hello World"

maybe_sane_html(content)

Sanitizes HTML content to ensure it is safe.

It is recommended to call this before storing any that data is coming in from the user or from a remote instance

Examples

> maybe_sane_html("<script>alert('XSS')</script>")
#=> "alert('XSS')" (if HtmlSanitizeEx is enabled)

maybe_to_snake(string)

Converts input to snake_case.

Examples

iex> maybe_to_snake("CamelCase")
"camel_case"

normalise_links(content, format \\ :markdown)

Normalizes links in the content based on format.

Examples

> normalise_links("<a href="/pub/actors/foo">Actor</a>", :markdown)
"<a href="/character/foo">Actor</a>"

random_string(str_length \\ 10)

Generates a random string of a given length.

See also unique_string/1 and unique_integer/1

Examples

iex> random_string(5) |> String.length()
5

> random_string()
#=> a string of length 10

regex_list(regex, text)

sentence_truncate(input, length \\ 250, add_to_end \\ "")

Truncates a string to a maximum length, ensuring it ends on a sentence boundary.

Examples

iex> sentence_truncate("Hello world. This is a test.", 12)
"Hello world."

iex> sentence_truncate("Hello world. This is a test.", 12, "...")
"Hello world...."

slug(text)

Generates a URL-friendly slug from the given text. The text is downcased, trimmed, spaces are replaced with dashes, and it is URI-encoded.

iex> Bonfire.Common.Text.slug("Hello World!")
"hello-world!"

iex> Bonfire.Common.Text.slug("Elixir Programming")
"elixir-programming"

iex> Bonfire.Common.Text.slug("Special & Characters")
"special-&-characters"

split_lines(string)

Splits a string into lines.

Examples

iex> split_lines("line1\nline2\r\nline3\rline4")
["line1", "line2", "line3", "line4"]

strlen(x)

Returns the length of the input based on its type.

Examples

iex> strlen("hello")
5

iex> strlen([1, 2, 3])
3

iex> strlen(%{})
0

iex> strlen(nil)
0

iex> strlen(0)
0

iex> strlen(123)
1

text_only(content)

Extracts plain text from HTML content.

Examples

iex> text_only("<div>Text</div>")
"Text"

iex> text_only({:safe, "<div>Safe Text</div>"})
"Safe Text"

truncate(text, max_length \\ 250, add_to_end \\ nil)

Truncates a string to a maximum length, optionally adding a suffix.

Examples

iex> truncate("Hello world", 5)
"Hello"

iex> truncate("Hello world", 5, "...")
"He..."

iex> truncate("Hello world", 7, "...")
"Hell..."

underscore_truncate(input, length \\ 250)

Truncates the input string at the last underscore (_) if its length exceeds the given length. If the input string is shorter than or equal to the given length, it returns the string as is.

iex> Bonfire.Common.Text.underscore_truncate("abc_def_ghi", 4)
"abc"

iex> Bonfire.Common.Text.underscore_truncate("abc_def_ghi", 10)
"abc_def"

iex> Bonfire.Common.Text.underscore_truncate("abc_def_ghi", 5)
"abc"

iex> Bonfire.Common.Text.underscore_truncate("abc_def_ghi", 0)

unique_integer()

Generates a unique random integer.

"Unique" means that this function will not return the same integer more than once on the current BEAM runtime, meaning until the application is next restarted.

Examples

iex> unique_integer()

unique_string()

Generates a unique random string.

"Unique" means that this function will not return the same string more than once on the current BEAM runtime, meaning until the application is next restarted.

Examples

iex> unique_string()

upcase_first(arg)

Converts the first character of a binary to uppercase.

Examples

iex> upcase_first("hello")
"Hello"

verb_infinitive(verb_conjugated)

Converts an English conjugated verb to its infinitive form using the Verbs library. Currently only supports irregular verbs.

Examples

> verb_infinitive("running")
"run"