Skip to content

Commit

Permalink
fix: allow for only embed content
Browse files Browse the repository at this point in the history
  • Loading branch information
MineBartekSA committed Aug 1, 2023
1 parent e450f22 commit 6109c3e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions mdbook-discord-components/src/generators/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl Generator for HTMLGenerator {
let html = "<discord-messages>\n".to_owned() +
&components.tree.drain(..).map(|tree| generate_components(&components.roles, tree, 1)).collect::<String>() +
"</discord-messages>";
#[cfg(debug_assertions)]
eprintln!("HTML Generator generated following:\n{html}");
Ok(Event::Html(html.into()))
}
Expand Down
25 changes: 20 additions & 5 deletions mdbook-discord-components/src/parsers/yaml_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ impl Parser for YamlParser {
match serde_yaml::from_str::<Vec<YamlMessage>>(&code_block.code) {
Ok(mut m) => {
for (i, mut mess) in m.drain(..).enumerate() {
mess.prepare();
if !mess.is_valid() {
return Err(YamlParserError::new(format!("Invalid message #{}", i+1)).anyhow());
}
mess.prepare();
mess.push_to_tree(&mut components);
}
},
Err(_) => {
let mut message = serde_yaml::from_str::<YamlMessage>(&code_block.code)?;
message.prepare();
if !message.is_valid() {
return Err(YamlParserError::new("Invalid message").anyhow());
}
message.prepare();
message.push_to_tree(&mut components);
}
};
Expand Down Expand Up @@ -79,6 +79,7 @@ struct YamlBasicMessage {
components: Option<Vec<YamlActionRow>>,
invites: Option<Vec<YamlInvite>>,

#[serde(default)]
content: String,
}

Expand Down Expand Up @@ -134,7 +135,12 @@ impl YamlMessage {
if basic.components.is_some() && basic.components.as_ref().unwrap().len() > 5 {
return false
}
!((basic.user_id.is_none() && basic.username.is_none()) || basic.content.is_empty())
if basic.content.is_empty() {
if basic.embeds.is_none() || basic.embeds.as_ref().unwrap().is_empty() {
return false
}
}
!(basic.user_id.is_none() && basic.username.is_none())
},
YamlMessage::System(ref system) => {
!system.content.is_empty()
Expand All @@ -149,7 +155,12 @@ impl YamlMessage {
if basic.components.is_some() && basic.components.as_ref().unwrap().len() > 5 {
return false
}
!(basic.username.is_none() || basic.content.is_empty())
if basic.content.is_empty() {
if basic.embeds.is_none() || basic.embeds.as_ref().unwrap().is_empty() {
return false
}
}
!basic.username.is_none()
},
YamlMessage::System(ref system) => {
!system.content.is_empty()
Expand Down Expand Up @@ -192,7 +203,11 @@ impl YamlMessage {
if let Some(verified) = basic.verified {
message.verified = verified;
}
let mut tree = vec![ComponentTree::Text(basic.content)];
let mut tree = if basic.content.is_empty() {
vec![]
} else {
vec![ComponentTree::Text(basic.content)]
};
if let Some(embeds) = basic.embeds {
for mut embed in embeds {
embed.prepare();
Expand Down

0 comments on commit 6109c3e

Please sign in to comment.