Skip to content

Commit 550bbb8

Browse files
authored
Merge pull request #1238 from epage/page.tag
fix: Always include page.tags
2 parents 17f7b54 + 1e6db6b commit 550bbb8

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

src/cobalt_model/frontmatter.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Frontmatter {
1717
pub description: Option<liquid::model::KString>,
1818
pub excerpt: Option<liquid::model::KString>,
1919
pub categories: Vec<liquid::model::KString>,
20-
pub tags: Option<Vec<liquid::model::KString>>,
20+
pub tags: Vec<liquid::model::KString>,
2121
pub excerpt_separator: liquid::model::KString,
2222
pub published_date: Option<DateTime>,
2323
pub format: SourceFormat,
@@ -56,16 +56,11 @@ impl Frontmatter {
5656

5757
let permalink = permalink.unwrap_or_default();
5858

59-
if let Some(ref tags) = tags {
59+
if let Some(tags) = &tags {
6060
if tags.iter().any(|x| x.trim().is_empty()) {
6161
anyhow::bail!("Empty strings are not allowed in tags");
6262
}
6363
}
64-
let tags = if tags.as_ref().map(|t| t.len()).unwrap_or(0) == 0 {
65-
None
66-
} else {
67-
tags
68-
};
6964
let fm = Frontmatter {
7065
pagination: pagination
7166
.and_then(|p| pagination::PaginationConfig::from_config(p, &permalink)),
@@ -75,7 +70,7 @@ impl Frontmatter {
7570
description,
7671
excerpt,
7772
categories: categories.unwrap_or_default(),
78-
tags,
73+
tags: tags.unwrap_or_default(),
7974
excerpt_separator: excerpt_separator.unwrap_or_else(|| "\n\n".into()),
8075
published_date,
8176
format: format.unwrap_or_default(),

src/document.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ impl Document {
9292
pub(crate) fn to_jsonfeed(&self, root_url: &str) -> jsonfeed::Item {
9393
let link = format!("{}/{}", root_url, &self.url_path);
9494

95+
let tags = if !self.front.tags.is_empty() {
96+
self.front
97+
.tags
98+
.iter()
99+
.map(|s| s.as_str().to_owned())
100+
.collect()
101+
} else {
102+
self.front
103+
.categories
104+
.iter()
105+
.map(|s| s.as_str().to_owned())
106+
.collect()
107+
};
108+
95109
jsonfeed::Item {
96110
id: link.clone(),
97111
url: Some(link),
@@ -100,19 +114,7 @@ impl Document {
100114
self.description_to_str().unwrap_or_else(|| "".into()),
101115
),
102116
date_published: self.front.published_date.map(|date| date.to_rfc2822()),
103-
tags: Some(
104-
self.front
105-
.tags
106-
.as_ref()
107-
.map(|tags| tags.iter().map(|s| s.as_str().to_owned()).collect())
108-
.unwrap_or_else(|| {
109-
self.front
110-
.categories
111-
.iter()
112-
.map(|s| s.as_str().to_owned())
113-
.collect()
114-
}),
115-
),
117+
tags: Some(tags),
116118
..Default::default()
117119
}
118120
}
@@ -328,10 +330,11 @@ fn document_attributes(
328330
.map(Value::scalar)
329331
.collect(),
330332
);
333+
let tags = Value::Array(front.tags.iter().cloned().map(Value::scalar).collect());
331334
// Reason for `file`:
332335
// - Allow access to assets in the original location
333336
// - Ease linking back to page's source
334-
let file: Object = vec![
337+
let file: Object = [
335338
(
336339
"permalink".into(),
337340
Value::scalar(source_file.as_str().to_owned()),
@@ -349,7 +352,7 @@ fn document_attributes(
349352
]
350353
.into_iter()
351354
.collect();
352-
let attributes = vec![
355+
let attributes = [
353356
("permalink".into(), Value::scalar(url_path.to_owned())),
354357
("title".into(), Value::scalar(front.title.clone())),
355358
("slug".into(), Value::scalar(front.slug.clone())),
@@ -358,6 +361,7 @@ fn document_attributes(
358361
Value::scalar(front.description.as_deref().unwrap_or("").to_owned()),
359362
),
360363
("categories".into(), categories),
364+
("tags".into(), tags),
361365
("is_draft".into(), Value::scalar(front.is_draft)),
362366
("weight".into(), Value::scalar(front.weight)),
363367
("file".into(), Value::Object(file)),
@@ -366,11 +370,6 @@ fn document_attributes(
366370
];
367371
let mut attributes: Object = attributes.into_iter().collect();
368372

369-
if let Some(ref tags) = front.tags {
370-
let tags = Value::Array(tags.iter().cloned().map(Value::scalar).collect());
371-
attributes.insert("tags".into(), tags);
372-
}
373-
374373
if let Some(ref published_date) = front.published_date {
375374
attributes.insert("published_date".into(), Value::scalar(*published_date));
376375
}

0 commit comments

Comments
 (0)