Skip to content

Commit bcc83ef

Browse files
Dan docs update (#1250)
1 parent a50d276 commit bcc83ef

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

pgml-dashboard/src/api/cms.rs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ pub struct Document {
3939
}
4040

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

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

@@ -271,35 +274,40 @@ impl Collection {
271274

272275
// renders document in layout
273276
async fn render<'a>(&self, path: &'a PathBuf, cluster: &Cluster) -> Result<ResponseOk, Status> {
274-
let doc = Document::from_path(&path).await.unwrap();
275-
let index = self.open_index(doc.path);
277+
match Document::from_path(&path).await {
278+
Ok(doc) => {
279+
let index = self.open_index(doc.path);
280+
281+
let user = if cluster.context.user.is_anonymous() {
282+
None
283+
} else {
284+
Some(cluster.context.user.clone())
285+
};
286+
287+
let mut layout = crate::templates::Layout::new(&doc.title, Some(cluster));
288+
if let Some(image) = doc.image {
289+
layout.image(&config::asset_url(image.into()));
290+
}
291+
if let Some(description) = &doc.description {
292+
layout.description(description);
293+
}
294+
if let Some(user) = &user {
295+
layout.user(user);
296+
}
276297

277-
let user = if cluster.context.user.is_anonymous() {
278-
None
279-
} else {
280-
Some(cluster.context.user.clone())
281-
};
298+
let layout = layout
299+
.nav_title(&self.name)
300+
.nav_links(&index)
301+
.toc_links(&doc.toc_links)
302+
.footer(cluster.context.marketing_footer.to_string());
282303

283-
let mut layout = crate::templates::Layout::new(&doc.title, Some(cluster));
284-
if let Some(image) = doc.image {
285-
layout.image(&config::asset_url(image.into()));
286-
}
287-
if let Some(description) = &doc.description {
288-
layout.description(description);
289-
}
290-
if let Some(user) = &user {
291-
layout.user(user);
304+
Ok(ResponseOk(
305+
layout.render(crate::templates::Article { content: doc.html }),
306+
))
307+
}
308+
// Return page not found on bad path
309+
_ => Err(Status::NotFound),
292310
}
293-
294-
let layout = layout
295-
.nav_title(&self.name)
296-
.nav_links(&index)
297-
.toc_links(&doc.toc_links)
298-
.footer(cluster.context.marketing_footer.to_string());
299-
300-
Ok(ResponseOk(
301-
layout.render(crate::templates::Article { content: doc.html }),
302-
))
303311
}
304312
}
305313

@@ -534,4 +542,17 @@ This is the end of the markdown
534542
)
535543
}
536544
}
545+
546+
#[sqlx::test]
547+
async fn doc_not_found() {
548+
let client = Client::tracked(rocket().await).await.unwrap();
549+
let req = client.get("/docs/not_a_doc");
550+
let rsp = req.dispatch().await;
551+
552+
assert!(
553+
rsp.status() == Status::NotFound,
554+
"Returned status {:?}",
555+
rsp.status()
556+
);
557+
}
537558
}

pgml-dashboard/src/utils/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl SyntaxHighlighterAdapter for SyntaxHighlighter {
446446
.enumerate()
447447
.map(|(index, code)| {
448448
format!(
449-
r#"<div class="code-line-highlight-{}">{}</div>"#,
449+
r#"<div class="code-line-highlight-{} d-inline-block" style="line-height: 20px;">{}</div>"#,
450450
match options.highlight.get(&(index + 1).to_string()) {
451451
Some(color) => color,
452452
_ => "none",

pgml-dashboard/static/css/scss/pages/_docs.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,14 @@
169169
}
170170
}
171171
}
172+
173+
figure {
174+
display: flex;
175+
flex-direction: column;
176+
img, figcaption {
177+
margin-left: auto;
178+
margin-right: auto;
179+
}
180+
}
172181
}
173182

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy