Markdown parser fork with extended syntax for personal use.
at hack 55 lines 1.5 kB view raw
1//! Encode HTML. 2 3use alloc::string::String; 4 5/// Encode dangerous html characters. 6/// 7/// This ensures that certain characters which have special meaning in HTML are 8/// dealt with. 9/// Technically, we can skip `>` and `"` in many cases, but `CommonMark` 10/// includes them. 11/// 12/// This behavior is not explained in prose in `CommonMark` but can be inferred 13/// from the input/output test cases. 14/// 15/// ## Examples 16/// 17/// ```rust ignore 18/// use markdown::util::encode; 19/// 20/// assert_eq!(encode("I <3 🦀"), "I &lt;3 🦀"); 21/// ``` 22/// 23/// ## References 24/// 25/// * [`micromark-util-encode` in `micromark`](https://github.com/micromark/micromark/tree/main/packages/micromark-util-encode) 26pub fn encode(value: &str, encode_html: bool) -> String { 27 // It’ll grow a bit bigger for each dangerous character. 28 let mut result = String::with_capacity(value.len()); 29 let bytes = value.as_bytes(); 30 let mut index = 0; 31 let mut start = 0; 32 33 while index < bytes.len() { 34 let byte = bytes[index]; 35 if matches!(byte, b'\0') || (encode_html && matches!(byte, b'&' | b'"' | b'<' | b'>')) { 36 result.push_str(&value[start..index]); 37 result.push_str(match byte { 38 b'\0' => "", 39 b'&' => "&amp;", 40 b'"' => "&quot;", 41 b'<' => "&lt;", 42 // `b'>'` 43 _ => "&gt;", 44 }); 45 46 start = index + 1; 47 } 48 49 index += 1; 50 } 51 52 result.push_str(&value[start..]); 53 54 result 55}