aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/encode.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-06-14 13:47:32 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-06-14 13:55:03 +0200
commita1ce43e428754084474a7ecf88ae6debf88b9164 (patch)
treea1df0ec515f624431d3e398f7d24e7b411c18e6e /src/util/encode.rs
parentc587aee9512119e61918bfbe81c3cca3de7e70aa (diff)
downloadmarkdown-rs-a1ce43e428754084474a7ecf88ae6debf88b9164.tar.gz
markdown-rs-a1ce43e428754084474a7ecf88ae6debf88b9164.tar.bz2
markdown-rs-a1ce43e428754084474a7ecf88ae6debf88b9164.zip
Reorganize to split util
Diffstat (limited to '')
-rw-r--r--src/util/encode.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util/encode.rs b/src/util/encode.rs
new file mode 100644
index 0000000..f79c8ea
--- /dev/null
+++ b/src/util/encode.rs
@@ -0,0 +1,29 @@
+//! Utilities to encode HTML.
+
+/// Encode dangerous html characters.
+///
+/// This ensures that certain characters which have special meaning in HTML are
+/// dealt with.
+/// Technically, we can skip `>` and `"` in many cases, but CM includes them.
+///
+/// This behavior is not explained in prose in `CommonMark` but can be inferred
+/// from the input/output test cases.
+///
+/// ## Examples
+///
+/// ```rust ignore
+/// use micromark::util::encode;
+///
+/// assert_eq!(encode("I <3 🦀"), "I &lt;3 🦀");
+/// ```
+///
+/// ## References
+///
+/// * [`micromark-util-encode` in `micromark`](https://github.com/micromark/micromark/tree/main/packages/micromark-util-encode)
+pub fn encode(value: &str) -> String {
+ value
+ .replace('&', "&amp;")
+ .replace('"', "&quot;")
+ .replace('<', "&lt;")
+ .replace('>', "&gt;")
+}