Skip to main content

htmd/element_handler/
emphasis.rs

1use crate::{
2    Element,
3    element_handler::{HandlerResult, Handlers},
4    serialize_if_faithful,
5    text_util::{StripWhitespace, concat_strings},
6};
7
8pub(super) fn emphasis_handler(
9    handlers: &dyn Handlers,
10    element: Element,
11    marker: &str,
12) -> Option<HandlerResult> {
13    serialize_if_faithful!(handlers, element, 0);
14    let content = handlers.walk_children(element.node).content;
15    if content.is_empty() {
16        return None;
17    }
18    // Note: this is whitespace, NOT document whitespace, per the
19    // [Commonmark spec](https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis).
20    let (content, leading_whitespace) = content.strip_leading_whitespace();
21    let (content, trailing_whitespace) = content.strip_trailing_whitespace();
22    if content.is_empty() {
23        return None;
24    }
25    let content = concat_strings!(
26        leading_whitespace.unwrap_or(""),
27        marker,
28        content,
29        marker,
30        trailing_whitespace.unwrap_or("")
31    );
32    Some(content.into())
33}