Skip to content

Commit 1dbc7a6

Browse files
committed
social image urls
1 parent 52c2372 commit 1dbc7a6

File tree

4 files changed

+76
-21
lines changed

4 files changed

+76
-21
lines changed

pgml-dashboard/src/api/cms.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl Collection {
7979

8080
let path = self.root_dir.join(path.with_extension("md"));
8181

82-
self.render(&path, cluster).await
82+
self.render(&path, cluster, self).await
8383
}
8484

8585
/// Create an index of the Collection based on the SUMMARY.md from Gitbook.
@@ -172,7 +172,7 @@ impl Collection {
172172
Ok(links)
173173
}
174174

175-
async fn render<'a>(&self, path: &'a PathBuf, cluster: &Cluster) -> Result<ResponseOk, Status> {
175+
async fn render<'a>(&self, path: &'a PathBuf, cluster: &Cluster, collection: &Collection) -> Result<ResponseOk, Status> {
176176
// Read to string0
177177
let contents = match tokio::fs::read_to_string(&path).await {
178178
Ok(contents) => {
@@ -185,13 +185,13 @@ impl Collection {
185185
}
186186
};
187187
let parts = contents.split("---").collect::<Vec<&str>>();
188-
let ((image, description), contents) = if parts.len() > 1 {
188+
let (description, contents) = if parts.len() > 1 {
189189
match YamlLoader::load_from_str(parts[1]) {
190190
Ok(meta) => {
191191
if !meta.is_empty() {
192192
let meta = meta[0].clone();
193193
if meta.as_hash().is_none() {
194-
((None, None), contents.to_string())
194+
(None, contents.to_string())
195195
} else {
196196
let description: Option<String> = match meta["description"]
197197
.is_badvalue()
@@ -200,21 +200,16 @@ impl Collection {
200200
false => Some(meta["description"].as_str().unwrap().to_string()),
201201
};
202202

203-
let image: Option<String> = match meta["image"].is_badvalue() {
204-
true => None,
205-
false => Some(meta["image"].as_str().unwrap().to_string()),
206-
};
207-
208-
((image, description), parts[2..].join("---").to_string())
203+
(description, parts[2..].join("---").to_string())
209204
}
210205
} else {
211-
((None, None), contents.to_string())
206+
(None, contents.to_string())
212207
}
213208
}
214-
Err(_) => ((None, None), contents.to_string()),
209+
Err(_) => (None, contents.to_string()),
215210
}
216211
} else {
217-
((None, None), contents.to_string())
212+
(None, contents.to_string())
218213
};
219214

220215
// Parse Markdown
@@ -224,7 +219,7 @@ impl Collection {
224219
// Title of the document is the first (and typically only) <h1>
225220
let title = crate::utils::markdown::get_title(&root).unwrap();
226221
let toc_links = crate::utils::markdown::get_toc(&root).unwrap();
227-
222+
let image = crate::utils::markdown::get_image(&root);
228223
crate::utils::markdown::wrap_tables(&root, &arena).unwrap();
229224

230225
// MkDocs syntax support, e.g. tabs, notes, alerts, etc.
@@ -263,8 +258,11 @@ impl Collection {
263258
};
264259

265260
let mut layout = crate::templates::Layout::new(&title);
266-
if image.is_some() {
267-
layout.image(&image.unwrap());
261+
if let Some(image) = image {
262+
// translate relative url into absolute for head social sharing
263+
let parts = image.split(".gitbook/assets/").collect::<Vec<&str>>();
264+
let image_path = collection.url_root.join(".gitbook/assets").join(parts[1]);
265+
layout.image(config::asset_url(image_path.to_string_lossy()).as_ref());
268266
}
269267
if description.is_some() {
270268
layout.description(&description.unwrap());

pgml-dashboard/src/utils/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn js_url(name: &str) -> String {
145145
asset_url(path.to_string_lossy())
146146
}
147147

148-
fn asset_url(path: Cow<str>) -> String {
148+
pub fn asset_url(path: Cow<str>) -> String {
149149
match &CONFIG.assets_domain {
150150
Some(domain) => format!("https://{domain}{path}"),
151151
None => path.to_string(),

pgml-dashboard/src/utils/markdown.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,17 +537,14 @@ pub fn nest_relative_links(node: &mut markdown::mdast::Node, path: &PathBuf) {
537537
let _ = iter_mut_all(node, &mut |node| {
538538
match node {
539539
markdown::mdast::Node::Link(ref mut link) => {
540-
info!("handling link: {:?}", link);
541540
match Url::parse(&link.url) {
542541
Ok(url) => {
543542
if !url.has_host() {
544-
info!("relative: {:?}", link);
545543
let mut url_path = url.path().to_string();
546544
let url_path_path = Path::new(&url_path);
547545
match url_path_path.extension() {
548546
Some(ext) => {
549547
if ext.to_str() == Some(".md") {
550-
info!("md: {:?}", link);
551548
let base = url_path_path.with_extension("");
552549
url_path = base.into_os_string().into_string().unwrap();
553550
}
@@ -617,6 +614,38 @@ pub fn get_title<'a>(root: &'a AstNode<'a>) -> anyhow::Result<String> {
617614
Ok(title)
618615
}
619616

617+
/// Get the social sharing image of the article.
618+
///
619+
/// # Arguments
620+
///
621+
/// * `root` - The root node of the document tree.
622+
///
623+
pub fn get_image<'a>(root: &'a AstNode<'a>) -> Option<String> {
624+
let re = regex::Regex::new(r#"<img src="([^"]*)" alt="([^"]*)""#).unwrap();
625+
let mut image = None;
626+
iter_nodes(root, &mut |node| {
627+
match &node.data.borrow().value {
628+
&NodeValue::HtmlBlock(ref html) => {
629+
match re.captures(&html.literal) {
630+
Some(c) => {
631+
if &c[2] != "Author" {
632+
image = Some(c[1].to_string());
633+
Ok(false)
634+
} else {
635+
Ok(true)
636+
}
637+
},
638+
None => {
639+
Ok(true)
640+
}
641+
}
642+
}
643+
_ => Ok(true)
644+
}
645+
}).ok()?;
646+
return image;
647+
}
648+
620649
/// Wrap tables in container to allow for x-scroll on overflow.
621650
pub fn wrap_tables<'a>(root: &'a AstNode<'a>, arena: &'a Arena<AstNode<'a>>) -> anyhow::Result<()> {
622651
let _ = iter_nodes(root, &mut |node| {

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.docs {
2-
32
div.results {
43
overflow-x: auto;
54
margin: 24px 24px;
@@ -142,4 +141,33 @@
142141
li:not(.nav-item) {
143142
margin: 0.8rem 0;
144143
}
144+
145+
// Gitbook blog author block
146+
h1 {
147+
+ div:first-of-type[align="left"] {
148+
float: left;
149+
height: 54px;
150+
width: 54px;
151+
display: inline-block;
152+
margin-right: 1rem;
153+
154+
figure {
155+
margin: 0 !important;
156+
157+
img {
158+
margin: 0 !important;
159+
border-radius: 50%;
160+
}
161+
}
162+
163+
+ p {
164+
margin: 0;
165+
}
166+
167+
+ p + p {
168+
margin-bottom: 2rem;
169+
}
170+
}
171+
}
145172
}
173+

0 commit comments

Comments
 (0)