- Notifications
You must be signed in to change notification settings - Fork328
Dan blog landing page#1294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
9b28417
102a8e4
45a5635
b23fd18
81b8002
40510fb
a7bea23
c198f61
18a0592
48eb715
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,6 +3,8 @@ use std::{ | ||
path::{Path, PathBuf}, | ||
}; | ||
use std::str::FromStr; | ||
use comrak::{format_html_with_plugins, parse_document, Arena, ComrakPlugins}; | ||
use lazy_static::lazy_static; | ||
use markdown::mdast::Node; | ||
@@ -16,7 +18,6 @@ use crate::{ | ||
templates::docs::*, | ||
utils::config, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
lazy_static! { | ||
@@ -61,35 +62,66 @@ lazy_static! { | ||
); | ||
} | ||
#[derive(PartialEq, Debug, Serialize, Deserialize)] | ||
pub enum DocType { | ||
Blog, | ||
Docs, | ||
Careers, | ||
} | ||
impl FromStr for DocType { | ||
type Err = (); | ||
fn from_str(s: &str) -> Result<DocType, Self::Err> { | ||
match s { | ||
"blog" => Ok(DocType::Blog), | ||
"Doc" => Ok(DocType::Docs), | ||
"Careers" => Ok(DocType::Careers), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Document { | ||
/// The absolute path on disk | ||
pub path: PathBuf, | ||
pub description: Option<String>, | ||
pub author: Option<String>, | ||
pub author_image: Option<String>, | ||
pub featured: bool, | ||
pub date: Option<chrono::NaiveDate>, | ||
pub tags: Vec<String>, | ||
pub image: Option<String>, | ||
pub title: String, | ||
pub toc_links: Vec<TocLink>, | ||
pub contents: String, | ||
pub doc_type: Option<DocType>, | ||
} | ||
// Gets document markdown | ||
impl Document { | ||
pub async fn from_path(path: &PathBuf) -> anyhow::Result<Document, std::io::Error> { | ||
warn!("path: {:?}", path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Never noticed the ^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. yeah, I can do that in my next merge. | ||
let regex = regex::Regex::new(r#".*/pgml-cms/([^"]*)/(.*)\.md"#).unwrap(); | ||
let doc_type = match regex.captures(&path.clone().display().to_string()) { | ||
Some(c) => DocType::from_str(&c[1]).ok(), | ||
_ => None, | ||
}; | ||
let contents = tokio::fs::read_to_string(&path).await?; | ||
let parts = contents.split("---").collect::<Vec<&str>>(); | ||
let (meta, contents) = if parts.len() > 1 { | ||
match YamlLoader::load_from_str(parts[1]) { | ||
Ok(meta) => { | ||
if meta.len() == 0 || meta[0].as_hash().is_none() { | ||
(None, contents) | ||
} else { | ||
(Some(meta[0].clone()), parts[2..].join("---").to_string()) | ||
} | ||
} | ||
Err(_) => (None, contents), | ||
@@ -98,16 +130,78 @@ impl Document { | ||
(None, contents) | ||
}; | ||
// parse meta section | ||
let (description, image, featured, tags) = match meta { | ||
Some(meta) => { | ||
let description = if meta["description"].is_badvalue() { | ||
None | ||
} else { | ||
Some(meta["description"].as_str().unwrap().to_string()) | ||
}; | ||
let image = if meta["image"].is_badvalue() { | ||
Some(".gitbook/assets/blog_image_placeholder.png".to_string()) | ||
} else { | ||
Some(meta["image"].as_str().unwrap().to_string()) | ||
}; | ||
let featured = if meta["featured"].is_badvalue() { | ||
false | ||
} else { | ||
meta["featured"].as_bool().unwrap() | ||
}; | ||
let tags = if meta["tags"].is_badvalue() { | ||
Vec::new() | ||
} else { | ||
let mut tags = Vec::new(); | ||
for tag in meta["tags"].as_vec().unwrap() { | ||
tags.push(tag.as_str().unwrap_or_else(|| "").to_string()); | ||
} | ||
tags | ||
}; | ||
(description, image, featured, tags) | ||
} | ||
None => ( | ||
None, | ||
Some(".gitbook/assets/blog_image_placeholder.png".to_string()), | ||
false, | ||
Vec::new(), | ||
), | ||
}; | ||
// Parse Markdown | ||
let arena = Arena::new(); | ||
let root = parse_document(&arena, &contents, &crate::utils::markdown::options()); | ||
let title = crate::utils::markdown::get_title(root).unwrap(); | ||
let toc_links = crate::utils::markdown::get_toc(root).unwrap(); | ||
let (author, date, author_image) = crate::utils::markdown::get_author(root); | ||
let document = Document { | ||
path: path.to_owned(), | ||
description, | ||
author, | ||
author_image, | ||
date, | ||
featured, | ||
tags, | ||
image, | ||
title, | ||
toc_links, | ||
contents, | ||
doc_type, | ||
}; | ||
Ok(document) | ||
} | ||
pub fn html(self) -> String { | ||
let contents = self.contents; | ||
// Parse Markdown | ||
let arena = Arena::new(); | ||
let spaced_contents = crate::utils::markdown::gitbook_preprocess(&contents); | ||
let root = parse_document(&arena, &spaced_contents, &crate::utils::markdown::options()); | ||
// MkDocs, gitbook syntax support, e.g. tabs, notes, alerts, etc. | ||
crate::utils::markdown::mkdocs(root, &arena).unwrap(); | ||
@@ -122,31 +216,23 @@ impl Document { | ||
format_html_with_plugins(root, &crate::utils::markdown::options(), &mut html, &plugins).unwrap(); | ||
let html = String::from_utf8(html).unwrap(); | ||
html | ||
} | ||
} | ||
/// A Gitbook collection of documents | ||
#[derive(Default)] | ||
pubstruct Collection { | ||
/// The properly capitalized identifier for this collection | ||
name: String, | ||
/// The root location on disk for this collection | ||
pubroot_dir: PathBuf, | ||
/// The root location for gitbook assets | ||
pubasset_dir: PathBuf, | ||
/// The base url for this collection | ||
url_root: PathBuf, | ||
/// A hierarchical list of content in this collection | ||
pubindex: Vec<IndexLink>, | ||
/// A list of old paths to new paths in this collection | ||
redirects: HashMap<&'static str, &'static str>, | ||
} | ||
@@ -303,7 +389,7 @@ impl Collection { | ||
} | ||
// Sets specified index as currently viewed. | ||
fn open_index(&self, path:&PathBuf) -> Vec<IndexLink> { | ||
self.index | ||
.clone() | ||
.iter_mut() | ||
@@ -330,10 +416,10 @@ impl Collection { | ||
match Document::from_path(&path).await { | ||
Ok(doc) => { | ||
let index = self.open_index(&doc.path); | ||
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 { | ||
@@ -351,7 +437,7 @@ impl Collection { | ||
.footer(cluster.context.marketing_footer.to_string()); | ||
Ok(ResponseOk( | ||
layout.render(crate::templates::Article { content: doc.html() }), | ||
)) | ||
} | ||
// Return page not found on bad path | ||
@@ -438,6 +524,20 @@ async fn get_docs( | ||
DOCS.get_content(path, cluster, origin).await | ||
} | ||
#[get("/blog")] | ||
async fn blog_landing_page(cluster: &Cluster) -> Result<ResponseOk, crate::responses::NotFound> { | ||
let layout = crate::components::layouts::marketing::Base::new("Blog landing page", Some(cluster)) | ||
.footer(cluster.context.marketing_footer.to_string()); | ||
Ok(ResponseOk( | ||
layout.render( | ||
crate::components::pages::blog::LandingPage::new(cluster) | ||
.index(&BLOG) | ||
.await, | ||
), | ||
)) | ||
} | ||
#[get("/user_guides/<path..>", rank = 5)] | ||
async fn get_user_guides( | ||
path: PathBuf, | ||
@@ -449,6 +549,7 @@ async fn get_user_guides( | ||
pub fn routes() -> Vec<Route> { | ||
routes![ | ||
blog_landing_page, | ||
get_blog, | ||
get_blog_asset, | ||
get_careers, | ||
Uh oh!
There was an error while loading.Please reload this page.