Skip to content

Commit 1061c9f

Browse files
fix doctype (#1301)
1 parent 839ce3a commit 1061c9f

File tree

3 files changed

+129
-26
lines changed

3 files changed

+129
-26
lines changed

pgml-dashboard/src/api/cms.rs

Lines changed: 122 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ lazy_static! {
6363
);
6464
}
6565

66-
#[derive(Debug, Serialize, Deserialize, Clone)]
66+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
6767
pub enum DocType {
6868
Blog,
6969
Docs,
@@ -115,15 +115,27 @@ pub struct Document {
115115
// Gets document markdown
116116
impl Document {
117117
pub async fn from_path(path: &PathBuf) -> anyhow::Result<Document, std::io::Error> {
118-
debug!("path: {:?}", path);
119-
120-
let regex = regex::Regex::new(r#".*/pgml-cms/([^"]*)/(.*)\.md"#).unwrap();
121-
122-
let doc_type = match regex.captures(&path.clone().display().to_string()) {
123-
Some(c) => DocType::from_str(&c[1]).ok(),
118+
let doc_type = match path.strip_prefix(config::cms_dir()) {
119+
Ok(path) => {
120+
match path.into_iter().next() {
121+
Some(dir) => {
122+
match &PathBuf::from(dir).display().to_string()[..] {
123+
"blog" => Some(DocType::Blog),
124+
"docs" => Some(DocType::Docs),
125+
"careers" => Some(DocType::Careers),
126+
_ => None
127+
}
128+
},
129+
_ => None
130+
}
131+
},
124132
_ => None,
125133
};
126134

135+
if doc_type.is_none() {
136+
warn!("doc_type not parsed from path: {path:?}");
137+
}
138+
127139
let contents = tokio::fs::read_to_string(&path).await?;
128140

129141
let parts = contents.split("---").collect::<Vec<&str>>();
@@ -143,7 +155,7 @@ impl Document {
143155
(None, contents)
144156
};
145157

146-
let default_image = ".gitbook/assets/blog_image_placeholder.png";
158+
let default_image_path = BLOG.asset_url_root.join("blog_image_placeholder.png").display().to_string();
147159

148160
// parse meta section
149161
let (description, image, featured, tags) = match meta {
@@ -154,14 +166,22 @@ impl Document {
154166
Some(meta["description"].as_str().unwrap().to_string())
155167
};
156168

169+
// For now the only images shown are blog images TODO: use doc_type to set asset path when working.
157170
let image = if meta["image"].is_badvalue() {
158-
Some(format!("/{}/{}", doc_type.clone().unwrap().to_string(), default_image))
171+
Some(default_image_path.clone())
159172
} else {
160-
Some(format!(
161-
"/{}/{}",
162-
doc_type.clone().unwrap().to_string().to_string(),
163-
meta["image"].as_str().unwrap()
164-
))
173+
match PathBuf::from_str(meta["image"].as_str().unwrap()) {
174+
Ok(image_path) => {
175+
match image_path.file_name() {
176+
Some(file_name) => {
177+
let file = PathBuf::from(file_name).display().to_string();
178+
Some(BLOG.asset_url_root.join(file).display().to_string())
179+
},
180+
_ => Some(default_image_path.clone())
181+
}
182+
},
183+
_ => Some(default_image_path.clone())
184+
}
165185
};
166186

167187
let featured = if meta["featured"].is_badvalue() {
@@ -184,15 +204,15 @@ impl Document {
184204
}
185205
None => (
186206
None,
187-
Some(format!("/{}/{}", doc_type.clone().unwrap().to_string(), default_image)),
207+
Some(default_image_path.clone()),
188208
false,
189209
Vec::new(),
190210
),
191211
};
192212

193213
let thumbnail = match &image {
194214
Some(image) => {
195-
if image.contains(default_image) {
215+
if image.contains(&default_image_path) || doc_type != Some(DocType::Blog) {
196216
None
197217
} else {
198218
Some(format!("{}{}", config::site_domain(), image))
@@ -266,6 +286,8 @@ pub struct Collection {
266286
pub index: Vec<IndexLink>,
267287
/// A list of old paths to new paths in this collection
268288
redirects: HashMap<&'static str, &'static str>,
289+
/// Url to assets for this collection
290+
pub asset_url_root: PathBuf
269291
}
270292

271293
impl Collection {
@@ -276,13 +298,15 @@ impl Collection {
276298
let root_dir = config::cms_dir().join(&slug);
277299
let asset_dir = root_dir.join(".gitbook").join("assets");
278300
let url_root = PathBuf::from("/").join(&slug);
301+
let asset_url_root = PathBuf::from("/").join(&slug).join(".gitbook").join("assets");
279302

280303
let mut collection = Collection {
281304
name,
282305
root_dir,
283306
asset_dir,
284307
url_root,
285308
redirects,
309+
asset_url_root,
286310
..Default::default()
287311
};
288312
collection.build_index(hide_root);
@@ -419,6 +443,49 @@ impl Collection {
419443
Ok(links)
420444
}
421445

446+
// Convert a IndexLink from summary to a file path.
447+
pub fn url_to_path(&self, url: &str) -> PathBuf {
448+
let url = if url.ends_with('/') {
449+
format!("{url}README.md")
450+
} else {
451+
format!("{url}.md")
452+
};
453+
454+
let mut path = PathBuf::from(url);
455+
if path.has_root() {
456+
path = path.strip_prefix("/").unwrap().to_owned();
457+
}
458+
459+
let mut path_v = path.components().collect::<Vec<_>>();
460+
path_v.remove(0);
461+
462+
let path_pb = PathBuf::from_iter(path_v.iter());
463+
464+
self.root_dir.join(path_pb)
465+
}
466+
467+
// get all urls in the collection and preserve order.
468+
pub fn get_all_urls(&self) -> Vec<String> {
469+
let mut urls: Vec<String> = Vec::new();
470+
let mut children: Vec<&IndexLink> = Vec::new();
471+
for item in &self.index {
472+
children.push(item);
473+
}
474+
475+
children.reverse();
476+
477+
while children.len() > 0 {
478+
let current = children.pop().unwrap();
479+
urls.push(current.href.clone());
480+
481+
for i in (0..current.children.len()).rev() {
482+
children.push(&current.children[i])
483+
}
484+
}
485+
486+
urls
487+
}
488+
422489
// Sets specified index as currently viewed.
423490
fn open_index(&self, path: &PathBuf) -> Vec<IndexLink> {
424491
self.index
@@ -802,4 +869,43 @@ This is the end of the markdown
802869
== expected.chars().filter(|c| !c.is_whitespace()).collect::<String>()
803870
)
804871
}
872+
873+
// Test we can parse doc meta with out issue.
874+
#[sqlx::test]
875+
async fn docs_meta_parse() {
876+
let collection = &crate::api::cms::DOCS;
877+
878+
let urls = collection.get_all_urls();
879+
880+
for url in urls {
881+
let path = collection.url_to_path(url.as_ref());
882+
crate::api::cms::Document::from_path(&path).await.unwrap();
883+
}
884+
}
885+
886+
// Test we can parse blog meta with out issue.
887+
#[sqlx::test]
888+
async fn blog_meta_parse() {
889+
let collection = &crate::api::cms::BLOG;
890+
891+
let urls = collection.get_all_urls();
892+
893+
for url in urls {
894+
let path = collection.url_to_path(url.as_ref());
895+
crate::api::cms::Document::from_path(&path).await.unwrap();
896+
}
897+
}
898+
899+
// Test we can parse career meta with out issue.
900+
#[sqlx::test]
901+
async fn career_meta_parse() {
902+
let collection = &crate::api::cms::CAREERS;
903+
904+
let urls = collection.get_all_urls();
905+
906+
for url in urls {
907+
let path = collection.url_to_path(url.as_ref());
908+
crate::api::cms::Document::from_path(&path).await.unwrap();
909+
}
910+
}
805911
}

pgml-dashboard/src/components/layouts/head/template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<meta property="og:site_name" content="PostgresML">
2626
<meta property="og:type" content="website">
2727
<meta property="og:title" content="<%= title %> – PostgresML">
28-
<meta property="og:url" content="http://www.postgresML.org">
28+
<meta property="og:url" content="http://www.postgresml.org">
2929
<meta property="og:locale" content="en_US">
3030

3131
<meta name="twitter:site" content="@postgresml">

pgml-dashboard/src/components/pages/blog/landing_page/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::guards::Cluster;
66
use crate::Notification;
77
use pgml_components::component;
88
use sailfish::TemplateOnce;
9-
use std::path::PathBuf;
109

1110
#[derive(TemplateOnce, Default)]
1211
#[template(path = "pages/blog/landing_page/template.html")]
@@ -26,14 +25,12 @@ impl LandingPage {
2625
}
2726

2827
pub async fn index(mut self, collection: &Collection) -> Self {
29-
let index = &collection.index;
28+
let urls = collection.get_all_urls();
3029

31-
for item in index {
32-
let path = &item.href.replace("/blog/", "");
33-
let root = collection.root_dir.clone();
34-
let file = root.join(format!("{}.md", path));
30+
for url in urls {
31+
let file = collection.url_to_path(url.as_ref());
3532

36-
let doc = crate::api::cms::Document::from_path(&PathBuf::from(file))
33+
let doc = crate::api::cms::Document::from_path(&file)
3734
.await
3835
.unwrap();
3936

@@ -46,7 +43,7 @@ impl LandingPage {
4643
featured: doc.featured,
4744
tags: doc.tags,
4845
title: doc.title,
49-
path: item.href.clone(),
46+
path: url,
5047
};
5148

5249
self.index.push(meta)
@@ -58,7 +55,7 @@ impl LandingPage {
5855
let mut cycle = 0;
5956
let mut html: Vec<String> = Vec::new();
6057

61-
// blogs are in cms Readme order, make the first post the big card and second long card.
58+
// blogs are in cms Summary order, make the first post the big card and second long card.
6259
let big_index = index.remove(0);
6360
let long_index = index.remove(0);
6461
let small_image_index = index.remove(0);

0 commit comments

Comments
 (0)