Skip to content

Instantly share code, notes, and snippets.

@akhilman
Last active May 4, 2020 19:23
Show Gist options
  • Save akhilman/337cb082fff23f7ab63e23b6eb33b6ed to your computer and use it in GitHub Desktop.
Save akhilman/337cb082fff23f7ab63e23b6eb33b6ed to your computer and use it in GitHub Desktop.
Wrap code blocks in src/page/guide.rs
fn view_guide_html(content: &str) -> Node<Msg> {
div![
C![
// it has to be "markdown-body" so it's content is styled by Github CSS
C.markdown_body,
],
wrap_code_blocks(raw!(content))
]
}
/// Adds element keys to code blocks to force reinitialization if code changed.
fn wrap_code_blocks(nodes: Vec<Node<Msg>>) -> Vec<Node<Msg>> {
use std::hash::{Hash, Hasher};
nodes
.into_iter()
.map(|node| match node {
Node::Element(el) => {
if el.is_custom() && el.tag.as_str() == "code-block" {
let mut hasher =
std::collections::hash_map::DefaultHasher::new();
el.get_text().hash(&mut hasher);
custom![
el.tag,
el_key(&hasher.finish().to_string()),
el.children,
]
} else {
Node::Element(el)
}
},
_ => node,
})
.collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment