@@ -17,6 +17,8 @@ use crate::library::Library;
1717use crate :: ser:: { SectionSerMode , SerializingSection } ;
1818use 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 ) ]
2224pub struct Section {
@@ -91,7 +93,27 @@ 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 component_range_idx = 0 ;
98+ let mut path_components: Vec < String > = Vec :: new ( ) ;
99+ for file_component in section. file . components . clone ( ) {
100+ component_range_idx += 1 ;
101+ let maybe_page_path = base_path
102+ . join ( "content" )
103+ . join ( section. file . components [ 0 ..component_range_idx] . join ( "/" ) ) ;
104+ let maybe_page = Page :: from_file (
105+ maybe_page_path. join ( "index.md" ) . as_path ( ) ,
106+ & Config :: default ( ) ,
107+ base_path,
108+ ) ;
109+ if maybe_page. is_ok ( ) {
110+ path_components. push ( maybe_page. unwrap ( ) . slug ) ;
111+ } else {
112+ path_components. push ( file_component) ;
113+ }
114+ }
115+ path_components. join ( "/" )
116+ } ) ( ) ;
95117 let lang_path = if section. lang != config. default_language {
96118 format ! ( "/{}" , section. lang)
97119 } else {
@@ -247,6 +269,8 @@ mod tests {
247269 use super :: Section ;
248270 use config:: { Config , LanguageOptions } ;
249271
272+ use crate :: Page ;
273+
250274 #[ test]
251275 fn section_with_assets_gets_right_info ( ) {
252276 let tmp_dir = tempdir ( ) . expect ( "create temp dir" ) ;
@@ -273,6 +297,42 @@ mod tests {
273297 assert_eq ! ( section. permalink, "http://a-website.com/posts/with-assets/" ) ;
274298 }
275299
300+ #[ test]
301+ fn should_respect_parent_path_of_a_page ( ) {
302+ // Create a base directory with `/content/posts` subdirectories
303+ let base_dir = tempdir ( ) . expect ( "create temp dir" ) ;
304+ let base_dir_path = base_dir. path ( ) ;
305+ create_dir ( & base_dir_path. join ( "content" ) ) . expect ( "create content temp dir" ) ;
306+ create_dir ( & base_dir_path. join ( "content" ) . join ( "posts" ) ) . expect ( "create posts temp dir" ) ;
307+
308+ // Create a 'post' (page) about my vacation, within a directory that has a date as a name
309+ let my_post_path =
310+ base_dir_path. join ( "content" ) . join ( "posts" ) . join ( "2013-06-02_my-vacation" ) ;
311+ create_dir ( & my_post_path) . expect ( "create dir for post (page) about my vacation" ) ;
312+ let mut my_post_file = File :: create ( my_post_path. join ( "index.md" ) ) . unwrap ( ) ;
313+ my_post_file. write_all ( b"+++\n \n +++\n " ) . unwrap ( ) ;
314+ let my_post_page = Page :: from_file (
315+ my_post_path. join ( "index.md" ) . as_path ( ) ,
316+ & Config :: default ( ) ,
317+ base_dir_path,
318+ ) ;
319+ assert ! ( my_post_page. is_ok( ) ) ;
320+ let my_post_page = my_post_page. unwrap ( ) ; // shadow the variable after confirming ok
321+
322+ // Create a subdirectory named 'comments' (section) within the post about my vacation
323+ let comments_path = my_post_path. join ( "comments" ) ;
324+ create_dir ( & comments_path) . unwrap ( ) ;
325+ let mut comments_section_file = File :: create ( comments_path. join ( "_index.md" ) ) . unwrap ( ) ;
326+ comments_section_file. write_all ( b"+++\n \n +++\n " ) . unwrap ( ) ;
327+ let comments_section =
328+ Section :: from_file ( comments_path. join ( "_index.md" ) , & Config :: default ( ) , base_dir_path) ;
329+ assert ! ( comments_section. is_ok( ) ) ;
330+ let comments_section = comments_section. unwrap ( ) ; // shadow the variable after confirming ok
331+
332+ // Check that that section URL path was created successfully
333+ assert_eq ! ( comments_section. permalink, "http://a-website.com/posts/my-vacation/comments/" ) ;
334+ }
335+
276336 #[ test]
277337 fn section_with_ignored_assets_filters_out_correct_files ( ) {
278338 let tmp_dir = tempdir ( ) . expect ( "create temp dir" ) ;
0 commit comments