Create a custom Scrubber
masterYou can define a custom scrubber by creating a module and using the HtmlSanitizeEx macro. This allows you to specify exactly which tags, attributes, and URI schemes are permitted. Anything not explicitly allowed will be stripped.
Use the following macros within your module:
allow_tag_with_these_attributes(tag, attributes): Allows a specific tag with a list of allowed attributes.allow_tag_with_uri_attributes(tag, attributes, uri_schemes): Allows a tag with specific attributes (likehref) and restricts them to a list of allowed URI schemes (e.g.,["https", "mailto"]).
Once defined, use YourModule.sanitize(html) to perform the scrubbing.
defmodule MyProject.MyScrubber do
use HtmlSanitizeEx
allow_tag_with_these_attributes("p", [])
allow_tag_with_these_attributes("h1", [])
allow_tag_with_uri_attributes("a", ["href"], ["https", "mailto"])
end
# Usage
text = "<h1>Hello <script>World!</script></h1>"
MyProject.MyScrubber.sanitize(text)
# => "<h1>Hello World!</h1>"