View Source Bonfire.Common.Text (Bonfire v0.9.10-classic-beta.169)

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.

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

Checks if a string is blank.

Examples

iex> blank?(nil)
true

iex> blank?("   ")
true

iex> blank?("not blank")
false

Converts a string to CamelCase.

Examples

iex> camelise("hello world")
"HelloWorld"
Link to this function

code_syntax(text, filename)

View Source

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>"
Link to this function

contains?(string, substring)

View Source

Checks if a string contains a substring.

Examples

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

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

Checks if a string contains HTML tags.

Examples

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

iex> contains_html?("Just text")
false

Hashes the input using a specified algorithm.

Examples

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

iex> hash("data")
"jXd_OF09_siBXSD3SWAm3A"

Lists all checkboxes from the text.

Examples

> list_checkboxes("* [ ] task
  • [x] done") [[" ", "task"], [" ", "done"]]
Link to this function

list_checked_boxes(text)

View Source

Lists checked boxes from the text.

Examples

> list_checked_boxes("* [x] done")
[["done"]]
Link to this function

list_unchecked_boxes(text)

View Source

Lists unchecked boxes from the text.

Examples

> list_unchecked_boxes("* [ ] task")
[["task"]]
Link to this function

markdown_checkboxes(text)

View Source

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>"
Link to this function

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

View Source

Converts text to emotes if the Emote module is enabled.

Examples

iex> maybe_emote(":smile:", nil, [])
"😄"
Link to this function

maybe_markdown_to_html(nothing, opts \\ [])

View Source

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>"
Link to this function

maybe_normalize_html(html_string)

View Source

Normalizes HTML content, handling various edge cases.

Examples

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

iex> maybe_normalize_html("<p><br/></p>")
""
Link to this function

maybe_other_custom_emoji(text, user)

View Source
Link to this function

maybe_render_templated(templated_content, data)

View Source

Renders templated content if the Solid library is enabled.

Examples

> maybe_render_templated("Hello {{name}}", %{name: "World"})
"Hello World"
Link to this function

maybe_sane_html(content)

View Source

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)

Converts input to snake_case.

Examples

iex> maybe_to_snake("CamelCase")
"camel_case"
Link to this function

normalise_links(content, format \\ :markdown)

View Source

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>"
Link to this function

random_string(length \\ 10)

View Source

Generates a random string of a given length.

Examples

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

> random_string()
#=> a string of length 10
Link to this function

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

View Source

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...."

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"

Splits a string into lines.

Examples

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

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

Extracts plain text from HTML content.

Examples

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

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

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

View Source

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..."
Link to this function

underscore_truncate(input, length \\ 250)

View Source

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)

Converts the first character of a binary to uppercase.

Examples

iex> upcase_first("hello")
"Hello"
Link to this function

verb_infinitive(verb_conjugated)

View Source

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

Examples

> verb_infinitive("running")
"run"