marmite 0.4.2

Marmite is the easiest static site generator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
use crate::cli::Cli;
use crate::config::Marmite;
use crate::highlight::MarmiteHighlighter;
use crate::image_provider;
use crate::parser::{
    append_references, get_html_with_options, get_links_to, get_media_links_to,
    get_table_of_contents_from_html, parse_front_matter,
};
use crate::re;
use crate::site::{get_content_folder, Data};
use chrono::{NaiveDate, NaiveDateTime};
use frontmatter_gen::{Frontmatter, Value};
use log::error;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::Write;
use std::path::Path;
use std::process;
use std::sync::Arc;

#[derive(Debug, Clone, Serialize)]
pub enum Kind {
    Tag,
    Archive,
    Author,
    Stream,
    Series,
    Language,
}

#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Serialize)]
pub struct GroupedContent {
    pub kind: Kind,
    pub map: HashMap<String, Vec<Content>>,
}

impl GroupedContent {
    pub fn new(kind: Kind) -> Self {
        Self {
            kind,
            map: HashMap::new(),
        }
    }

    pub fn entry(&mut self, key: String) -> Entry<'_, String, Vec<Content>> {
        self.map.entry(key)
    }

    pub fn sort_all(&mut self) {
        for contents in self.map.values_mut() {
            match self.kind {
                Kind::Series => {
                    // Series should be sorted chronologically (oldest to newest)
                    contents.sort_by_key(|a| a.date);
                }
                _ => {
                    // All other content types sort newest first
                    contents.sort_by_key(|a| std::cmp::Reverse(a.date));
                }
            }
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = (&String, Vec<Content>)> {
        // Assuming the content is already sorted by date on a previous call of .sort_all
        let mut vec: Vec<(&String, Vec<Content>)> =
            self.map.iter().map(|(k, v)| (k, v.clone())).collect();

        match self.kind {
            Kind::Tag => {
                // sort by number of contents
                vec.sort_by_key(|a| std::cmp::Reverse(a.1.len()));
            }
            Kind::Archive => {
                // sort by year, newest first
                vec.sort_by(|a, b| b.0.cmp(a.0));
            }
            Kind::Author | Kind::Stream | Kind::Series | Kind::Language => {
                // sort alphabetically
                vec.sort_by(|a, b| a.0.cmp(b.0));
            }
        }
        vec.into_iter()
    }
}

#[derive(Debug, Deserialize, Clone, Serialize, Default)]
pub struct TranslationRef {
    pub lang: String,
    pub name: String,
    pub slug: String,
    pub title: String,
}

#[derive(Debug, Deserialize, Clone, Serialize, Default)]
pub struct Content {
    pub title: String,
    pub description: Option<String>,
    pub slug: String,
    pub html: String,
    pub tags: Vec<String>,
    pub date: Option<NaiveDateTime>,
    pub extra: Option<Value>,
    pub links_to: Option<Vec<String>>,
    pub media_links_to: Option<Vec<String>>,
    pub back_links: Vec<Self>,
    pub card_image: Option<String>,
    pub banner_image: Option<String>,
    pub authors: Vec<String>,
    pub stream: Option<String>,
    pub series: Option<String>,
    pub pinned: bool,
    pub toc: Option<String>,
    pub modified_time: Option<i64>,
    pub comments: Option<bool>,
    pub next: Option<Box<Content>>,
    pub previous: Option<Box<Content>>,
    pub source_path: Option<std::path::PathBuf>,
    pub at_uri: Option<String>,
    pub aliases: Vec<String>,
    pub language: Option<String>,
    pub translations: Vec<TranslationRef>,
    pub translates: Option<String>,
}

impl Content {
    /// From the file content, extract the frontmatter and the markdown content
    /// then parse the markdown content to html and return a Content struct
    /// if the file is a fragment, the markdown content will be modified to include the references
    /// if is a regular content then content will be modified to include the `markdown_header`
    /// and `markdown_footer` and references
    #[allow(clippy::too_many_lines)]
    pub fn from_markdown(
        path: &Path,
        fragments: Option<&HashMap<String, String>>,
        site: &Marmite,
        modified_time: Option<i64>,
        highlighter: Option<&MarmiteHighlighter>,
        folder_defaults: Option<&Frontmatter>,
        content_dir: Option<&Path>,
    ) -> Result<Content, String> {
        let file_content = fs::read_to_string(path).map_err(|e| e.to_string())?;
        let (mut frontmatter, raw_markdown) = parse_front_matter(&file_content)?;

        let page_mermaid_config: Option<serde_yaml::Value> = frontmatter
            .remove("mermaid_config")
            .and_then(|v| serde_yaml::to_value(&v).ok());
        let folder_mermaid_config: Option<serde_yaml::Value> = folder_defaults
            .and_then(|fd| fd.get("mermaid_config"))
            .and_then(|v| serde_yaml::to_value(v).ok());

        if let Some(defaults) = folder_defaults {
            merge_frontmatter(defaults, &mut frontmatter);
        }

        let merged_mermaid_config = merge_mermaid_configs(
            site.mermaid_config.as_ref(),
            folder_mermaid_config.as_ref(),
            page_mermaid_config.as_ref(),
        );
        let (title, markdown_without_title) = get_title(&frontmatter, raw_markdown);
        let slug = get_slug(&frontmatter, path);

        let is_fragment = path
            .file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|s| s.starts_with('_'));

        let default_parser_options = crate::config::ParserOptions::default();
        let parser_options = site
            .markdown_parser
            .as_ref()
            .unwrap_or(&default_parser_options);
        let html = if is_fragment {
            let references_path = path.with_file_name("_references.md");
            let mut raw_markdown = raw_markdown.to_string();
            if path != references_path {
                raw_markdown = append_references(&raw_markdown, &references_path);
            }
            get_html_with_options(&raw_markdown, parser_options, highlighter)
        } else if fragments.is_some() {
            let mut markdown_without_title = markdown_without_title.clone();
            if let Some(header) = fragments.and_then(|f| f.get("markdown_header")) {
                markdown_without_title.insert_str(0, format!("{header}\n\n").as_str());
            }
            if let Some(footer) = fragments.and_then(|f| f.get("markdown_footer")) {
                markdown_without_title.push_str(format!("\n\n{footer}").as_str());
            }
            if let Some(references) = fragments.and_then(|f| f.get("references")) {
                markdown_without_title.push_str(format!("\n\n{references}").as_str());
            }
            get_html_with_options(&markdown_without_title, parser_options, highlighter)
        } else {
            get_html_with_options(&markdown_without_title, parser_options, highlighter)
        };

        let html = if is_fragment {
            html
        } else {
            fix_at_media_refs(&html, &site.media_path, &slug, path, content_dir)
        };

        let html = if site.native_mermaid_render && !is_fragment {
            crate::parser::render_native_mermaid(&html, &slug, merged_mermaid_config.as_ref())
        } else {
            html
        };

        let description = get_description(&frontmatter);
        let tags = get_tags(&frontmatter);
        let date = get_date(&frontmatter, path);
        let extra = frontmatter.get("extra").map(std::borrow::ToOwned::to_owned);
        let links_to = get_links_to(&html);
        let media_links_to = get_media_links_to(&html);
        let back_links = Vec::new(); // will be mutated later

        // Download banner image if image provider is configured and this is a post (has date)
        if date.is_some() {
            let media_root = content_dir.or(path.parent()).unwrap_or(path);
            let _ =
                image_provider::download_banner_image(site, &frontmatter, media_root, &slug, &tags);
        }

        let card_image = get_card_image(
            &frontmatter,
            &html,
            path,
            &slug,
            &site.media_path,
            content_dir,
        );
        let banner_image =
            get_banner_image(&frontmatter, path, &slug, &site.media_path, content_dir);
        let authors = get_authors(&frontmatter, Some(site.default_author.clone()));
        let pinned = frontmatter
            .get("pinned")
            .is_some_and(|p| p.as_bool().unwrap_or(false));

        let toc = if frontmatter
            .get("toc")
            .map_or(site.toc, |t| t.as_bool().unwrap_or(site.toc))
        {
            Some(get_table_of_contents_from_html(&html))
        } else {
            None
        };

        let stream = if date.is_some() {
            // For posts with dates, determine stream from frontmatter or filename patterns
            Some(determine_stream(&frontmatter, path))
        } else {
            // For pages without dates, stream is None (pages don't have streams)
            None
        };

        let series = determine_series(&frontmatter);

        let comments = get_comments(&frontmatter);
        let aliases = get_aliases(&frontmatter);
        let language = get_language(&frontmatter);
        let translates = get_translates(&frontmatter);
        let frontmatter_translations = get_frontmatter_translations(&frontmatter);
        let content = Content {
            title,
            description,
            slug,
            html,
            tags,
            date,
            extra,
            links_to,
            media_links_to,
            back_links,
            card_image,
            banner_image,
            authors,
            stream,
            series,
            pinned,
            toc,
            modified_time,
            comments,
            next: None,
            previous: None,
            source_path: Some(path.to_path_buf()),
            at_uri: None,
            aliases,
            language,
            translations: frontmatter_translations,
            translates,
        };
        Ok(content)
    }
}

#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Default)]
pub struct ContentBuilder {
    title: Option<String>,
    description: Option<String>,
    slug: Option<String>,
    html: Option<String>,
    tags: Option<Vec<String>>,
    date: Option<NaiveDateTime>,
    extra: Option<Value>,
    links_to: Option<Vec<String>>,
    media_links_to: Option<Vec<String>>,
    back_links: Option<Vec<Content>>,
    card_image: Option<String>,
    banner_image: Option<String>,
    authors: Option<Vec<String>>,
    stream: Option<String>,
    series: Option<String>,
    pinned: Option<bool>,
    toc: Option<String>,
    comments: Option<bool>,
    source_path: Option<std::path::PathBuf>,
    at_uri: Option<String>,
    aliases: Option<Vec<String>>,
    language: Option<String>,
    translations: Option<Vec<TranslationRef>>,
    translates: Option<String>,
}

#[allow(dead_code)]
impl ContentBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn title(mut self, title: String) -> Self {
        self.title = Some(title);
        self
    }

    pub fn description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    pub fn slug(mut self, slug: String) -> Self {
        self.slug = Some(slug);
        self
    }

    pub fn html(mut self, html: String) -> Self {
        self.html = Some(html);
        self
    }

    pub fn tags(mut self, tags: Vec<String>) -> Self {
        self.tags = Some(tags);
        self
    }

    pub fn date(mut self, date: NaiveDateTime) -> Self {
        self.date = Some(date);
        self
    }

    pub fn extra(mut self, extra: Value) -> Self {
        self.extra = Some(extra);
        self
    }

    pub fn links_to(mut self, links_to: Vec<String>) -> Self {
        self.links_to = Some(links_to);
        self
    }

    pub fn media_links_to(mut self, media_links_to: Vec<String>) -> Self {
        self.media_links_to = Some(media_links_to);
        self
    }

    pub fn back_links(mut self, back_links: Vec<Content>) -> Self {
        self.back_links = Some(back_links);
        self
    }

    pub fn card_image(mut self, card_image: String) -> Self {
        self.card_image = Some(card_image);
        self
    }

    pub fn banner_image(mut self, banner_image: String) -> Self {
        self.banner_image = Some(banner_image);
        self
    }

    pub fn authors(mut self, authors: Vec<String>) -> Self {
        self.authors = Some(authors);
        self
    }

    pub fn stream(mut self, stream: String) -> Self {
        self.stream = Some(stream);
        self
    }

    pub fn series(mut self, series: String) -> Self {
        self.series = Some(series);
        self
    }

    pub fn pinned(mut self, pinned: bool) -> Self {
        self.pinned = Some(pinned);
        self
    }

    pub fn toc(mut self, toc: String) -> Self {
        self.toc = Some(toc);
        self
    }

    pub fn comments(mut self, comments: bool) -> Self {
        self.comments = Some(comments);
        self
    }

    pub fn source_path(mut self, source_path: std::path::PathBuf) -> Self {
        self.source_path = Some(source_path);
        self
    }

    pub fn at_uri(mut self, at_uri: String) -> Self {
        self.at_uri = Some(at_uri);
        self
    }

    pub fn aliases(mut self, aliases: Vec<String>) -> Self {
        self.aliases = Some(aliases);
        self
    }

    pub fn language(mut self, language: String) -> Self {
        self.language = Some(language);
        self
    }

    pub fn translations(mut self, translations: Vec<TranslationRef>) -> Self {
        self.translations = Some(translations);
        self
    }

    pub fn translates(mut self, translates: String) -> Self {
        self.translates = Some(translates);
        self
    }

    pub fn build(self) -> Content {
        Content {
            title: self.title.unwrap_or_default(),
            description: self.description,
            slug: self.slug.unwrap_or_default(),
            html: self.html.unwrap_or_default(),
            tags: self.tags.unwrap_or_default(),
            date: self.date,
            extra: self.extra,
            links_to: self.links_to,
            media_links_to: self.media_links_to,
            back_links: self.back_links.unwrap_or_default(),
            card_image: self.card_image,
            banner_image: self.banner_image,
            authors: self.authors.unwrap_or_default(),
            stream: self.stream,
            series: self.series,
            pinned: self.pinned.unwrap_or_default(),
            toc: self.toc,
            modified_time: None,
            comments: self.comments,
            next: None,
            previous: None,
            source_path: self.source_path,
            at_uri: self.at_uri,
            aliases: self.aliases.unwrap_or_default(),
            language: self.language,
            translations: self.translations.unwrap_or_default(),
            translates: self.translates,
        }
    }
}

/// Try to get the title from the frontmatter
/// If not found, get the first line of the markdown without the leading '#'
/// If no lines are found, return an empty string
/// return (title, markdown without title)
pub fn get_title<'a>(frontmatter: &'a Frontmatter, markdown: &'a str) -> (String, String) {
    let title = match frontmatter.get("title") {
        Some(Value::String(t)) => t.clone(),
        _ => markdown
            .lines()
            .find(|line| !line.trim().is_empty() && !line.trim().starts_with("<!"))
            .unwrap_or("")
            .trim_start_matches('#')
            .trim()
            .to_string(),
    };
    let markdown = markdown
        .lines()
        .skip_while(|line| line.trim().is_empty() || line.trim_start_matches('#').trim() == title)
        .collect::<Vec<&str>>()
        .join("\n");
    (title, markdown)
}

pub fn get_description(frontmatter: &Frontmatter) -> Option<String> {
    if let Some(description) = frontmatter.get("description") {
        return Some(description.to_string());
    }
    None
}

pub fn get_comments(frontmatter: &Frontmatter) -> Option<bool> {
    if let Some(comments) = frontmatter.get("comments") {
        return comments.as_bool();
    }
    None
}

/// Try to get the slug from the frontmatter
/// If not found, get the title from the frontmatter
/// If not found, get the filename without the date and stream prefix
/// If a stream is not the default `index`, prepend it to the slug
/// return the slug
pub fn get_slug<'a>(frontmatter: &'a Frontmatter, path: &'a Path) -> String {
    let stream = determine_stream(frontmatter, path);
    let mut final_slug: String;

    if let Some(slug) = frontmatter.get("slug") {
        final_slug = crate::slugify::slugify(slug.to_string());
    } else if let Some(title) = frontmatter.get("title") {
        final_slug = crate::slugify::slugify(title.to_string());
    } else {
        final_slug = path
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or("untitled")
            .to_string();
        final_slug = remove_stream_and_date_from_filename(&final_slug);
    }

    if stream != "index" {
        final_slug = format!("{stream}-{final_slug}");
    }

    final_slug
}

/// Determine stream from frontmatter or filename
fn determine_stream(frontmatter: &Frontmatter, path: &Path) -> String {
    // First try frontmatter
    if let Some(stream) = frontmatter.get("stream") {
        return stream
            .as_str()
            .unwrap_or("index")
            .trim_matches('"')
            .to_string();
    }

    // Then try filename patterns
    if let Some(filename_stream) = get_stream_from_filename(path) {
        return filename_stream;
    }

    // Default
    "index".to_string()
}

/// Determine series from frontmatter
fn determine_series(frontmatter: &Frontmatter) -> Option<String> {
    frontmatter
        .get("series")
        .and_then(|s| s.as_str().map(|s| s.trim_matches('"').to_string()))
}

// Remove date prefix from filename `2024-01-01-myfile.md` -> `myfile.md`
// Return filename if no date prefix is found
pub fn remove_date_from_filename(filename: &str) -> String {
    let date_prefix_re =
        Regex::new(re::MATCH_DATE_PREFIX_FROM_FILENAME).expect("Date prefix regex should compile");
    date_prefix_re.replace(filename, "").to_string()
}

// Remove stream and date prefix from filename
// Handles patterns: `stream-2024-01-01-myfile.md` -> `myfile` and `stream-S-myfile.md` -> `myfile`
fn remove_stream_and_date_from_filename(filename: &str) -> String {
    // Pattern 1: stream-date-slug -> slug (handle time components too)
    let stream_date_pattern = Regex::new(re::CAPTURE_SLUG_FROM_STREAM_DATED_FILENAME)
        .expect("Stream date pattern regex should compile");
    if let Some(captures) = stream_date_pattern.captures(filename) {
        if let Some(slug_match) = captures.get(1) {
            return slug_match.as_str().to_string();
        }
    }

    // Pattern 2: stream-S-slug -> slug
    let stream_s_pattern = Regex::new(re::CAPTURE_SLUG_FROM_STREAM_S_FILENAME)
        .expect("Stream S pattern regex should compile");
    if let Some(captures) = stream_s_pattern.captures(filename) {
        if let Some(slug_match) = captures.get(1) {
            return slug_match.as_str().to_string();
        }
    }

    // Fallback: remove date prefix only
    remove_date_from_filename(filename)
}

/// Extract stream from filename using patterns
/// Returns None if no stream pattern is detected
pub fn get_stream_from_filename(path: &Path) -> Option<String> {
    if let Some(filename) = path.file_stem().and_then(|stem| stem.to_str()) {
        // Pattern 1: {stream}-{date}-{slug} (single word before date)
        if let Some(stream) = extract_stream_from_date_pattern(filename) {
            return Some(stream);
        }

        // Pattern 2: {stream}-S-{slug} (single word before 'S' marker)
        if let Some(stream) = extract_stream_from_s_pattern(filename) {
            return Some(stream);
        }
    }
    None
}

pub const ISO_639_1_CODES: &[&str] = &[
    "aa", "ab", "af", "ak", "am", "an", "ar", "as", "av", "ay", "az", "ba", "be", "bg", "bh", "bi",
    "bm", "bn", "bo", "br", "bs", "ca", "ce", "ch", "co", "cr", "cs", "cu", "cv", "cy", "da", "de",
    "dv", "dz", "ee", "el", "en", "eo", "es", "et", "eu", "fa", "ff", "fi", "fj", "fo", "fr", "fy",
    "ga", "gd", "gl", "gn", "gu", "gv", "ha", "he", "hi", "ho", "hr", "ht", "hu", "hy", "hz", "ia",
    "id", "ie", "ig", "ii", "ik", "io", "is", "it", "iu", "ja", "jv", "ka", "kg", "ki", "kj", "kk",
    "kl", "km", "kn", "ko", "kr", "ks", "ku", "kv", "kw", "ky", "la", "lb", "lg", "li", "ln", "lo",
    "lt", "lu", "lv", "mg", "mh", "mi", "mk", "ml", "mn", "mr", "ms", "mt", "my", "na", "nb", "nd",
    "ne", "ng", "nl", "nn", "no", "nr", "nv", "ny", "oc", "oj", "om", "or", "os", "pa", "pi", "pl",
    "ps", "pt", "qu", "rm", "rn", "ro", "ru", "rw", "sa", "sc", "sd", "se", "sg", "si", "sk", "sl",
    "sm", "sn", "so", "sq", "sr", "ss", "st", "su", "sv", "sw", "ta", "te", "tg", "th", "ti", "tk",
    "tl", "tn", "to", "tr", "ts", "tt", "tw", "ty", "ug", "uk", "ur", "uz", "ve", "vi", "vo", "wa",
    "wo", "xh", "yi", "yo", "za", "zh", "zu",
];

pub fn is_iso_639_1_code(code: &str) -> bool {
    ISO_639_1_CODES.contains(&code)
}

/// Detect language from a file in a content subfolder.
/// Only applies when the file is inside a subfolder AND the filename
/// starts with an ISO 639-1 language code followed by a hyphen.
/// Returns the language code if detected.
pub fn detect_language_from_path(path: &Path, content_dir: &Path) -> Option<String> {
    let relative = path.strip_prefix(content_dir).ok()?;
    let components: Vec<_> = relative.components().collect();

    if components.len() < 2 {
        return None;
    }

    let filename_stem = path.file_stem()?.to_str()?;

    for &lang_code in ISO_639_1_CODES {
        let prefix = format!("{lang_code}-");
        if filename_stem.starts_with(&prefix) {
            return Some(lang_code.to_string());
        }
    }

    None
}

/// Extract stream from filename pattern: {stream}-{date}-{slug}
/// Only accepts single word before date (no hyphens allowed in stream name)
fn extract_stream_from_date_pattern(filename: &str) -> Option<String> {
    let date_pattern = Regex::new(re::CAPTURE_STREAM_AND_DATE_FROM_FILENAME)
        .expect("Date pattern regex should compile");
    if let Some(captures) = date_pattern.captures(filename) {
        if let Some(stream_match) = captures.get(1) {
            return Some(stream_match.as_str().to_string());
        }
    }
    None
}

/// Extract stream from filename pattern: {stream}-S-{slug}
/// Only accepts single word before 'S' marker
fn extract_stream_from_s_pattern(filename: &str) -> Option<String> {
    let s_pattern =
        Regex::new(re::CAPTURE_STREAM_FROM_S_FILENAME).expect("S pattern regex should compile");
    if let Some(captures) = s_pattern.captures(filename) {
        if let Some(stream_match) = captures.get(1) {
            return Some(stream_match.as_str().to_string());
        }
    }
    None
}

pub fn get_tags(frontmatter: &Frontmatter) -> Vec<String> {
    let tags: Vec<String> = match frontmatter.get("tags") {
        Some(Value::Array(tags)) => tags
            .iter()
            .map(Value::to_string)
            .map(|t| t.trim_matches('"').to_string())
            .collect(),
        Some(Value::String(tags)) => tags.split(',').map(str::trim).map(String::from).collect(),
        _ => Vec::new(),
    };

    // Remove empty tags but keep original names
    tags.iter()
        .filter(|tag| !tag.is_empty())
        .map(|t| t.trim().to_string())
        .collect()
}

pub fn get_authors(frontmatter: &Frontmatter, default_author: Option<String>) -> Vec<String> {
    let mut authors: Vec<String> = match frontmatter.get("authors") {
        Some(Value::Array(authors)) => authors
            .iter()
            .map(Value::to_string)
            .map(|t| t.trim_matches('"').to_string())
            .collect(),
        Some(Value::String(authors)) => authors
            .split(',')
            .map(str::trim)
            .map(String::from)
            .collect(),
        _ => Vec::new(),
    };
    // If authors is empty, try to get single author from frontmatter
    if authors.is_empty() {
        authors = match frontmatter.get("author") {
            Some(Value::Array(authors)) => authors
                .iter()
                .map(Value::to_string)
                .map(|t| t.trim_matches('"').to_string())
                .collect(),
            Some(Value::String(authors)) => authors
                .split(',')
                .map(str::trim)
                .map(String::from)
                .collect(),
            _ => Vec::new(),
        };
    }
    if authors.is_empty() {
        if let Some(default_author) = default_author {
            if !default_author.is_empty() {
                authors.push(default_author);
            }
        }
    }
    authors
}

pub fn get_aliases(frontmatter: &Frontmatter) -> Vec<String> {
    let aliases: Vec<String> = match frontmatter.get("aliases") {
        Some(Value::Array(aliases)) => aliases
            .iter()
            .map(Value::to_string)
            .map(|t| t.trim_matches('"').to_string())
            .collect(),
        Some(Value::String(aliases)) => aliases
            .split(',')
            .map(str::trim)
            .map(String::from)
            .collect(),
        _ => Vec::new(),
    };
    aliases
        .iter()
        .filter(|alias| !alias.is_empty())
        .map(|a| a.trim().to_string())
        .collect()
}

pub fn merge_frontmatter(defaults: &Frontmatter, file_fm: &mut Frontmatter) {
    for (key, value) in defaults.iter() {
        if key == "title" || key == "slug" {
            continue;
        }
        if !file_fm.contains_key(key) {
            file_fm.insert(key.clone(), value.clone());
        }
    }
}

fn merge_mermaid_configs(
    site: Option<&serde_yaml::Value>,
    folder: Option<&serde_yaml::Value>,
    page: Option<&serde_yaml::Value>,
) -> Option<serde_yaml::Value> {
    let mut result: Option<serde_yaml::Value> = None;
    for overlay in [site, folder, page].into_iter().flatten() {
        result = Some(match result {
            Some(base) => crate::workspace::deep_merge_yaml(base, overlay.clone()),
            None => overlay.clone(),
        });
    }
    result
}

fn get_language(frontmatter: &Frontmatter) -> Option<String> {
    frontmatter
        .get("language")
        .and_then(|v| v.as_str())
        .map(|s| s.trim_matches('"').to_string())
}

fn get_translates(frontmatter: &Frontmatter) -> Option<String> {
    frontmatter
        .get("translates")
        .and_then(|v| v.as_str())
        .map(|s| s.trim_matches('"').to_string())
}

fn get_frontmatter_translations(frontmatter: &Frontmatter) -> Vec<TranslationRef> {
    let slugs: Vec<String> = match frontmatter.get("translations") {
        Some(Value::Array(items)) => items
            .iter()
            .filter_map(|v| v.as_str().map(|s| s.trim_matches('"').to_string()))
            .filter(|s| !s.is_empty())
            .collect(),
        Some(Value::String(items)) => items
            .split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(String::from)
            .collect(),
        _ => Vec::new(),
    };
    slugs
        .into_iter()
        .map(|slug| TranslationRef {
            slug,
            lang: String::new(),
            name: String::new(),
            title: String::new(),
        })
        .collect()
}

/// Tries to get `date` from the front-matter metadata, else from filename
/// Input examples:
///   frontmatter = Frontmatter {date: Value("2024-10-10")}
///   path = "2024-01-01-myfile.md"
pub fn get_date(frontmatter: &Frontmatter, path: &Path) -> Option<NaiveDateTime> {
    if let Some(input) = frontmatter.get("date").and_then(|v| v.as_str()) {
        match try_to_parse_date(input) {
            Ok(date) => return Some(date),
            Err(e) => {
                error!(
                    "ERROR: Invalid date format {} when parsing {}, {}",
                    input,
                    path.display(),
                    e
                );
                process::exit(1);
            }
        }
    }
    extract_date_from_filename(path)
}

/// Tries to parse 3 different date formats or return Error.
/// input: "2024-01-01 15:40:56" | "2024-01-01 15:40" | "2024-01-01"
fn try_to_parse_date(input: &str) -> Result<NaiveDateTime, chrono::ParseError> {
    // Fix input to match the format "2023-02-08 19:03:32" or "2023-02-08 19:03" or "2023-02-08"
    // even if the input is on format 2020-01-19T21:05:12.984Z or 2020-01-19T21:05:12+0000
    let re = Regex::new(re::CAPTURE_DATE_PREFIX_FROM_TEXT)
        .expect("Date extraction regex should compile");
    let input = re.find(input).map_or("", |m| m.as_str());

    input
        .parse::<NaiveDateTime>()
        .or_else(|_| NaiveDateTime::parse_from_str(input, "%Y-%m-%d %H:%M:%S"))
        .or_else(|_| NaiveDateTime::parse_from_str(input, "%Y-%m-%d %H:%M"))
        .or_else(|_| {
            NaiveDate::parse_from_str(input, "%Y-%m-%d").map(|d| {
                // and_hms_opt should always succeed with valid time values, but provide fallback
                d.and_hms_opt(0, 0, 0).unwrap_or_else(|| {
                    // This should never happen with valid inputs, but provide safe fallback
                    NaiveDate::from_ymd_opt(1970, 1, 1)
                        .unwrap_or_default()
                        .and_hms_opt(0, 0, 0)
                        .unwrap_or_default()
                })
            })
        })
}

/// Use regex to extract date from filename `2024-01-01-myfile.md` or `2024-01-01-15-30-myfile.md`
/// Also handles stream prefixes like `news-2024-01-15-site-update.md`
/// Falls back to extracting date from parent directory name (e.g., `2024-01-01-my-post/`)
fn extract_date_from_filename(path: &Path) -> Option<NaiveDateTime> {
    if let Some(filename) = path.file_stem().and_then(|stem| stem.to_str()) {
        // First try direct date parsing (existing behavior for backward compatibility)
        if let Ok(date) = try_to_parse_date(filename) {
            return Some(date);
        }

        // Try to extract date from stream-date-slug pattern
        let stream_date_pattern = Regex::new(re::CAPTURE_DATE_FROM_STREAM_DATED_FILENAME)
            .expect("Stream date extraction regex should compile");
        if let Some(captures) = stream_date_pattern.captures(filename) {
            if let Some(date_match) = captures.get(1) {
                if let Ok(date) = try_to_parse_date(date_match.as_str()) {
                    return Some(date);
                }
            }
        }
    }

    // Fallback: try extracting date from parent directory name
    if let Some(parent_name) = path
        .parent()
        .and_then(|p| p.file_name())
        .and_then(|n| n.to_str())
    {
        if let Ok(date) = try_to_parse_date(parent_name) {
            return Some(date);
        }
    }

    None
}

pub fn check_for_duplicate_slugs(contents: &Vec<&Content>) -> Result<(), String> {
    let mut seen = HashSet::new();

    for content in contents {
        if !seen.insert(&content.slug) {
            return Err(content.slug.clone());
        }
    }

    Ok(())
}

/// Find an existing content file whose slug matches the given target slug.
/// Checks filename-derived slug, lang-stripped slug, and explicit slug in frontmatter.
pub fn find_file_by_slug(content_folder: &Path, target_slug: &str) -> Option<std::path::PathBuf> {
    let mut candidates: Vec<std::path::PathBuf> = Vec::new();

    for entry in walkdir::WalkDir::new(content_folder)
        .into_iter()
        .filter_map(std::result::Result::ok)
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("md") {
            continue;
        }
        if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
            let candidate = remove_date_from_filename(stem);
            if candidate == target_slug {
                return Some(path.to_path_buf());
            }
            // Also try stripping lang prefix (e.g. "pt-slug" -> "slug")
            if let Some((prefix, rest)) = candidate.split_once('-') {
                if is_iso_639_1_code(prefix) && rest == target_slug {
                    return Some(path.to_path_buf());
                }
            }
        }
        candidates.push(path.to_path_buf());
    }

    // Fallback: check explicit slug in frontmatter (handles stream-prefixed slugs)
    for path in &candidates {
        if let Ok(content) = fs::read_to_string(path) {
            if let Ok((fm, _)) = crate::parser::parse_front_matter(&content) {
                if let Some(slug_val) = fm.get("slug") {
                    let slug = slug_val
                        .as_str()
                        .unwrap_or("")
                        .trim_matches('"')
                        .to_string();
                    if slug == target_slug {
                        return Some(path.clone());
                    }
                }
            }
        }
    }

    None
}

#[derive(Debug, Clone)]
pub struct CreateContentParams {
    pub title: String,
    pub tags: Option<String>,
    pub directory: Option<String>,
    pub page: bool,
    pub lang: Option<String>,
    pub translates: Option<String>,
}

#[derive(Debug, Clone)]
pub struct CreateContentResult {
    pub file_path: std::path::PathBuf,
    pub title: String,
    pub slug: String,
    pub date: Option<String>,
    pub tags: Option<String>,
    pub lang: Option<String>,
    pub translates: Option<String>,
    pub is_page: bool,
}

pub fn create_content(
    input_folder: &Path,
    config_path: &Path,
    params: &CreateContentParams,
) -> Result<CreateContentResult, String> {
    let content_folder = get_content_folder(&Data::from_file(config_path).site, input_folder);
    let mut path = content_folder.clone();

    if let Some(ref lang) = params.lang {
        if !is_iso_639_1_code(lang) {
            return Err(format!(
                "Invalid language code: {lang}. Must be a valid ISO 639-1 code."
            ));
        }
    }

    let slug = crate::slugify::slugify(&params.title);
    let now = chrono::Local::now();
    let mut in_subfolder = false;

    if let Some(existing) = find_file_by_slug(&content_folder, &slug) {
        return Err(format!(
            "Slug '{slug}' already exists: {}",
            existing.display()
        ));
    }

    if let Some(ref translates_slug) = params.translates {
        let lang = params
            .lang
            .as_ref()
            .ok_or("--lang is required with --translates")?;
        if let Some(original_path) = find_file_by_slug(&content_folder, translates_slug) {
            if let Ok(relative) = original_path.strip_prefix(&content_folder) {
                // Check if the original file is inside a slug-named content subfolder
                // e.g. content/i-like-potato/i-like-potato.md (subfolder matches slug)
                // vs content/posts/i-like-potato.md (just a category directory)
                let parent = original_path.parent().ok_or("file should have parent")?;
                let parent_name = parent.file_name().and_then(|n| n.to_str()).unwrap_or("");
                let components: Vec<_> = relative.components().collect();
                if components.len() > 1 && parent_name == translates_slug {
                    in_subfolder = true;
                    path = parent.to_path_buf();
                    path.push(format!("{lang}-{slug}.md"));
                } else {
                    // Place in the same directory as the original
                    path = parent.to_path_buf();
                    path.push(format!("{slug}.md"));
                }
            }
        } else {
            return Err(format!(
                "Cannot find content with slug '{translates_slug}' in {}",
                content_folder.display()
            ));
        }
    } else {
        if let Some(ref dir) = params.directory {
            path.push(dir);
            std::fs::create_dir_all(&path)
                .map_err(|e| format!("Failed to create directory: {e}"))?;
        } else if params.page {
            if content_folder.join("pages").is_dir() {
                path.push("pages");
            }
        } else if content_folder.join("posts").is_dir() {
            path.push("posts");
        }
        path.push(format!("{slug}.md"));
    }

    if path.exists() {
        return Err(format!("File already exists: {}", path.display()));
    }

    let tags = params.tags.as_deref();
    let lang = params.lang.as_deref();
    let translates = params.translates.as_deref();
    let needs_translates_field = translates.is_some() && !in_subfolder;
    let date_str = if params.page {
        None
    } else {
        Some(now.format("%Y-%m-%d %H:%M:%S").to_string())
    };
    let body = build_new_content_body(
        &params.title,
        date_str.as_deref(),
        tags,
        lang,
        translates,
        needs_translates_field,
    );

    let mut file =
        std::fs::File::create(&path).map_err(|e| format!("Failed to create file: {e}"))?;
    file.write_all(body.as_bytes())
        .map_err(|e| format!("Failed to write to file: {e}"))?;

    let date = if params.page {
        None
    } else {
        Some(now.format("%Y-%m-%d %H:%M:%S").to_string())
    };

    Ok(CreateContentResult {
        file_path: path,
        title: params.title.clone(),
        slug,
        date,
        tags: params.tags.clone(),
        lang: params.lang.clone(),
        translates: params.translates.clone(),
        is_page: params.page,
    })
}

/// Create a new file with the given text as title and slug
pub fn new(input_folder: &Path, text: &str, cli_args: &Arc<Cli>, config_path: &Path) {
    let params = CreateContentParams {
        title: text.to_string(),
        tags: cli_args.create.tags.clone(),
        directory: cli_args.create.directory.clone(),
        page: cli_args.create.page,
        lang: cli_args.create.lang.clone(),
        translates: cli_args.create.translates.clone(),
    };
    match create_content(input_folder, config_path, &params) {
        Ok(result) => {
            print_new_content_json(
                &result.file_path,
                text,
                &result.slug,
                result.date.as_deref(),
                params.tags.as_deref(),
                params.lang.as_deref(),
                params.translates.as_deref(),
            );
            if cli_args.create.edit {
                open_in_editor(&result.file_path);
            }
        }
        Err(e) => error!("{e}"),
    }
}

fn json_to_fm_value(v: &serde_json::Value) -> Option<frontmatter_gen::Value> {
    match v {
        serde_json::Value::Null => None,
        serde_json::Value::String(s) => Some(frontmatter_gen::Value::String(s.clone())),
        serde_json::Value::Bool(b) => Some(frontmatter_gen::Value::Boolean(*b)),
        serde_json::Value::Number(n) => n.as_f64().map(frontmatter_gen::Value::Number),
        serde_json::Value::Array(arr) => {
            let items: Vec<frontmatter_gen::Value> =
                arr.iter().filter_map(json_to_fm_value).collect();
            Some(frontmatter_gen::Value::Array(items))
        }
        serde_json::Value::Object(map) => {
            let mut fm = Frontmatter::new();
            for (k, v) in map {
                if let Some(fv) = json_to_fm_value(v) {
                    fm.insert(k.clone(), fv);
                }
            }
            Some(frontmatter_gen::Value::Object(Box::new(fm)))
        }
    }
}

fn fm_value_to_json(v: &frontmatter_gen::Value) -> serde_json::Value {
    match v {
        frontmatter_gen::Value::Null => serde_json::Value::Null,
        frontmatter_gen::Value::String(s) => serde_json::Value::String(s.clone()),
        frontmatter_gen::Value::Boolean(b) => serde_json::Value::Bool(*b),
        frontmatter_gen::Value::Number(n) => serde_json::Value::Number(
            serde_json::Number::from_f64(*n).unwrap_or_else(|| serde_json::Number::from(0)),
        ),
        frontmatter_gen::Value::Array(arr) => {
            serde_json::Value::Array(arr.iter().map(fm_value_to_json).collect())
        }
        frontmatter_gen::Value::Object(fm) => {
            let map: serde_json::Map<String, serde_json::Value> = fm
                .iter()
                .map(|(k, v)| (k.clone(), fm_value_to_json(v)))
                .collect();
            serde_json::Value::Object(map)
        }
        frontmatter_gen::Value::Tagged(tag, inner) => {
            serde_json::json!({ tag: fm_value_to_json(inner) })
        }
    }
}

pub fn update_frontmatter(
    file_path: &Path,
    updates: &serde_json::Map<String, serde_json::Value>,
) -> Result<serde_json::Value, String> {
    let file_content = fs::read_to_string(file_path).map_err(|e| e.to_string())?;
    let (mut frontmatter, raw_markdown) = crate::parser::parse_front_matter(&file_content)?;

    for (key, value) in updates {
        if value.is_null() {
            frontmatter.remove(key);
        } else if let Some(fm_val) = json_to_fm_value(value) {
            frontmatter.insert(key.clone(), fm_val);
        }
    }

    let yaml_str = serde_yaml::to_string(&frontmatter)
        .map_err(|e| format!("Failed to serialize frontmatter: {e}"))?;

    let new_content = format!("---\n{yaml_str}---\n{raw_markdown}");
    fs::write(file_path, new_content).map_err(|e| format!("Failed to write file: {e}"))?;

    let result: serde_json::Map<String, serde_json::Value> = frontmatter
        .iter()
        .map(|(k, v)| (k.clone(), fm_value_to_json(v)))
        .collect();
    Ok(serde_json::Value::Object(result))
}

pub fn get_raw_content(
    content_folder: &Path,
    slug: &str,
) -> Result<(serde_json::Value, String, std::path::PathBuf, usize), String> {
    let file_path = find_file_by_slug(content_folder, slug)
        .ok_or_else(|| format!("Content with slug '{slug}' not found"))?;

    let file_content = fs::read_to_string(&file_path).map_err(|e| e.to_string())?;
    let (frontmatter, raw_markdown) = crate::parser::parse_front_matter(&file_content)?;

    let fm_byte_len = file_content.len() - raw_markdown.len();
    let frontmatter_lines = file_content[..fm_byte_len]
        .chars()
        .filter(|c| *c == '\n')
        .count();

    let fm_json: serde_json::Map<String, serde_json::Value> = frontmatter
        .iter()
        .map(|(k, v)| (k.clone(), fm_value_to_json(v)))
        .collect();

    Ok((
        serde_json::Value::Object(fm_json),
        raw_markdown.to_string(),
        file_path,
        frontmatter_lines,
    ))
}

pub fn update_content_body(
    content_folder: &Path,
    slug: &str,
    new_body: &str,
    frontmatter_updates: Option<&serde_json::Map<String, serde_json::Value>>,
) -> Result<serde_json::Value, String> {
    let file_path = find_file_by_slug(content_folder, slug)
        .ok_or_else(|| format!("Content with slug '{slug}' not found"))?;

    let file_content = fs::read_to_string(&file_path).map_err(|e| e.to_string())?;
    let (mut frontmatter, _) = crate::parser::parse_front_matter(&file_content)?;

    if let Some(updates) = frontmatter_updates {
        for (key, value) in updates {
            if value.is_null() {
                frontmatter.remove(key);
            } else if let Some(fm_val) = json_to_fm_value(value) {
                frontmatter.insert(key.clone(), fm_val);
            }
        }
    }

    let yaml_str = serde_yaml::to_string(&frontmatter)
        .map_err(|e| format!("Failed to serialize frontmatter: {e}"))?;

    let new_content = format!("---\n{yaml_str}---\n{new_body}");
    fs::write(&file_path, new_content).map_err(|e| format!("Failed to write file: {e}"))?;

    let result: serde_json::Map<String, serde_json::Value> = frontmatter
        .iter()
        .map(|(k, v)| (k.clone(), fm_value_to_json(v)))
        .collect();
    Ok(serde_json::Value::Object(result))
}

pub fn clone_content(
    content_folder: &Path,
    source_slug: &str,
    new_title: &str,
    new_slug: Option<&str>,
) -> Result<(std::path::PathBuf, String), String> {
    let source_path = find_file_by_slug(content_folder, source_slug)
        .ok_or_else(|| format!("Content with slug '{source_slug}' not found"))?;

    let slug = new_slug.map_or_else(|| crate::slugify::slugify(new_title), String::from);

    if find_file_by_slug(content_folder, &slug).is_some() {
        return Err(format!("Slug '{slug}' already exists"));
    }

    let parent = source_path
        .parent()
        .ok_or_else(|| "Source file has no parent directory".to_string())?;
    let dest_path = parent.join(format!("{slug}.md"));

    if dest_path.exists() {
        return Err(format!("File already exists: {}", dest_path.display()));
    }

    fs::copy(&source_path, &dest_path).map_err(|e| {
        format!(
            "Failed to copy {} to {}: {e}",
            source_path.display(),
            dest_path.display()
        )
    })?;

    let mut updates = serde_json::Map::new();
    updates.insert(
        "title".to_string(),
        serde_json::Value::String(new_title.to_string()),
    );
    updates.insert("slug".to_string(), serde_json::Value::String(slug.clone()));
    // Remove fields that should not carry over to the clone
    updates.insert("aliases".to_string(), serde_json::Value::Null);
    updates.insert("translates".to_string(), serde_json::Value::Null);

    update_frontmatter(&dest_path, &updates)
        .map_err(|e| format!("Failed to update cloned frontmatter: {e}"))?;

    Ok((dest_path, slug))
}

pub fn delete_content(content_folder: &Path, slug: &str) -> Result<std::path::PathBuf, String> {
    let file_path = find_file_by_slug(content_folder, slug)
        .ok_or_else(|| format!("Content with slug '{slug}' not found"))?;
    fs::remove_file(&file_path)
        .map_err(|e| format!("Failed to delete {}: {e}", file_path.display()))?;
    Ok(file_path)
}

pub fn move_content(
    content_folder: &Path,
    slug: &str,
    new_filename: &str,
) -> Result<(std::path::PathBuf, std::path::PathBuf), String> {
    let file_path = find_file_by_slug(content_folder, slug)
        .ok_or_else(|| format!("Content with slug '{slug}' not found"))?;

    if !std::path::Path::new(new_filename)
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("md"))
    {
        return Err("New filename must end with .md".to_string());
    }

    let new_path = if new_filename.contains('/') || new_filename.contains(std::path::MAIN_SEPARATOR)
    {
        let full = content_folder.join(new_filename);
        if let Some(parent) = full.parent() {
            fs::create_dir_all(parent).map_err(|e| format!("Failed to create directory: {e}"))?;
        }
        full
    } else {
        let parent = file_path
            .parent()
            .ok_or_else(|| "File has no parent directory".to_string())?;
        parent.join(new_filename)
    };

    if new_path.exists() {
        return Err(format!(
            "Target file already exists: {}",
            new_path.display()
        ));
    }

    fs::rename(&file_path, &new_path).map_err(|e| {
        format!(
            "Failed to move {} to {}: {e}",
            file_path.display(),
            new_path.display()
        )
    })?;

    Ok((file_path, new_path))
}

fn open_in_editor(path: &Path) {
    let editor = std::env::var("EDITOR").unwrap_or_else(|_| {
        if cfg!(target_os = "windows") {
            "notepad".to_string()
        } else {
            "nano".to_string()
        }
    });
    let status = std::process::Command::new(editor).arg(path).status();
    if let Err(e) = status {
        error!("Failed to open editor: {e:?}");
    }
}

fn build_new_content_body(
    text: &str,
    date: Option<&str>,
    tags: Option<&str>,
    lang: Option<&str>,
    translates: Option<&str>,
    needs_translates_field: bool,
) -> String {
    use std::fmt::Write;

    let has_frontmatter =
        date.is_some() || tags.is_some() || lang.is_some() || needs_translates_field;
    if !has_frontmatter {
        return format!("# {text}\n");
    }

    let mut fm = String::from("---\n");
    // Always include title in frontmatter for translations so the translated
    // title is explicit and not derived from the slug
    if lang.is_some() {
        let _ = writeln!(fm, "title: \"{}\"", text.replace('"', "\\\""));
    }
    if let Some(date) = date {
        let _ = writeln!(fm, "date: {date}");
    }
    if let Some(lang) = lang {
        let _ = writeln!(fm, "language: {lang}");
    }
    if needs_translates_field {
        let _ = writeln!(fm, "translates: {}", translates.expect("checked above"));
    }
    if let Some(tags) = tags {
        let _ = writeln!(fm, "tags: {tags}");
    }
    fm.push_str("---\n");
    format!("{fm}# {text}\n")
}

fn print_new_content_json(
    path: &Path,
    text: &str,
    slug: &str,
    date: Option<&str>,
    tags: Option<&str>,
    lang: Option<&str>,
    translates: Option<&str>,
) {
    let mut output = serde_json::Map::new();
    output.insert(
        "file".to_string(),
        serde_json::Value::String(path.display().to_string()),
    );
    output.insert(
        "title".to_string(),
        serde_json::Value::String(text.to_string()),
    );
    output.insert(
        "slug".to_string(),
        serde_json::Value::String(slug.to_string()),
    );
    if let Some(date) = date {
        output.insert(
            "date".to_string(),
            serde_json::Value::String(date.to_string()),
        );
    }
    if let Some(tags) = tags {
        output.insert(
            "tags".to_string(),
            serde_json::Value::String(tags.to_string()),
        );
    }
    if let Some(lang) = lang {
        output.insert(
            "language".to_string(),
            serde_json::Value::String(lang.to_string()),
        );
    }
    if let Some(translates_slug) = translates {
        output.insert(
            "translates".to_string(),
            serde_json::Value::String(translates_slug.to_string()),
        );
    }
    println!(
        "{}",
        serde_json::to_string(&output).expect("JSON serialization should not fail")
    );
}

/// Capture `card_image` from frontmatter, then if not defined
/// take the first img src found in the post content
pub fn get_card_image(
    frontmatter: &Frontmatter,
    html: &str,
    path: &Path,
    slug: &str,
    media_path: &str,
    content_dir: Option<&Path>,
) -> Option<String> {
    if let Some(card_image) = frontmatter.get("card_image") {
        return Some(card_image.to_string());
    }

    // Try to find image matching the slug
    if let Some(value) = find_matching_file(
        slug,
        path,
        "card",
        &["png", "jpg", "jpeg"],
        media_path,
        content_dir,
    ) {
        return Some(value);
    }

    // try banner_image
    if let Some(banner_image) = get_banner_image(frontmatter, path, slug, media_path, content_dir) {
        return Some(banner_image);
    }

    // first <img> src attribute
    let img_regex =
        Regex::new(re::CAPTURE_SRC_FROM_IMG_HTMLTAG).expect("Image src regex should compile");
    img_regex
        .captures(html)
        .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()))
}

/// Find a matching media file name in the media folder
/// {slug}.{kind}.{ext}
/// if the file exists, return the path to the file
/// if not found in media folder, try to find in the same directory
/// if the file does not exist, return None
fn find_matching_file(
    slug: &str,
    path: &Path,
    kind: &str,
    exts: &[&str],
    media_folder_name: &str,
    content_dir: Option<&Path>,
) -> Option<String> {
    let parent_path = path.parent().unwrap_or(path);
    let media_path = parent_path.join(media_folder_name);
    for ext in exts {
        // Content subfolder media: {parent}/{slug}/media/{kind}.{ext}
        let content_subfolder_media = parent_path.join(slug).join(media_folder_name);
        if content_subfolder_media.is_dir() {
            for subfolder_filename in [format!("{kind}.{ext}"), format!("{slug}.{ext}")] {
                let file_path = content_subfolder_media.join(&subfolder_filename);
                if file_path.exists() {
                    return Some(format!("{media_folder_name}/{slug}/{subfolder_filename}"));
                }
            }
        }
        // Global media subfolder: {parent}/media/{slug}/{kind}.{ext}
        let slug_subfolder = media_path.join(slug);
        if slug_subfolder.is_dir() {
            for subfolder_filename in [format!("{kind}.{ext}"), format!("{slug}.{ext}")] {
                let file_path = slug_subfolder.join(&subfolder_filename);
                if file_path.exists() {
                    return Some(format!("{media_folder_name}/{slug}/{subfolder_filename}"));
                }
            }
        }
        // Flat slug-prefixed files: media/{slug}.{kind}.{ext}
        for image_filename in [format!("{slug}.{kind}.{ext}"), format!("{slug}.{ext}")] {
            let file_path = media_path.join(&image_filename);
            if file_path.exists() {
                return Some(format!("{media_folder_name}/{image_filename}"));
            }
            let file_path = parent_path.join(&image_filename);
            if file_path.exists() {
                return Some(image_filename.clone());
            }
        }
    }

    // Fallback: generic {kind}.{ext} in subfolder media (shared by all files in the subfolder)
    // e.g., content/language-streams/media/banner.jpg matches for any .md in that subfolder
    // Only applies when the content is in a real subfolder, not the content root itself.
    // An ancestor (up to 3 levels) must have its own media folder for this to be a subfolder.
    if media_path.is_dir() {
        let is_subfolder = content_dir.is_some_and(|cd| parent_path != cd);
        if is_subfolder {
            if let Some(subfolder_name) = parent_path.file_name().and_then(|n| n.to_str()) {
                for ext in exts {
                    let generic_filename = format!("{kind}.{ext}");
                    let file_path = media_path.join(&generic_filename);
                    if file_path.exists() {
                        return Some(format!(
                            "{media_folder_name}/{subfolder_name}/{generic_filename}"
                        ));
                    }
                }
            }
        }
    }

    // Fallback: check the content root's media directory for slug-based files.
    // When content is in a subfolder (content/docs/post.md), the root
    // content/media/ may have the banner (e.g. downloaded by image_provider).
    if let Some(cd) = content_dir {
        if parent_path != cd {
            let root_media = cd.join(media_folder_name);
            if root_media.is_dir() {
                for ext in exts {
                    for image_filename in [format!("{slug}.{kind}.{ext}"), format!("{slug}.{ext}")]
                    {
                        if root_media.join(&image_filename).exists() {
                            return Some(format!("{media_folder_name}/{image_filename}"));
                        }
                    }
                }
            }
        }
    }

    None
}

fn get_banner_image(
    frontmatter: &Frontmatter,
    path: &Path,
    slug: &str,
    media_path: &str,
    content_dir: Option<&Path>,
) -> Option<String> {
    if let Some(banner_image) = frontmatter.get("banner_image") {
        return Some(
            banner_image
                .as_str()
                .unwrap_or("")
                .trim_matches('"')
                .to_string(),
        );
    }

    // Try to find image matching the slug
    if let Some(value) = find_matching_file(
        slug,
        path,
        "banner",
        &["png", "jpg", "jpeg"],
        media_path,
        content_dir,
    ) {
        return Some(value);
    }

    // attempt to get extra.banner_image
    if let Some(extra) = frontmatter.get("extra") {
        if let Some(extra) = extra.as_object() {
            if let Some(banner_image) = extra.get("banner_image") {
                let url = banner_image.to_string();
                // trim start and end quotes
                return Some(url.trim_matches('"').to_string());
            }
        }
    }
    None
}

fn fix_at_media_refs(
    html: &str,
    media_path: &str,
    slug: &str,
    path: &Path,
    content_dir: Option<&Path>,
) -> String {
    let re =
        Regex::new(re::REPLACE_AT_MEDIA_REF_IN_HTML).expect("At-media HTML regex should compile");

    let parent = path.parent().unwrap_or(path);
    let has_slug_media = content_dir.is_some_and(|cd| cd.join(media_path).join(slug).is_dir())
        || content_dir.is_some_and(|cd| cd.join(slug).join(media_path).is_dir());
    let parent_has_media =
        content_dir.is_some_and(|cd| parent != cd) && parent.join(media_path).is_dir();

    let replacement = if has_slug_media {
        format!("${{attr}}=\"{media_path}/{slug}/")
    } else if parent_has_media {
        let folder = parent.file_name().and_then(|n| n.to_str()).unwrap_or(slug);
        format!("${{attr}}=\"{media_path}/{folder}/")
    } else {
        format!("${{attr}}=\"{media_path}/")
    };
    re.replace_all(html, replacement.as_str()).into_owned()
}

#[cfg(test)]
#[path = "tests/content.rs"]
mod tests;