Download crates/forge_markdown_stream/src/inline.rs from SaylorTwift/forgecode: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/SaylorTwift/forgecode/resolve/main/crates/forge_markdown_stream/src/inline.rs
- Command line
-
hf download hf://SaylorTwift/forgecode/crates/forge_markdown_stream/src/inline.rs
-
curl -L -o inline.rs https://huggingface.co/SaylorTwift/forgecode/resolve/main/crates/forge_markdown_stream/src/inline.rs
4.53 kB
| //! Inline content rendering with theme-based formatting. | |
| use streamdown_parser::{InlineElement, InlineParser}; | |
| use crate::style::InlineStyler; | |
| /// Render inline elements to a string using a styler. | |
| pub fn render_inline_content<S: InlineStyler>(content: &str, styler: &S) -> String { | |
| render_inline_elements(&InlineParser::new().parse(content), styler) | |
| } | |
| /// Render inline elements to a string using a styler. | |
| pub fn render_inline_elements<S: InlineStyler>(elements: &[InlineElement], styler: &S) -> String { | |
| let mut result = String::new(); | |
| for element in elements { | |
| match element { | |
| InlineElement::Text(text) => { | |
| result.push_str(&styler.text(text)); | |
| } | |
| InlineElement::Bold(text) => { | |
| result.push_str(&styler.bold(text)); | |
| } | |
| InlineElement::Italic(text) => { | |
| result.push_str(&styler.italic(text)); | |
| } | |
| InlineElement::BoldItalic(text) => { | |
| result.push_str(&styler.bold_italic(text)); | |
| } | |
| InlineElement::Strikeout(text) => { | |
| result.push_str(&styler.strikethrough(text)); | |
| } | |
| InlineElement::Underline(text) => { | |
| result.push_str(&styler.underline(text)); | |
| } | |
| InlineElement::Code(text) => { | |
| result.push_str(&styler.code(text)); | |
| } | |
| InlineElement::Link { text, url } => { | |
| result.push_str(&styler.link(text, url)); | |
| } | |
| InlineElement::Image { alt, url } => { | |
| result.push_str(&styler.image(alt, url)); | |
| } | |
| InlineElement::Footnote(text) => { | |
| result.push_str(&styler.footnote(text)); | |
| } | |
| } | |
| } | |
| result | |
| } | |
| mod tests { | |
| use super::*; | |
| use crate::theme::TagStyler; | |
| fn render(content: &str) -> String { | |
| render_inline_content(content, &TagStyler) | |
| } | |
| fn test_plain_text() { | |
| insta::assert_snapshot!(render("hello world"), @"hello world"); | |
| } | |
| fn test_html_entities() { | |
| insta::assert_snapshot!(render("& < > ""), @r#"& < > ""#); | |
| } | |
| fn test_bold() { | |
| insta::assert_snapshot!(render("**bold**"), @"<b>bold</b>"); | |
| } | |
| fn test_italic() { | |
| insta::assert_snapshot!(render("*italic*"), @"<i>italic</i>"); | |
| } | |
| fn test_bold_italic() { | |
| insta::assert_snapshot!(render("***text***"), @"<b><i>text</i></b>"); | |
| } | |
| fn test_strikethrough() { | |
| insta::assert_snapshot!(render("~~struck~~"), @"<s>struck</s>"); | |
| } | |
| fn test_code() { | |
| insta::assert_snapshot!(render("`code`"), @"<code>code</code>"); | |
| } | |
| fn test_underline() { | |
| insta::assert_snapshot!(render("__underline__"), @"<u>underline</u>"); | |
| } | |
| fn test_link() { | |
| insta::assert_snapshot!(render("[click](https://example.com)"), @r#"<a href="https://example.com">click</a>"#); | |
| } | |
| fn test_image() { | |
| insta::assert_snapshot!(render(""), @r#"<img alt="alt" src="image.png"/>"#); | |
| } | |
| fn test_mixed() { | |
| insta::assert_snapshot!(render("hello **bold** and *italic*"), @"hello <b>bold</b> and <i>italic</i>"); | |
| } | |
| fn test_multiple_bold() { | |
| insta::assert_snapshot!(render("**one** and **two**"), @"<b>one</b> and <b>two</b>"); | |
| } | |
| fn test_entities_in_bold() { | |
| insta::assert_snapshot!(render("**&**"), @"<b>&</b>"); | |
| } | |
| fn test_entities_in_link() { | |
| insta::assert_snapshot!(render("[<click>](https://example.com)"), @r#"<a href="https://example.com"><click></a>"#); | |
| } | |
| fn test_code_content() { | |
| insta::assert_snapshot!(render("`let x = 1;`"), @"<code>let x = 1;</code>"); | |
| } | |
| fn test_link_special_url() { | |
| insta::assert_snapshot!(render("[link](https://example.com/path?q=1&b=2)"), @r#"<a href="https://example.com/path?q=1&b=2">link</a>"#); | |
| } | |
| fn test_empty() { | |
| insta::assert_snapshot!(render(""), @""); | |
| } | |
| fn test_whitespace() { | |
| insta::assert_snapshot!(render("hello world"), @"hello world"); | |
| } | |
| fn test_image_empty_alt() { | |
| insta::assert_snapshot!(render(""), @r#"<img alt="" src="image.png"/>"#); | |
| } | |
| } | |