@@ -17,6 +17,8 @@ use crate::library::Library;
17
17
use crate :: ser:: { SectionSerMode , SerializingSection } ;
18
18
use crate :: utils:: { find_related_assets, get_reading_analytics, has_anchor} ;
19
19
20
+ use crate :: Page ;
21
+
20
22
// Default is used to create a default index section if there is no _index.md in the root content directory
21
23
#[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
22
24
pub struct Section {
@@ -91,7 +93,25 @@ impl Section {
91
93
section. word_count = Some ( word_count) ;
92
94
section. reading_time = Some ( reading_time) ;
93
95
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
+
95
115
let lang_path = if section. lang != config. default_language {
96
116
format ! ( "/{}" , section. lang)
97
117
} else {
@@ -247,6 +267,8 @@ mod tests {
247
267
use super :: Section ;
248
268
use config:: { Config , LanguageOptions } ;
249
269
270
+ use crate :: Page ;
271
+
250
272
#[ test]
251
273
fn section_with_assets_gets_right_info ( ) {
252
274
let tmp_dir = tempdir ( ) . expect ( "create temp dir" ) ;
@@ -273,6 +295,42 @@ mod tests {
273
295
assert_eq ! ( section. permalink, "http://a-website.com/posts/with-assets/" ) ;
274
296
}
275
297
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
+
276
334
#[ test]
277
335
fn section_with_ignored_assets_filters_out_correct_files ( ) {
278
336
let tmp_dir = tempdir ( ) . expect ( "create temp dir" ) ;
0 commit comments