-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathpage.rs
930 lines (834 loc) · 32.5 KB
/
page.rs
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
/// A page, can be a blog post or a basic page
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use libs::once_cell::sync::Lazy;
use libs::regex::Regex;
use libs::tera::{Context as TeraContext, Tera};
use config::Config;
use errors::{Context, Result};
use markdown::context::Caches;
use markdown::{render_content, RenderContext};
use utils::slugs::slugify_paths;
use utils::table_of_contents::Heading;
use utils::templates::{render_template, ShortcodeDefinition};
use utils::types::InsertAnchor;
use crate::file_info::FileInfo;
use crate::front_matter::{split_page_content, PageFrontMatter};
use crate::library::Library;
use crate::ser::SerializingPage;
use crate::utils::get_reading_analytics;
use crate::utils::{find_related_assets, has_anchor};
use utils::anchors::has_anchor_id;
use utils::fs::read_file;
// Based on https://regex101.com/r/H2n38Z/1/tests
// A regex parsing RFC3339 date followed by {_,-} and some characters
static RFC3339_DATE: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"^(?P<datetime>(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)([01][0-9]|2[0-3]):([0-5][0-9])))?)\s?(_|-)(?P<slug>.+$)"
).unwrap()
});
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Page {
/// All info about the actual file
pub file: FileInfo,
/// The front matter meta-data
pub meta: PageFrontMatter,
/// The list of parent sections relative paths
pub ancestors: Vec<String>,
/// The actual content of the page, in markdown
pub raw_content: String,
/// All the non-md files we found next to the .md file
pub assets: Vec<PathBuf>,
/// All the non-md files we found next to the .md file
pub serialized_assets: Vec<String>,
/// The HTML rendered of the page
pub content: String,
/// The slug of that page.
/// First tries to find the slug in the meta and defaults to filename otherwise
pub slug: String,
/// The URL path of the page, always starting with a slash
pub path: String,
/// The components of the path of the page
pub components: Vec<String>,
/// The full URL for that page
pub permalink: String,
/// The summary for the article, defaults to None
/// When <!-- more --> is found in the text, will take the content up to that part
/// as summary
pub summary: Option<String>,
/// The previous page when sorting: earlier/earlier_updated/lighter/prev
pub lower: Option<PathBuf>,
/// The next page when sorting: later/later_updated/heavier/next
pub higher: Option<PathBuf>,
/// Toc made from the headings of the markdown file
pub toc: Vec<Heading>,
/// How many words in the raw content
pub word_count: Option<usize>,
/// How long would it take to read the raw content.
/// See `get_reading_analytics` on how it is calculated
pub reading_time: Option<usize>,
/// The language of that page. Equal to the default lang if the user doesn't setup `languages` in config.
/// Corresponds to the lang in the {slug}.{lang}.md file scheme
pub lang: String,
/// Contains all the translated version of that page
pub translations: Vec<PathBuf>,
/// The list of all internal links (as path to markdown file), with optional anchor fragments.
/// We can only check the anchor after all pages have been built and their ToC compiled.
/// The page itself should exist otherwise it would have errored before getting there.
pub internal_links: Vec<(String, Option<String>)>,
/// The list of all links to external webpages. They can be validated by the `link_checker`.
pub external_links: Vec<String>,
}
impl Page {
pub fn new<P: AsRef<Path>>(file_path: P, meta: PageFrontMatter, base_path: &Path) -> Page {
let file_path = file_path.as_ref();
Page { file: FileInfo::new_page(file_path, base_path), meta, ..Self::default() }
}
/// Parse a page given the content of the .md file
/// Files without front matter or with invalid front matter are considered
/// erroneous
pub fn parse(
file_path: &Path,
content: &str,
config: &Config,
base_path: &Path,
) -> Result<Page> {
let (meta, content) = split_page_content(file_path, content)?;
let mut page = Page::new(file_path, meta, base_path);
page.lang =
page.file.find_language(&config.default_language, &config.other_languages_codes())?;
page.raw_content = content.to_string();
let (word_count, reading_time) = get_reading_analytics(&page.raw_content);
page.word_count = Some(word_count);
page.reading_time = Some(reading_time);
let mut slug_from_dated_filename = None;
let file_path_for_slug = if page.file.name == "index" {
if let Some(parent) = page.file.path.parent() {
parent.file_name().unwrap().to_str().unwrap().to_string()
} else {
page.file.name.to_string()
}
} else {
page.file.name.to_string()
};
if let Some(ref caps) = RFC3339_DATE.captures(&file_path_for_slug) {
if !config.slugify.paths_keep_dates {
slug_from_dated_filename = Some(caps.name("slug").unwrap().as_str().to_string());
}
if page.meta.date.is_none() {
page.meta.date = Some(caps.name("datetime").unwrap().as_str().to_string());
page.meta.date_to_datetime();
}
}
page.slug = {
if let Some(ref slug) = page.meta.slug {
slugify_paths(slug, config.slugify.paths)
} else if let Some(slug) = slug_from_dated_filename {
slugify_paths(&slug, config.slugify.paths)
} else {
slugify_paths(&file_path_for_slug, config.slugify.paths)
}
};
page.path = if let Some(ref p) = page.meta.path {
let path = p.trim();
if path.starts_with('/') {
path.into()
} else {
format!("/{}", path)
}
} else {
let mut path = if page.file.components.is_empty() {
if page.file.name == "index" && page.file.colocated_path.is_none() {
String::new()
} else {
page.slug.clone()
}
} else {
format!("{}/{}", page.file.components.join("/"), page.slug)
};
if page.lang != config.default_language {
path = format!("{}/{}", page.lang, path);
}
format!("/{}", path)
};
if !page.path.ends_with('/') {
page.path = format!("{}/", page.path);
}
page.components = page
.path
.split('/')
.map(|p| p.to_string())
.filter(|p| !p.is_empty())
.collect::<Vec<_>>();
page.permalink = config.make_permalink(&page.path);
Ok(page)
}
pub fn find_language(&mut self) {}
/// Read and parse a .md file into a Page struct
pub fn from_file<P: AsRef<Path>>(path: P, config: &Config, base_path: &Path) -> Result<Page> {
let path = path.as_ref();
let content = read_file(path)?;
let mut page = Page::parse(path, &content, config, base_path)?;
if page.file.name == "index" {
let parent_dir = path.parent().unwrap();
page.assets = find_related_assets(parent_dir, config, true);
page.serialized_assets = page.serialize_assets(base_path);
} else {
page.assets = vec![];
}
Ok(page)
}
/// We need access to all pages url to render links relative to content
/// so that can't happen at the same time as parsing
pub fn render_markdown(
&mut self,
permalinks: &HashMap<String, String>,
tera: &Tera,
config: &Config,
anchor_insert: InsertAnchor,
shortcode_definitions: &HashMap<String, ShortcodeDefinition>,
caches: Option<Arc<Caches>>,
) -> Result<()> {
let mut context = RenderContext::new(
tera,
config,
&self.lang,
&self.permalink,
permalinks,
anchor_insert,
caches,
);
context.set_shortcode_definitions(shortcode_definitions);
context.set_current_page_path(&self.file.relative);
context.set_parent_absolute(&self.file.parent);
context.tera_context.insert("page", &SerializingPage::new(self, None, false));
let res = render_content(&self.raw_content, &context)
.with_context(|| format!("Failed to render content of {}", self.file.path.display()))?;
self.summary = res.summary;
self.content = res.body;
self.toc = res.toc;
self.external_links = res.external_links;
self.internal_links = res.internal_links;
Ok(())
}
/// Renders the page using the default layout, unless specified in front-matter
pub fn render_html(&self, tera: &Tera, config: &Config, library: &Library) -> Result<String> {
let tpl_name = match self.meta.template {
Some(ref l) => l,
None => "page.html",
};
let mut context = TeraContext::new();
context.insert("config", &config.serialize(&self.lang));
context.insert("current_url", &self.permalink);
context.insert("current_path", &self.path);
context.insert("page", &self.serialize(library));
context.insert("lang", &self.lang);
render_template(tpl_name, tera, context, &config.theme)
.with_context(|| format!("Failed to render page '{}'", self.file.path.display()))
}
/// Creates a vectors of asset URLs.
fn serialize_assets(&self, base_path: &Path) -> Vec<String> {
self.assets
.iter()
.filter_map(|asset| asset.strip_prefix(self.file.path.parent().unwrap()).ok())
.filter_map(|filename| filename.to_str())
.map(|filename| {
let mut path = self.file.path.clone();
// Popping the index.md from the path since file.parent would be one level too high
// for our need here
path.pop();
path.push(filename);
path = path
.strip_prefix(&base_path.join("content"))
.expect("Should be able to stripe prefix")
.to_path_buf();
path
})
.map(|path| format!("/{}", path.display()))
.collect()
}
pub fn has_anchor(&self, anchor: &str) -> bool {
has_anchor(&self.toc, anchor)
}
pub fn has_anchor_id(&self, id: &str) -> bool {
has_anchor_id(&self.content, id)
}
pub fn serialize<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
SerializingPage::new(self, Some(library), true)
}
pub fn serialize_without_siblings<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
SerializingPage::new(self, Some(library), false)
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::fs::{create_dir, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use libs::globset::{Glob, GlobSetBuilder};
use tempfile::tempdir;
use templates::ZOLA_TERA;
use crate::Page;
use config::{Config, LanguageOptions};
use utils::slugs::SlugifyStrategy;
use utils::types::InsertAnchor;
#[test]
fn can_parse_a_valid_page() {
let config = Config::default_for_test();
let content = r#"
+++
title = "Hello"
description = "hey there"
slug = "hello-world"
+++
Hello world"#;
let res = Page::parse(Path::new("post.md"), content, &config, &PathBuf::new());
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(
&HashMap::default(),
&ZOLA_TERA,
&config,
InsertAnchor::None,
&HashMap::new(),
None,
)
.unwrap();
assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
assert_eq!(page.raw_content, "Hello world".to_string());
assert_eq!(page.content, "<p>Hello world</p>\n".to_string());
}
#[test]
fn can_parse_author() {
let config = Config::default_for_test();
let content = r#"
+++
title = "Hello"
description = "hey there"
authors = ["[email protected] (A. Person)"]
+++
Hello world"#;
let res = Page::parse(Path::new("post.md"), content, &config, &PathBuf::new());
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(
&HashMap::default(),
&ZOLA_TERA,
&config,
InsertAnchor::None,
&HashMap::new(),
None,
)
.unwrap();
assert_eq!(1, page.meta.authors.len());
}
#[test]
fn test_can_make_url_from_sections_and_slug() {
let content = r#"
+++
slug = "hello-world"
+++
Hello world"#;
let mut conf = Config::default();
conf.base_url = "http://hello.com/".to_string();
let res =
Page::parse(Path::new("content/posts/intro/start.md"), content, &conf, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/posts/intro/hello-world/");
assert_eq!(page.components, vec!["posts", "intro", "hello-world"]);
assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world/");
}
#[test]
fn can_make_url_from_slug_only() {
let content = r#"
+++
slug = "hello-world"
+++
Hello world"#;
let config = Config::default();
let res = Page::parse(Path::new("start.md"), content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/hello-world/");
assert_eq!(page.components, vec!["hello-world"]);
assert_eq!(page.permalink, config.make_permalink("hello-world"));
}
#[test]
fn can_make_url_from_slug_only_with_no_special_chars() {
let content = r#"
+++
slug = "hello-&-world"
+++
Hello world"#;
let mut config = Config::default();
config.slugify.paths = SlugifyStrategy::On;
let res = Page::parse(Path::new("start.md"), content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/hello-world/");
assert_eq!(page.components, vec!["hello-world"]);
assert_eq!(page.permalink, config.make_permalink("hello-world"));
}
#[test]
fn can_make_url_from_utf8_slug_frontmatter() {
let content = r#"
+++
slug = "日本"
+++
Hello world"#;
let mut config = Config::default();
config.slugify.paths = SlugifyStrategy::Safe;
let res = Page::parse(Path::new("start.md"), content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/日本/");
assert_eq!(page.components, vec!["日本"]);
assert_eq!(page.permalink, config.make_permalink("日本"));
}
#[test]
fn can_make_url_from_path() {
let content = r#"
+++
path = "hello-world"
+++
Hello world"#;
let config = Config::default();
let res = Page::parse(
Path::new("content/posts/intro/start.md"),
content,
&config,
&PathBuf::new(),
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/hello-world/");
assert_eq!(page.components, vec!["hello-world"]);
assert_eq!(page.permalink, config.make_permalink("hello-world"));
}
#[test]
fn can_make_url_from_path_starting_slash() {
let content = r#"
+++
path = "/hello-world"
+++
Hello world"#;
let config = Config::default();
let res = Page::parse(
Path::new("content/posts/intro/start.md"),
content,
&config,
&PathBuf::new(),
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/hello-world/");
assert_eq!(page.permalink, config.make_permalink("hello-world"));
}
#[test]
fn errors_on_invalid_front_matter_format() {
// missing starting +++
let content = r#"
title = "Hello"
description = "hey there"
slug = "hello-world"
+++
Hello world"#;
let res = Page::parse(Path::new("start.md"), content, &Config::default(), &PathBuf::new());
assert!(res.is_err());
}
#[test]
fn can_make_slug_from_non_slug_filename() {
let mut config = Config::default();
config.slugify.paths = SlugifyStrategy::On;
let res =
Page::parse(Path::new(" file with space.md"), "+++\n+++\n", &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.slug, "file-with-space");
assert_eq!(page.permalink, config.make_permalink(&page.slug));
}
#[test]
fn can_make_path_from_utf8_filename() {
let mut config = Config::default();
config.slugify.paths = SlugifyStrategy::Safe;
let res = Page::parse(Path::new("日本.md"), "+++\n+++\n", &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.slug, "日本");
assert_eq!(page.permalink, config.make_permalink(&page.slug));
}
#[test]
fn can_specify_summary() {
let config = Config::default_for_test();
let content = r#"
+++
+++
Hello world
<!-- more -->"#
.to_string();
let res = Page::parse(Path::new("hello.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(
&HashMap::default(),
&ZOLA_TERA,
&config,
InsertAnchor::None,
&HashMap::new(),
None,
)
.unwrap();
assert_eq!(page.summary, Some("<p>Hello world</p>".to_string()));
}
#[test]
fn strips_footnotes_in_summary() {
let config = Config::default_for_test();
let content = r#"
+++
+++
This page use <sup>1.5</sup> and has footnotes, here's one. [^1]
Here's another. [^2]
<!-- more -->
And here's another. [^3]
[^1]: This is the first footnote.
[^2]: This is the second footnote.
[^3]: This is the third footnote."#
.to_string();
let res = Page::parse(Path::new("hello.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(
&HashMap::default(),
&ZOLA_TERA,
&config,
InsertAnchor::None,
&HashMap::new(),
None,
)
.unwrap();
assert_eq!(
page.summary,
Some("<p>This page use <sup>1.5</sup> and has footnotes, here\'s one. </p>\n<p>Here's another. </p>".to_string())
);
}
#[test]
fn page_with_assets_gets_right_info() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("with-assets");
create_dir(&nested_path).expect("create nested temp dir");
let mut f = File::create(nested_path.join("index.md")).unwrap();
f.write_all(b"+++\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.file.parent, path.join("content").join("posts"));
assert_eq!(page.slug, "with-assets");
assert_eq!(page.assets.len(), 3);
assert!(page.serialized_assets[0].starts_with('/'));
assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
}
#[test]
fn page_with_assets_and_slug_overrides_path() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("with-assets");
create_dir(&nested_path).expect("create nested temp dir");
let mut f = File::create(nested_path.join("index.md")).unwrap();
f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.file.parent, path.join("content").join("posts"));
assert_eq!(page.slug, "hey");
assert_eq!(page.assets.len(), 3);
assert_eq!(page.permalink, "http://a-website.com/posts/hey/");
}
// https://github.com/getzola/zola/issues/674
#[test]
fn page_with_assets_uses_filepath_for_assets() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("with_assets");
create_dir(&nested_path).expect("create nested temp dir");
let mut f = File::create(nested_path.join("index.md")).unwrap();
f.write_all(b"+++\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.file.parent, path.join("content").join("posts"));
assert_eq!(page.assets.len(), 3);
assert_eq!(page.serialized_assets.len(), 3);
// We should not get with-assets since that's the slugified version
assert!(page.serialized_assets[0].contains("with_assets"));
assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
}
// https://github.com/getzola/zola/issues/607
#[test]
fn page_with_assets_and_date_in_folder_name() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("2013-06-02_with-assets");
create_dir(&nested_path).expect("create nested temp dir");
let mut f = File::create(nested_path.join("index.md")).unwrap();
f.write_all(b"+++\n\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default(), path);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.file.parent, path.join("content").join("posts"));
assert_eq!(page.slug, "with-assets");
assert_eq!(page.meta.date, Some("2013-06-02".to_string()));
assert_eq!(page.assets.len(), 3);
assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
}
#[test]
fn page_with_ignored_assets_filters_out_correct_files() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("with-assets");
create_dir(&nested_path).expect("create nested temp dir");
let mut f = File::create(nested_path.join("index.md")).unwrap();
f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let mut gsb = GlobSetBuilder::new();
gsb.add(Glob::new("*.{js,png}").unwrap());
let mut config = Config::default();
config.ignored_content_globset = Some(gsb.build().unwrap());
let res = Page::from_file(nested_path.join("index.md").as_path(), &config, path);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.assets.len(), 1);
assert_eq!(page.assets[0].file_name().unwrap().to_str(), Some("graph.jpg"));
}
// https://github.com/getzola/zola/issues/1566
#[test]
fn colocated_page_with_slug_and_date_in_path() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
let articles_path = path.join("content").join("articles");
create_dir(&articles_path).expect("create posts temp dir");
let config = Config::default();
// first a non-colocated one
let file_path = articles_path.join("2021-07-29-sample-article-1.md");
let mut f = File::create(&file_path).unwrap();
f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
let res = Page::from_file(&file_path, &config, path);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/articles/hey/");
// then a colocated one, it should still work
let dir_path = articles_path.join("2021-07-29-sample-article-2.md");
create_dir(&dir_path).expect("create posts temp dir");
let mut f = File::create(&dir_path.join("index.md")).unwrap();
f.write_all(b"+++\nslug=\"ho\"\n+++\n").unwrap();
let res = Page::from_file(&dir_path.join("index.md"), &config, path);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "/articles/ho/");
}
#[test]
fn can_get_date_from_short_date_in_filename() {
let config = Config::default();
let content = r#"
+++
+++
Hello world
<!-- more -->"#
.to_string();
let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
assert_eq!(page.slug, "hello");
}
// https://github.com/getzola/zola/pull/1323#issuecomment-779401063
#[test]
fn can_get_date_from_short_date_in_filename_respects_slugification_strategy() {
let mut config = Config::default();
config.slugify.paths = SlugifyStrategy::Off;
let content = r#"
+++
+++
Hello world
<!-- more -->"#
.to_string();
let res =
Page::parse(Path::new("2018-10-08_ こんにちは.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
assert_eq!(page.slug, " こんにちは");
}
#[test]
fn can_get_date_from_filename_with_spaces() {
let config = Config::default();
let content = r#"
+++
+++
Hello world
<!-- more -->"#
.to_string();
let res =
Page::parse(Path::new("2018-10-08 - hello.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
assert_eq!(page.slug, "hello");
}
#[test]
fn can_get_date_from_filename_with_spaces_respects_slugification() {
let mut config = Config::default();
config.slugify.paths = SlugifyStrategy::Off;
let content = r#"
+++
+++
Hello world
<!-- more -->"#
.to_string();
let res =
Page::parse(Path::new("2018-10-08 - hello.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
assert_eq!(page.slug, " hello");
}
#[test]
fn can_get_date_from_full_rfc3339_date_in_filename() {
let config = Config::default();
let content = r#"
+++
+++
Hello world
<!-- more -->"#
.to_string();
let res = Page::parse(
Path::new("2018-10-02T15:00:00Z-hello.md"),
&content,
&config,
&PathBuf::new(),
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-02T15:00:00Z".to_string()));
assert_eq!(page.slug, "hello");
}
// https://github.com/getzola/zola/pull/1323#issuecomment-779401063
#[test]
fn can_get_date_from_full_rfc3339_date_in_filename_respects_slugification_strategy() {
let mut config = Config::default();
config.slugify.paths = SlugifyStrategy::Off;
let content = r#"
+++
+++
Hello world
<!-- more -->"#
.to_string();
let res = Page::parse(
Path::new("2018-10-02T15:00:00Z- こんにちは.md"),
&content,
&config,
&PathBuf::new(),
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-02T15:00:00Z".to_string()));
assert_eq!(page.slug, " こんにちは");
}
#[test]
fn frontmatter_date_override_filename_date() {
let config = Config::default();
let content = r#"
+++
date = 2018-09-09
+++
Hello world
<!-- more -->"#
.to_string();
let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-09-09".to_string()));
assert_eq!(page.slug, "hello");
}
#[test]
fn can_specify_language_in_filename() {
let mut config = Config::default();
config.languages.insert("fr".to_owned(), LanguageOptions::default());
let content = r#"
+++
+++
Bonjour le monde"#
.to_string();
let res = Page::parse(Path::new("hello.fr.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.lang, "fr".to_string());
assert_eq!(page.slug, "hello");
assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
}
#[test]
fn can_specify_language_in_filename_with_date() {
let mut config = Config::default();
config.languages.insert("fr".to_owned(), LanguageOptions::default());
let content = r#"
+++
+++
Bonjour le monde"#
.to_string();
let res =
Page::parse(Path::new("2018-10-08_hello.fr.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
assert_eq!(page.lang, "fr".to_string());
assert_eq!(page.slug, "hello");
assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
}
#[test]
fn i18n_frontmatter_path_overrides_default_permalink() {
let mut config = Config::default();
config.languages.insert("fr".to_owned(), LanguageOptions::default());
let content = r#"
+++
path = "bonjour"
+++
Bonjour le monde"#
.to_string();
let res = Page::parse(Path::new("hello.fr.md"), &content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.lang, "fr".to_string());
assert_eq!(page.slug, "hello");
assert_eq!(page.permalink, "http://a-website.com/bonjour/");
}
}