Skip to content

Commit b98420e

Browse files
committed
Spence section nested under page uses parent slug in url path (#1)
* Make a test that replicates the issue regarding section paths not respecting page parents * Add a very rudimentary working implementation that passes the test * Rewrite solution to be more idiomatic rust
1 parent b883c49 commit b98420e

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

Diff for: components/content/src/section.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::library::Library;
1717
use crate::ser::{SectionSerMode, SerializingSection};
1818
use crate::utils::{find_related_assets, get_reading_analytics, has_anchor};
1919

20+
use crate::Page;
21+
2022
// Default is used to create a default index section if there is no _index.md in the root content directory
2123
#[derive(Clone, Debug, Default, PartialEq, Eq)]
2224
pub struct Section {
@@ -91,7 +93,25 @@ impl Section {
9193
section.word_count = Some(word_count);
9294
section.reading_time = Some(reading_time);
9395

94-
let path = section.file.components.join("/");
96+
let path = {
97+
let mut path_components: Vec<String> = Vec::new();
98+
for (index, file_component) in section.file.components.iter().enumerate() {
99+
let maybe_page_path =
100+
base_path.join("content").join(&section.file.components[0..=index].join("/"));
101+
let maybe_page = Page::from_file(
102+
maybe_page_path.join("index.md").as_path(),
103+
&Config::default(),
104+
base_path,
105+
);
106+
if let Ok(page) = maybe_page {
107+
path_components.push(page.slug);
108+
} else {
109+
path_components.push(file_component.to_string());
110+
}
111+
}
112+
path_components.join("/")
113+
};
114+
95115
let lang_path = if section.lang != config.default_language {
96116
format!("/{}", section.lang)
97117
} else {
@@ -247,6 +267,8 @@ mod tests {
247267
use super::Section;
248268
use config::{Config, LanguageOptions};
249269

270+
use crate::Page;
271+
250272
#[test]
251273
fn section_with_assets_gets_right_info() {
252274
let tmp_dir = tempdir().expect("create temp dir");
@@ -273,6 +295,42 @@ mod tests {
273295
assert_eq!(section.permalink, "http://a-website.com/posts/with-assets/");
274296
}
275297

298+
#[test]
299+
fn should_respect_parent_path_of_a_page() {
300+
// Create a base directory with `/content/posts` subdirectories
301+
let base_dir = tempdir().expect("create temp dir");
302+
let base_dir_path = base_dir.path();
303+
create_dir(&base_dir_path.join("content")).expect("create content temp dir");
304+
create_dir(&base_dir_path.join("content").join("posts")).expect("create posts temp dir");
305+
306+
// Create a 'post' (page) about my vacation, within a directory that has a date as a name
307+
let my_post_path =
308+
base_dir_path.join("content").join("posts").join("2013-06-02_my-vacation");
309+
create_dir(&my_post_path).expect("create dir for post (page) about my vacation");
310+
let mut my_post_file = File::create(my_post_path.join("index.md")).unwrap();
311+
my_post_file.write_all(b"+++\n\n+++\n").unwrap();
312+
let my_post_page = Page::from_file(
313+
my_post_path.join("index.md").as_path(),
314+
&Config::default(),
315+
base_dir_path,
316+
);
317+
assert!(my_post_page.is_ok());
318+
let my_post_page = my_post_page.unwrap(); // shadow the variable after confirming ok
319+
320+
// Create a subdirectory named 'comments' (section) within the post about my vacation
321+
let comments_path = my_post_path.join("comments");
322+
create_dir(&comments_path).unwrap();
323+
let mut comments_section_file = File::create(comments_path.join("_index.md")).unwrap();
324+
comments_section_file.write_all(b"+++\n\n+++\n").unwrap();
325+
let comments_section =
326+
Section::from_file(comments_path.join("_index.md"), &Config::default(), base_dir_path);
327+
assert!(comments_section.is_ok());
328+
let comments_section = comments_section.unwrap(); // shadow the variable after confirming ok
329+
330+
// Check that that section URL path was created successfully
331+
assert_eq!(comments_section.permalink, "http://a-website.com/posts/my-vacation/comments/");
332+
}
333+
276334
#[test]
277335
fn section_with_ignored_assets_filters_out_correct_files() {
278336
let tmp_dir = tempdir().expect("create temp dir");

0 commit comments

Comments
 (0)