Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 49 additions & 28 deletions pgml-dashboard/src/api/cms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ pub struct Document {
}

impl Document {
pub async fn from_path(path: &PathBuf) -> anyhow::Result<Document> {
let contents = tokio::fs::read_to_string(&path).await?;
pub async fn from_path(path: &PathBuf) -> anyhow::Result<Document, std::io::Error> {
let contents = match tokio::fs::read_to_string(&path).await {
Ok(contents) => contents,
Err(_) => String::from("<h3>Failed to find your requested document!</h3>"),
};

let parts = contents.split("---").collect::<Vec<&str>>();

Expand Down Expand Up @@ -271,35 +274,40 @@ impl Collection {

// renders document in layout
async fn render<'a>(&self, path: &'a PathBuf, cluster: &Cluster) -> Result<ResponseOk, Status> {
let doc = Document::from_path(&path).await.unwrap();
let index = self.open_index(doc.path);
match Document::from_path(&path).await {
Ok(doc) => {
let index = self.open_index(doc.path);

let user = if cluster.context.user.is_anonymous() {
None
} else {
Some(cluster.context.user.clone())
};

let mut layout = crate::templates::Layout::new(&doc.title, Some(cluster));
if let Some(image) = doc.image {
layout.image(&config::asset_url(image.into()));
}
if let Some(description) = &doc.description {
layout.description(description);
}
if let Some(user) = &user {
layout.user(user);
}

let user = if cluster.context.user.is_anonymous() {
None
} else {
Some(cluster.context.user.clone())
};
let layout = layout
.nav_title(&self.name)
.nav_links(&index)
.toc_links(&doc.toc_links)
.footer(cluster.context.marketing_footer.to_string());

let mut layout = crate::templates::Layout::new(&doc.title, Some(cluster));
if let Some(image) = doc.image {
layout.image(&config::asset_url(image.into()));
}
if let Some(description) = &doc.description {
layout.description(description);
}
if let Some(user) = &user {
layout.user(user);
Ok(ResponseOk(
layout.render(crate::templates::Article { content: doc.html }),
))
}
// Return page not found on bad path
_ => Err(Status::NotFound),
}

let layout = layout
.nav_title(&self.name)
.nav_links(&index)
.toc_links(&doc.toc_links)
.footer(cluster.context.marketing_footer.to_string());

Ok(ResponseOk(
layout.render(crate::templates::Article { content: doc.html }),
))
}
}

Expand Down Expand Up @@ -534,4 +542,17 @@ This is the end of the markdown
)
}
}

#[sqlx::test]
async fn doc_not_found() {
let client = Client::tracked(rocket().await).await.unwrap();
let req = client.get("/docs/not_a_doc");
let rsp = req.dispatch().await;

assert!(
rsp.status() == Status::NotFound,
"Returned status {:?}",
rsp.status()
);
}
}
2 changes: 1 addition & 1 deletion pgml-dashboard/src/utils/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl SyntaxHighlighterAdapter for SyntaxHighlighter {
.enumerate()
.map(|(index, code)| {
format!(
r#"<div class="code-line-highlight-{}">{}</div>"#,
r#"<div class="code-line-highlight-{} d-inline-block" style="line-height: 20px;">{}</div>"#,
match options.highlight.get(&(index + 1).to_string()) {
Some(color) => color,
_ => "none",
Expand Down
9 changes: 9 additions & 0 deletions pgml-dashboard/static/css/scss/pages/_docs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,14 @@
}
}
}

figure {
display: flex;
flex-direction: column;
img, figcaption {
margin-left: auto;
margin-right: auto;
}
}
}