Wrap markdown in remote object content in <p> tag

This commit is contained in:
silverpill 2022-10-13 12:44:45 +00:00
parent c95e983a3d
commit 03ebf75da2
2 changed files with 15 additions and 6 deletions

View file

@ -66,6 +66,9 @@ pub struct Object {
#[serde(skip_serializing_if = "Option::is_none")]
pub former_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub media_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub object: Option<String>,

View file

@ -65,18 +65,24 @@ fn parse_object_url(value: &JsonValue) -> Result<String, ConversionError> {
}
pub fn get_note_content(object: &Object) -> Result<String, ValidationError> {
let mut content = object.content.as_deref()
let mut content = if let Some(ref content) = object.content {
if object.media_type == Some("text/markdown".to_string()) {
format!("<p>{}</p>", content)
} else {
// HTML
content.to_string()
}
} else {
// Lemmy pages and PeerTube videos have "name" property
.or(object.name.as_deref())
.unwrap_or("")
.to_owned();
object.name.as_deref().unwrap_or("").to_string()
};
if object.object_type != NOTE {
if let Some(ref value) = object.url {
// Append link to object
let object_url = parse_object_url(value)
.map_err(|_| ValidationError("invalid object URL"))?;
content += &format!(
r#"<br><p><a href="{0}">{0}</a></p>"#,
r#"<p><a href="{0}">{0}</a></p>"#,
object_url,
);
};
@ -423,7 +429,7 @@ mod tests {
let content = get_note_content(&object).unwrap();
assert_eq!(
content,
r#"test-content<br><p><a href="https://example.org/xyz" rel="noopener">https://example.org/xyz</a></p>"#,
r#"test-content<p><a href="https://example.org/xyz" rel="noopener">https://example.org/xyz</a></p>"#,
);
}