Bonfire.Common.Text (Bonfire v1.0.0-social-rc.3.13)
View SourceHelpers for handling plain or rich text (markdown, HTML, etc)
Summary
Functions
Normalizes HTML content and returns a string. Accepts string, LazyHTML struct, or HTML syntax tree as input.
Converts input to a LazyHTML tree.
Converts input to a LazyHTML struct.
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.
Extracts URLs from HTML content using LazyHTML.
Hashes the input using a specified algorithm (md5 by default, which is fast but not secure).
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.
Sanitizes HTML content to ensure it is safe.
Converts input to snake_case.
Generates a random string of a given length.
Replaces the href attribute of <a> tags in HTML input using a replacements map.
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
Normalizes HTML content and returns a string. Accepts string, LazyHTML struct, or HTML syntax tree as input.
Trims all leading and trailing <p><br/></p>
, <br/>
, and empty <p></p>
tags.
Examples
iex> as_html("<p>Test</p>")
"<p>Test</p>"
iex> as_html("<p><br/></p><p></p><br/>")
""
iex> as_html("<p><br/></p><p></p><p><br/></p><br/>")
""
iex> as_html("<p><br/></p><br/>")
""
iex> as_html("<p></p>")
""
iex> as_html("<br/>")
""
iex> as_html("<p><br/></p>Hello<br/>")
"Hello"
iex> as_html("<p><br/></p><p>Test</p><br/>")
"<p>Test</p>"
iex> as_html("<p><br/></p><p>Test</p><p></p><br/>")
"<p>Test</p>"
iex> as_html("<p><br/></p><p>Test</p><p>More</p><br/>")
"<p>Test</p><p>More</p>"
iex> as_html("<p><br/></p><p>Test</p><p>More</p><p></p><br/>")
"<p>Test</p><p>More</p>"
iex> as_html("<p><br/></p><p></p><p>Test</p><p>More</p><p></p><br/>")
"<p>Test</p><p>More</p>"
iex> as_html("<p><br/></p><p>Test</p><p>More</p><p>Even</p><br/>")
"<p>Test</p><p>More</p><p>Even</p>"
iex> as_html("<p><br/></p><p>Test</p><p>More</p><p>Even</p><p></p><br/>")
"<p>Test</p><p>More</p><p>Even</p>"
iex> as_html("<p><br/></p><p>Test</p><p>More</p><p>Even</p><p><br/></p><br/>")
"<p>Test</p><p>More</p><p>Even</p>"
iex> as_html("<p>Test</p><p>More</p>")
"<p>Test</p><p>More</p>"
iex> as_html("<p>Test</p><p></p>")
"<p>Test</p>"
iex> as_html("Test<br/>")
"Test"
iex> as_html("<p>Test</p>More<br/>")
"<p>Test</p>More"
iex> as_html("<p>Test</p><p>More</p><p></p>")
"<p>Test</p><p>More</p>"
iex> as_html("<p>Test</p><p>More</p><p><br/></p>")
"<p>Test</p><p>More</p>"
iex> as_html("<p>Test</p><p>More</p><p>Even</p>")
"<p>Test</p><p>More</p><p>Even</p>"
iex> as_html("<p>Test</p><p>More</p><p>Even</p><p></p>")
"<p>Test</p><p>More</p><p>Even</p>"
iex> as_html("Test<p>More</p><p>Even</p><p><br/></p>")
"Test<p>More</p><p>Even</p>"
iex> as_html("")
""
Converts input to a LazyHTML tree.
Accepts string, LazyHTML struct, or tree.
Examples
iex> as_html_tree("<b>hi</b>")
[{"b", [], ["hi"]}]
iex> as_html_tree([{"b", [], ["hi"]}])
[{"b", [], ["hi"]}]
iex> as_html_tree(LazyHTML.from_fragment("<b>hi</b>"))
[{"b", [], ["hi"]}]
Accepts string, LazyHTML struct, or tree.
Examples
iex> as_html_tree("<b>hi</b>")
[{"b", [], ["hi"]}]
iex> as_html_tree([{"b", [], ["hi"]}])
[{"b", [], ["hi"]}]
iex> as_html_tree(LazyHTML.from_fragment("<b>hi</b>"))
[{"b", [], ["hi"]}]
Converts input to a LazyHTML struct.
Accepts string, tree, or LazyHTML struct.
Examples
iex> %LazyHTML{resource: _} = as_lazy_html("<b>hi</b>")
iex> %LazyHTML{resource: _} = as_lazy_html([{"b", [], ["hi"]}])
iex> %LazyHTML{resource: _} = as_lazy_html(%LazyHTML{resource: nil})
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
Extracts URLs from HTML content using LazyHTML.
Accepts string, LazyHTML struct, or tree as input. Returns {:ok, %{html: LazyHTML struct, urls: [url, ...]}} or nil for blank input.
Filters out mention and hashtag URLs.
Examples
iex> {:ok, %{html: %LazyHTML{resource: _}, urls: ["http://foo.com"]}} = Bonfire.Common.Text.extract_urls_from_html("<a href='http://foo.com'>foo</a>")
iex> {:ok, %{html: %LazyHTML{resource: _}, urls: []}} = Bonfire.Common.Text.extract_urls_from_html("<div>no valid links</div><a href='/'>test</a><a href='mailto:test@example.com'>test</a>")
iex> {:ok, %{html: %LazyHTML{resource: _}, urls: ["/"]}} = Bonfire.Common.Text.extract_urls_from_html("<div>including relative links</div><a href='/'>test</a>", allow_relative: true)
iex> {:ok, %{html: %LazyHTML{resource: _}, urls: ["mailto:test@example.com"]}} = Bonfire.Common.Text.extract_urls_from_html("<div>no non-HTTP links</div><a href='/'>test</a><a href='mailto:test@example.com'>test</a>", allow_non_http: true)
iex> {:ok, %{html: %LazyHTML{resource: _}, urls: ["ftp://test@example.com"]}} = Bonfire.Common.Text.extract_urls_from_html("<div>no non-HTTP links</div><a href='/'>test</a><a href='ftp://test@example.com'>test</a>", allow_non_http: true)
iex> Bonfire.Common.Text.extract_urls_from_html(nil)
nil
iex> Bonfire.Common.Text.extract_urls_from_html("")
nil
Hashes the input using a specified algorithm (md5 by default, which is fast but not secure).
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>"
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
Uses HtmlSanitizeEx if enabled, otherwise returns input.
Examples
iex> Bonfire.Common.Text.maybe_sane_html("<script>alert('XSS')</script>")
"alert('XSS')"
iex> Bonfire.Common.Text.maybe_sane_html("<p>Safe</p>")
"<p>Safe</p>"
iex> Bonfire.Common.Text.maybe_sane_html("<div class='test'>Safe</div>")
"Safe"
Converts input to snake_case.
Examples
iex> maybe_to_snake("CamelCase")
"camel_case"
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
Replaces the href attribute of <a> tags in HTML input using a replacements map.
Accepts a string, tree, or LazyHTML struct as input. Returns the updated tree.
Examples
iex> Bonfire.Common.Text.replace_links("<a href='/foo'>test</a>", %{"/foo" => "/bar"}) [{"a", [{"href", "/bar"}], ["test"]}]
iex> Bonfire.Common.Text.replace_links([{"a", [{"href", "/foo"}], ["test"]}], %{"/foo" => "/bar"}) [{"a", [{"href", "/bar"}], ["test"]}]
iex> Bonfire.Common.Text.replace_links("<a href='/foo'>test</a>", %{"/baz" => "/qux"}) [{"a", [{"href", "/foo"}], ["test"]}]
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"
iex> Bonfire.Common.Text.slug(["Many", nil, ["Words"]])
"many-words"
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)
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()
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()
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"