Content-Length: 505896 | pFad | http://github.com/postgresml/postgresml/pull/1301/files

45 fix doctype by chillenberger · Pull Request #1301 · postgresml/postgresml · GitHub
Skip to content

fix doctype #1301

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2024
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
138 changes: 122 additions & 16 deletions pgml-dashboard/src/api/cms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ lazy_static! {
);
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum DocType {
Blog,
Docs,
Expand Down Expand Up @@ -115,15 +115,27 @@ pub struct Document {
// Gets document markdown
impl Document {
pub async fn from_path(path: &PathBuf) -> anyhow::Result<Document, std::io::Error> {
debug!("path: {:?}", path);

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(),
let doc_type = match path.strip_prefix(config::cms_dir()) {
Ok(path) => {
match path.into_iter().next() {
Some(dir) => {
match &PathBuf::from(dir).display().to_string()[..] {
"blog" => Some(DocType::Blog),
"docs" => Some(DocType::Docs),
"careers" => Some(DocType::Careers),
_ => None
}
},
_ => None
}
},
_ => None,
};

if doc_type.is_none() {
warn!("doc_type not parsed from path: {path:?}");
}

let contents = tokio::fs::read_to_string(&path).await?;

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

let default_image = ".gitbook/assets/blog_image_placeholder.png";
let default_image_path = BLOG.asset_url_root.join("blog_image_placeholder.png").display().to_string();

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

// For now the only images shown are blog images TODO: use doc_type to set asset path when working.
let image = if meta["image"].is_badvalue() {
Some(format!("/{}/{}", doc_type.clone().unwrap().to_string(), default_image))
Some(default_image_path.clone())
} else {
Some(format!(
"/{}/{}",
doc_type.clone().unwrap().to_string().to_string(),
meta["image"].as_str().unwrap()
))
match PathBuf::from_str(meta["image"].as_str().unwrap()) {
Ok(image_path) => {
match image_path.file_name() {
Some(file_name) => {
let file = PathBuf::from(file_name).display().to_string();
Some(BLOG.asset_url_root.join(file).display().to_string())
},
_ => Some(default_image_path.clone())
}
},
_ => Some(default_image_path.clone())
}
};

let featured = if meta["featured"].is_badvalue() {
Expand All @@ -184,15 +204,15 @@ impl Document {
}
None => (
None,
Some(format!("/{}/{}", doc_type.clone().unwrap().to_string(), default_image)),
Some(default_image_path.clone()),
false,
Vec::new(),
),
};

let thumbnail = match &image {
Some(image) => {
if image.contains(default_image) {
if image.contains(&default_image_path) || doc_type != Some(DocType::Blog) {
None
} else {
Some(format!("{}{}", config::site_domain(), image))
Expand Down Expand Up @@ -266,6 +286,8 @@ pub struct Collection {
pub index: Vec<IndexLink>,
//github.com/ A list of old paths to new paths in this collection
redirects: HashMap<&'static str, &'static str>,
//github.com/ Url to assets for this collection
pub asset_url_root: PathBuf
}

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

let mut collection = Collection {
name,
root_dir,
asset_dir,
url_root,
redirects,
asset_url_root,
..Default::default()
};
collection.build_index(hide_root);
Expand Down Expand Up @@ -419,6 +443,49 @@ impl Collection {
Ok(links)
}

// Convert a IndexLink from summary to a file path.
pub fn url_to_path(&self, url: &str) -> PathBuf {
let url = if url.ends_with('/') {
format!("{url}README.md")
} else {
format!("{url}.md")
};

let mut path = PathBuf::from(url);
if path.has_root() {
path = path.strip_prefix("/").unwrap().to_owned();
}

let mut path_v = path.components().collect::<Vec<_>>();
path_v.remove(0);

let path_pb = PathBuf::from_iter(path_v.iter());

self.root_dir.join(path_pb)
}

// get all urls in the collection and preserve order.
pub fn get_all_urls(&self) -> Vec<String> {
let mut urls: Vec<String> = Vec::new();
let mut children: Vec<&IndexLink> = Vec::new();
for item in &self.index {
children.push(item);
}

children.reverse();

while children.len() > 0 {
let current = children.pop().unwrap();
urls.push(current.href.clone());

for i in (0..current.children.len()).rev() {
children.push(&current.children[i])
}
}

urls
}

// Sets specified index as currently viewed.
fn open_index(&self, path: &PathBuf) -> Vec<IndexLink> {
self.index
Expand Down Expand Up @@ -802,4 +869,43 @@ This is the end of the markdown
== expected.chars().filter(|c| !c.is_whitespace()).collect::<String>()
)
}

// Test we can parse doc meta with out issue.
#[sqlx::test]
async fn docs_meta_parse() {
let collection = &crate::api::cms::DOCS;

let urls = collection.get_all_urls();

for url in urls {
let path = collection.url_to_path(url.as_ref());
crate::api::cms::Document::from_path(&path).await.unwrap();
}
}

// Test we can parse blog meta with out issue.
#[sqlx::test]
async fn blog_meta_parse() {
let collection = &crate::api::cms::BLOG;

let urls = collection.get_all_urls();

for url in urls {
let path = collection.url_to_path(url.as_ref());
crate::api::cms::Document::from_path(&path).await.unwrap();
}
}

// Test we can parse career meta with out issue.
#[sqlx::test]
async fn career_meta_parse() {
let collection = &crate::api::cms::CAREERS;

let urls = collection.get_all_urls();

for url in urls {
let path = collection.url_to_path(url.as_ref());
crate::api::cms::Document::from_path(&path).await.unwrap();
}
}
}
2 changes: 1 addition & 1 deletion pgml-dashboard/src/components/layouts/head/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<meta property="og:site_name" content="PostgresML">
<meta property="og:type" content="website">
<meta property="og:title" content="<%= title %> – PostgresML">
<meta property="og:url" content="http://www.postgresML.org">
<meta property="og:url" content="http://www.postgresml.org">
<meta property="og:locale" content="en_US">

<meta name="twitter:site" content="@postgresml">
Expand Down
15 changes: 6 additions & 9 deletions pgml-dashboard/src/components/pages/blog/landing_page/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::guards::Cluster;
use crate::Notification;
use pgml_components::component;
use sailfish::TemplateOnce;
use std::path::PathBuf;

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

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

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

let doc = crate::api::cms::Document::from_path(&PathBuf::from(file))
let doc = crate::api::cms::Document::from_path(&file)
.await
.unwrap();

Expand All @@ -46,7 +43,7 @@ impl LandingPage {
featured: doc.featured,
tags: doc.tags,
title: doc.title,
path: item.href.clone(),
path: url,
};

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

// blogs are in cms Readme order, make the first post the big card and second long card.
// blogs are in cms Summary order, make the first post the big card and second long card.
let big_index = index.remove(0);
let long_index = index.remove(0);
let small_image_index = index.remove(0);
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/postgresml/postgresml/pull/1301/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy