Merge pull request 'escape special prefixes in plaintext nodes' (#9) from boringcactus/maj:preserve-texthood-of-text-nodes into main
continuous-integration/drone/push Build is failing Details

Reviewed-on: #9
This commit is contained in:
Cadey Ratio 2020-09-26 23:09:23 +00:00
commit e58a01d14d
1 changed files with 18 additions and 1 deletions

View File

@ -60,7 +60,13 @@ pub fn render(nodes: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
for node in nodes { for node in nodes {
match node { match node {
Text(body) => write!(out, "{}\n", body)?, Text(body) => {
let special_prefixes = ["=>", "```", "#", "*", ">"];
if special_prefixes.iter().any(|prefix| body.starts_with(prefix)) {
write!(out, " ")?;
}
write!(out, "{}\n", body)?
},
Link { to, name } => match name { Link { to, name } => match name {
Some(name) => write!(out, "=> {} {}\n", to, name)?, Some(name) => write!(out, "=> {} {}\n", to, name)?,
None => write!(out, "=> {}\n", to)?, None => write!(out, "=> {}\n", to)?,
@ -337,4 +343,15 @@ mod tests {
]; ];
assert_eq!(expected, parse(msg)); assert_eq!(expected, parse(msg));
} }
#[test]
fn ambiguous_text() {
let _ = pretty_env_logger::try_init();
let original = Node::Text("#1 World's Best Coder".to_string());
let expected = " #1 World's Best Coder\n";
let mut rendered: Vec<u8> = vec![];
render(vec![original], &mut rendered).unwrap();
let rendered = String::from_utf8(rendered).unwrap();
assert_eq!(expected, rendered)
}
} }