Skip to content

Commit bd7c50e

Browse files
committed
blogs handlers
1 parent eeb435f commit bd7c50e

File tree

18 files changed

+437
-425
lines changed

18 files changed

+437
-425
lines changed

pgml-dashboard/src/api/cms.rs

Lines changed: 272 additions & 283 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub struct Content {
2+
3+
}

pgml-dashboard/src/components/cms/content/template.html

Whitespace-only changes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//! Documentation and blog templates.
2+
use sailfish::TemplateOnce;
3+
4+
use crate::utils::markdown::SearchResult;
5+
6+
/// Documentation and blog link used in the left nav.
7+
#[derive(TemplateOnce, Debug, Clone)]
8+
#[template(path = "cms/index_link/template.html")]
9+
pub struct IndexLink {
10+
pub id: String,
11+
pub title: String,
12+
pub href: String,
13+
pub children: Vec<IndexLink>,
14+
pub open: bool,
15+
pub active: bool,
16+
}
17+
18+
impl IndexLink {
19+
/// Create a new documentation link.
20+
pub fn new(title: &str) -> IndexLink {
21+
IndexLink {
22+
id: crate::utils::random_string(25),
23+
title: title.to_owned(),
24+
href: "#".to_owned(),
25+
children: vec![],
26+
open: false,
27+
active: false,
28+
}
29+
}
30+
31+
/// Set the link href.
32+
pub fn href(mut self, href: &str) -> IndexLink {
33+
self.href = href.to_owned();
34+
self
35+
}
36+
37+
/// Set the link's children which are shown when the link is expanded
38+
/// using Bootstrap's collapse.
39+
pub fn children(mut self, children: Vec<IndexLink>) -> IndexLink {
40+
self.children = children;
41+
self
42+
}
43+
44+
/// Automatically expand the link and it's parents
45+
/// when one of the children is visible.
46+
pub fn should_open(&mut self, path: &str, root: &std::path::Path) -> &mut Self {
47+
info!(
48+
"should_open self: {:?}, path: {:?}, root: {:?}",
49+
self, path, root
50+
);
51+
// if path.is_empty() {
52+
// if self.href.as_str() == root.as_os_str() {
53+
// return true;
54+
// } else {
55+
// return false;
56+
// }
57+
// }
58+
self.active = self.href.ends_with(&path);
59+
self.open = self.active;
60+
for child in self.children.iter_mut() {
61+
if child.should_open(path, root).open {
62+
self.open = true;
63+
}
64+
}
65+
self
66+
}
67+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<div class="nav flex-column" role="tablist" aria-orientation="vertical">
2+
<%
3+
4+
let color = if active {
5+
"purple"
6+
} else {
7+
""
8+
};
9+
10+
if children.is_empty() {
11+
%>
12+
<a class="nav-link px-0 text-break <%- color %>" href="<%- href %>"><%- title %></a>
13+
<% } else {
14+
let aria = if open {
15+
"true"
16+
} else {
17+
"false"
18+
};
19+
20+
let show = if open {
21+
"show"
22+
} else {
23+
"false"
24+
};
25+
%>
26+
27+
<span class="px-0 d-flex justify-content-between align-items-center text-break" >
28+
<a class="nav-link px-0 text-break <%- color %>" href="<%- href %>"><%- title %></a>
29+
<span class="material-symbols-outlined" data-bs-toggle="collapse" href="#doc-<%= id %>" role="button" aria-expanded="<%- aria %>" aria-controls="doc-<%= id %>">expand_more</span>
30+
</span>
31+
<div class="collapse <%- show %> ps-3" id="doc-<%= id %>">
32+
<div class="nav flex-column" role="tablist" aria-orentation="vertical">
33+
<% for child in children.into_iter() { %>
34+
<%- child.render_once().unwrap() %>
35+
<% } %>
36+
</div>
37+
</div>
38+
<% } %>
39+
</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This file is automatically generated.
2+
// You shouldn't modify it manually.
3+
4+
// src/components/cms/content
5+
pub mod content;
6+
pub use content::Content;
7+
8+
// src/components/cms/index_link
9+
pub mod index_link;
10+
pub use index_link::IndexLink;
11+
12+
// src/components/cms/toc_link
13+
pub mod toc_link;
14+
pub use toc_link::TocLink;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct TocLink {}

pgml-dashboard/src/components/cms/toc_link/template.html

Whitespace-only changes.

pgml-dashboard/src/components/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub use breadcrumbs::Breadcrumbs;
1313
pub mod chatbot;
1414
pub use chatbot::Chatbot;
1515

16+
// src/components/cms
17+
pub mod cms;
18+
1619
// src/components/confirm_modal
1720
pub mod confirm_modal;
1821
pub use confirm_modal::ConfirmModal;

pgml-dashboard/src/components/navigation/navbar/web_app/template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
</li>
8181

8282
<li class="menu-item rounded-0 d-flex align-items-center">
83-
<a href="/docs/guides//">Docs</a>
83+
<a href="/docs/">Docs</a>
8484
</li>
8585

8686
<li class="menu-item rounded-0 d-flex align-items-center">

pgml-dashboard/src/components/sections/footers/marketing_footer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ impl MarketingFooter {
1414
pub fn new() -> MarketingFooter {
1515
MarketingFooter {
1616
solutions: vec![
17-
StaticNavLink::new("Overview".into(), "/docs/guides/".into()),
17+
StaticNavLink::new("Overview".into(), "/docs/".into()),
1818
StaticNavLink::new("Chatbot".into(), "/chatbot".into()),
1919
StaticNavLink::new("Site Search".into(), "/search".into()).disabled(true),
2020
StaticNavLink::new("Fraud Detection".into(), "/fraud".into()).disabled(true),
2121
StaticNavLink::new("Forecasting".into(), "/forecasting".into()).disabled(true),
2222
],
2323
resources: vec![
24-
StaticNavLink::new("Documentation".into(), "/docs/guides/".into()),
24+
StaticNavLink::new("Documentation".into(), "/docs/".into()),
2525
StaticNavLink::new(
2626
"Blog".into(),
2727
"/blog/speeding-up-vector-recall-by-5x-with-hnsw".into(),

pgml-dashboard/src/templates/docs.rs

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,7 @@
1-
//! Documentation and blog templates.
21
use sailfish::TemplateOnce;
32

43
use crate::utils::markdown::SearchResult;
54

6-
/// Documentation and blog link used in the left nav.
7-
#[derive(TemplateOnce, Debug, Clone)]
8-
#[template(path = "components/link.html")]
9-
pub struct NavLink {
10-
pub id: String,
11-
pub title: String,
12-
pub href: String,
13-
pub children: Vec<NavLink>,
14-
pub open: bool,
15-
pub active: bool,
16-
}
17-
18-
impl NavLink {
19-
/// Create a new documentation link.
20-
pub fn new(title: &str) -> NavLink {
21-
NavLink {
22-
id: crate::utils::random_string(25),
23-
title: title.to_owned(),
24-
href: "#".to_owned(),
25-
children: vec![],
26-
open: false,
27-
active: false,
28-
}
29-
}
30-
31-
/// Set the link href.
32-
pub fn href(mut self, href: &str) -> NavLink {
33-
self.href = href.to_owned();
34-
self
35-
}
36-
37-
/// Set the link's children which are shown when the link is expanded
38-
/// using Bootstrap's collapse.
39-
pub fn children(mut self, children: Vec<NavLink>) -> NavLink {
40-
self.children = children;
41-
self
42-
}
43-
44-
/// Automatically expand the link and it's parents
45-
/// when one of the children is visible.
46-
pub fn should_open(&mut self, path: &str, root: &std::path::Path) -> bool {
47-
info!(
48-
"should_open self: {:?}, path: {:?}, root: {:?}",
49-
self, path, root
50-
);
51-
if path.is_empty() {
52-
if self.href.as_str() == root.as_os_str() {
53-
return true;
54-
} else {
55-
return false;
56-
}
57-
}
58-
self.active = self.href.ends_with(&path);
59-
self.open = self.active;
60-
for child in self.children.iter_mut() {
61-
if child.should_open(path, root) {
62-
self.open = true;
63-
}
64-
}
65-
self.open
66-
}
67-
}
68-
695
/// The search results template.
706
#[derive(TemplateOnce)]
717
#[template(path = "components/search.html")]

pgml-dashboard/src/templates/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use pgml_components::Component;
22
use std::collections::HashMap;
33

4-
pub use crate::components::{self, NavLink, StaticNav, StaticNavLink};
4+
pub use crate::components::{self, NavLink, StaticNav, StaticNavLink, cms::index_link::IndexLink};
55

66
use sailfish::TemplateOnce;
77
use sqlx::postgres::types::PgMoney;
@@ -33,7 +33,7 @@ pub struct Layout {
3333
pub content: Option<String>,
3434
pub user: Option<models::User>,
3535
pub nav_title: Option<String>,
36-
pub nav_links: Vec<docs::NavLink>,
36+
pub nav_links: Vec<IndexLink>,
3737
pub toc_links: Vec<docs::TocLink>,
3838
pub footer: String,
3939
}
@@ -71,7 +71,7 @@ impl Layout {
7171
self
7272
}
7373

74-
pub fn nav_links(&mut self, nav_links: &[docs::NavLink]) -> &mut Self {
74+
pub fn nav_links(&mut self, nav_links: &[IndexLink]) -> &mut Self {
7575
self.nav_links = nav_links.to_vec();
7676
self
7777
}

pgml-dashboard/src/utils/config.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use std::env::var;
2-
use std::path::{Path, PathBuf};
1+
use std::{
2+
env::var,
3+
borrow::Cow,
4+
path::{Path, PathBuf}
5+
};
36

47
use lazy_static::lazy_static;
58

@@ -21,8 +24,8 @@ struct Config {
2124
static_dir: PathBuf,
2225
search_index_dir: PathBuf,
2326
render_errors: bool,
24-
css_version: String,
25-
js_version: String,
27+
css_extension: String,
28+
js_extension: String,
2629
assets_domain: Option<String>,
2730
}
2831

@@ -37,6 +40,8 @@ impl Config {
3740
}
3841
.to_string();
3942

43+
let cargo_manifest_dir = env!("CARGO_MANIFEST_DIR");
44+
4045
let github_stars = match var("GITHUB_STARS") {
4146
Ok(stars) => match stars.parse::<f32>() {
4247
Ok(stars) => format!("{:.1}K", (stars / 1000.0)),
@@ -45,7 +50,11 @@ impl Config {
4550
_ => "2.0K".to_string(),
4651
};
4752

48-
let cargo_manifest_dir = env!("CARGO_MANIFEST_DIR");
53+
let css_version = env_string_default("CSS_VERSION", "");
54+
let js_version = env_string_default("JS_VERSION", "1");
55+
56+
let css_extension = if dev_mode { "css".to_string() } else { format!("{css_version}.css") };
57+
let js_extension = if dev_mode { "js".to_string() } else { format!("{js_version}.js") };
4958

5059
Config {
5160
dev_mode,
@@ -61,9 +70,9 @@ impl Config {
6170
signup_url,
6271
standalone_dashboard: !cargo_manifest_dir.contains("deps")
6372
&& !cargo_manifest_dir.contains("cloud2"),
64-
github_stars: github_stars,
65-
css_version: env_string_default("CSS_VERSION", "1"),
66-
js_version: env_string_default("JS_VERSION", "1"),
73+
github_stars,
74+
css_extension,
75+
js_extension,
6776
assets_domain: env_string_optional("ASSETS_DOMAIN"),
6877
}
6978
}
@@ -116,29 +125,19 @@ pub fn github_stars<'a>() -> &'a str {
116125
&CONFIG.github_stars
117126
}
118127

119-
pub fn css_url() -> String {
120-
if CONFIG.dev_mode {
121-
return "/dashboard/static/css/style.css".to_string();
122-
}
123-
124-
let path = format!("/dashboard/static/css/style.{}.css", CONFIG.css_version);
125-
asset_url(&path)
128+
pub fn css_url(name: &str) -> String {
129+
let path = PathBuf::from(format!("/dashboard/static/css/{name}"));
130+
let path = path.with_extension(&CONFIG.css_extension);
131+
asset_url(path.to_string_lossy())
126132
}
127133

128134
pub fn js_url(name: &str) -> String {
129-
if CONFIG.dev_mode {
130-
return format!("/dashboard/static/js/{name}");
131-
}
132-
133-
let name = name.split(".").collect::<Vec<&str>>();
134-
let name = name[0..name.len() - 1].join(".");
135-
136-
let path = format!("/dashboard/static/js/{name}.{}.js", CONFIG.js_version);
137-
138-
asset_url(&path)
135+
let path = PathBuf::from(format!("/dashboard/static/js/{name}"));
136+
let path = path.with_extension(&CONFIG.js_extension);
137+
asset_url(path.to_string_lossy())
139138
}
140139

141-
fn asset_url(path: &str) -> String {
140+
fn asset_url(path: Cow<str>) -> String {
142141
match &CONFIG.assets_domain {
143142
Some(domain) => format!("https://{domain}{path}"),
144143
None => path.to_string(),
@@ -155,7 +154,7 @@ fn env_is_set(name: &str) -> bool {
155154
fn env_string_required(name: &str) -> String {
156155
var(name)
157156
.expect(&format!(
158-
"{} env variable is required for proper configration",
157+
"{} env variable is required for proper configuration",
159158
name
160159
))
161160
.to_string()

0 commit comments

Comments
 (0)