@@ -227,10 +227,12 @@ pub async fn init_tera(templates_dir: &str, theme_templates_dir: &Path) -> Resul
227
227
///
228
228
/// # Arguments
229
229
/// * `path` - The path to the metadata file.
230
+ /// * `rel_path` - Relative path to the metadata file without the build directory prefix.
231
+ /// * `routes_url` - The URL used for routing.
230
232
///
231
233
/// # Returns
232
234
/// * `toml::Value` - The parsed metadata or an empty table if an error occurs.
233
- pub async fn load_metadata ( path : PathBuf ) -> toml:: Value {
235
+ pub async fn load_metadata ( path : PathBuf , rel_path : PathBuf , routes_url : & str ) -> toml:: Value {
234
236
match tokio:: fs:: read_to_string ( & path) . await {
235
237
Ok ( content) => {
236
238
let mut value = toml:: from_str ( & content) . unwrap_or_else ( |e| {
@@ -247,6 +249,37 @@ pub async fn load_metadata(path: PathBuf) -> toml::Value {
247
249
}
248
250
}
249
251
252
+ // Generate permalink from file structure
253
+ // Remove .meta.toml
254
+ let mut permalink_path = rel_path. with_extension ( "" ) . with_extension ( "" ) ;
255
+
256
+ // Handle index pages
257
+ if let Some ( file_name) = permalink_path. file_name ( ) {
258
+ if file_name == "index" {
259
+ permalink_path = permalink_path
260
+ . parent ( )
261
+ . unwrap_or_else ( || Path :: new ( "" ) )
262
+ . to_path_buf ( ) ;
263
+ }
264
+ }
265
+
266
+ // Convert to URL path
267
+ let permalink_str = permalink_path
268
+ . to_string_lossy ( )
269
+ . trim_start_matches ( '/' )
270
+ . to_string ( ) ;
271
+
272
+ let permalink = if permalink_str. is_empty ( ) {
273
+ format ! ( "{}/" , routes_url)
274
+ } else {
275
+ format ! ( "{}/{}/" , routes_url, permalink_str)
276
+ } ;
277
+
278
+ // Add permalink and html content to metadata
279
+ if let toml:: Value :: Table ( ref mut table) = value {
280
+ table. insert ( "permalink" . to_string ( ) , toml:: Value :: String ( permalink) ) ;
281
+ }
282
+
250
283
value
251
284
}
252
285
Err ( e) => {
@@ -280,7 +313,12 @@ pub async fn validate_content_metadata(
280
313
let relative_path = path. strip_prefix ( content_dir) . unwrap ( ) ;
281
314
let meta_path = build_dir. join ( relative_path) . with_extension ( "meta.toml" ) ;
282
315
283
- let metadata = load_metadata ( meta_path) . await ;
316
+ let rel_path = meta_path
317
+ . clone ( )
318
+ . strip_prefix ( build_dir)
319
+ . map ( |p| p. to_path_buf ( ) ) ?;
320
+ // We do not need to do anything with the metadata permalink here so we pass an empty string to it
321
+ let metadata = load_metadata ( meta_path, rel_path, "" ) . await ;
284
322
let metadata_map = metadata
285
323
. as_table ( )
286
324
. unwrap ( )
@@ -325,43 +363,16 @@ pub async fn collect_all_posts_metadata(
325
363
} )
326
364
{
327
365
let meta_path = entry. path ( ) ;
328
- let mut metadata = load_metadata ( entry. path ( ) . to_path_buf ( ) ) . await ;
366
+ let rel_path = meta_path. strip_prefix ( build_dir) ?. to_path_buf ( ) ;
367
+ let mut metadata = load_metadata ( entry. path ( ) . to_path_buf ( ) , rel_path, routes_url) . await ;
329
368
330
369
// TODO: this won't hot reload if the content changes, should be passed as an argument instead
331
370
// Get the raw html content
332
371
let html_file = entry. path ( ) . with_extension ( "" ) . with_extension ( "html" ) ;
333
372
let html = tokio:: fs:: read_to_string ( & html_file) . await ?;
334
373
335
- // Generate permalink from file structure
336
- let rel_path = meta_path. strip_prefix ( build_dir) ?;
337
- // Remove .meta.toml
338
- let mut permalink_path = rel_path. with_extension ( "" ) . with_extension ( "" ) ;
339
-
340
- // Handle index pages
341
- if let Some ( file_name) = permalink_path. file_name ( ) {
342
- if file_name == "index" {
343
- permalink_path = permalink_path
344
- . parent ( )
345
- . unwrap_or_else ( || Path :: new ( "" ) )
346
- . to_path_buf ( ) ;
347
- }
348
- }
349
-
350
- // Convert to URL path
351
- let permalink_str = permalink_path
352
- . to_string_lossy ( )
353
- . trim_start_matches ( '/' )
354
- . to_string ( ) ;
355
-
356
- let permalink = if permalink_str. is_empty ( ) {
357
- format ! ( "{}/" , routes_url)
358
- } else {
359
- format ! ( "{}/{}/" , routes_url, permalink_str)
360
- } ;
361
-
362
- // Add permalink and html content to metadata
374
+ // Add html content to metadata
363
375
if let toml:: Value :: Table ( ref mut table) = metadata {
364
- table. insert ( "permalink" . to_string ( ) , toml:: Value :: String ( permalink) ) ;
365
376
table. insert ( "raw" . to_string ( ) , toml:: Value :: String ( html) ) ;
366
377
}
367
378
posts. push ( metadata) ;
0 commit comments