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"
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>"
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"]]
Lists checked boxes from the text.
Examples
> list_checked_boxes("* [x] done")
[["done"]]
Lists unchecked boxes from the text.
Examples
> list_unchecked_boxes("* [ ] task")
[["task"]]
Makes local links within content live.
Examples
> make_local_links_live("<a href="/path">Link</a>")
"<a href="/path" data-phx-link="redirect" data-phx-link-state="push">Link</a>"
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>"
Converts text to emotes if the Emote module is enabled.
Examples
iex> maybe_emote(":smile:", nil, [])
"😄"
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>"
Normalizes HTML content, handling various edge cases.
Examples
iex> maybe_normalize_html("<p>Test</p>")
"Test"
iex> maybe_normalize_html("<p><br/></p>")
""
Renders templated content if the Solid
library is enabled.
Examples
> maybe_render_templated("Hello {{name}}", %{name: "World"})
"Hello World"
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"
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>"
Generates a random string of a given length.
Examples
iex> random_string(5) |> String.length()
5
> random_string()
#=> a string of length 10
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"
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..."
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"
Converts an English conjugated verb to its infinitive form using the Verbs
library. Currently only supports irregular verbs.
Examples
> verb_infinitive("running")
"run"