You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

675 lines
24KB

  1. /// A page, can be a blog post or a basic page
  2. use std::collections::HashMap;
  3. use std::path::{Path, PathBuf};
  4. use regex::Regex;
  5. use slotmap::Key;
  6. use slug::slugify;
  7. use tera::{Context as TeraContext, Tera};
  8. use config::Config;
  9. use errors::{Error, Result};
  10. use front_matter::{split_page_content, InsertAnchor, PageFrontMatter};
  11. use library::Library;
  12. use rendering::{render_content, Header, RenderContext};
  13. use utils::fs::{find_related_assets, read_file};
  14. use utils::site::get_reading_analytics;
  15. use utils::templates::render_template;
  16. use content::file_info::FileInfo;
  17. use content::ser::SerializingPage;
  18. lazy_static! {
  19. // Based on https://regex101.com/r/H2n38Z/1/tests
  20. // A regex parsing RFC3339 date followed by {_,-}, some characters and ended by .md
  21. static ref RFC3339_DATE: Regex = Regex::new(
  22. 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])))?)(_|-)(?P<slug>.+$)"
  23. ).unwrap();
  24. }
  25. #[derive(Clone, Debug, PartialEq)]
  26. pub struct Page {
  27. /// All info about the actual file
  28. pub file: FileInfo,
  29. /// The front matter meta-data
  30. pub meta: PageFrontMatter,
  31. /// The list of parent sections
  32. pub ancestors: Vec<Key>,
  33. /// The actual content of the page, in markdown
  34. pub raw_content: String,
  35. /// All the non-md files we found next to the .md file
  36. pub assets: Vec<PathBuf>,
  37. /// All the non-md files we found next to the .md file as string for use in templates
  38. pub serialized_assets: Vec<String>,
  39. /// The HTML rendered of the page
  40. pub content: String,
  41. /// The slug of that page.
  42. /// First tries to find the slug in the meta and defaults to filename otherwise
  43. pub slug: String,
  44. /// The URL path of the page
  45. pub path: String,
  46. /// The components of the path of the page
  47. pub components: Vec<String>,
  48. /// The full URL for that page
  49. pub permalink: String,
  50. /// The summary for the article, defaults to None
  51. /// When <!-- more --> is found in the text, will take the content up to that part
  52. /// as summary
  53. pub summary: Option<String>,
  54. /// The earlier page, for pages sorted by date
  55. pub earlier: Option<Key>,
  56. /// The later page, for pages sorted by date
  57. pub later: Option<Key>,
  58. /// The lighter page, for pages sorted by weight
  59. pub lighter: Option<Key>,
  60. /// The heavier page, for pages sorted by weight
  61. pub heavier: Option<Key>,
  62. /// Toc made from the headers of the markdown file
  63. pub toc: Vec<Header>,
  64. /// How many words in the raw content
  65. pub word_count: Option<usize>,
  66. /// How long would it take to read the raw content.
  67. /// See `get_reading_analytics` on how it is calculated
  68. pub reading_time: Option<usize>,
  69. /// The language of that page. Equal to the default lang if the user doesn't setup `languages` in config.
  70. /// Corresponds to the lang in the {slug}.{lang}.md file scheme
  71. pub lang: String,
  72. /// Contains all the translated version of that page
  73. pub translations: Vec<Key>,
  74. }
  75. impl Page {
  76. pub fn new<P: AsRef<Path>>(file_path: P, meta: PageFrontMatter) -> Page {
  77. let file_path = file_path.as_ref();
  78. Page {
  79. file: FileInfo::new_page(file_path),
  80. meta,
  81. ancestors: vec![],
  82. raw_content: "".to_string(),
  83. assets: vec![],
  84. serialized_assets: vec![],
  85. content: "".to_string(),
  86. slug: "".to_string(),
  87. path: "".to_string(),
  88. components: vec![],
  89. permalink: "".to_string(),
  90. summary: None,
  91. earlier: None,
  92. later: None,
  93. lighter: None,
  94. heavier: None,
  95. toc: vec![],
  96. word_count: None,
  97. reading_time: None,
  98. lang: String::new(),
  99. translations: Vec::new(),
  100. }
  101. }
  102. pub fn is_draft(&self) -> bool {
  103. self.meta.draft
  104. }
  105. /// Parse a page given the content of the .md file
  106. /// Files without front matter or with invalid front matter are considered
  107. /// erroneous
  108. pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Page> {
  109. let (meta, content) = split_page_content(file_path, content)?;
  110. let mut page = Page::new(file_path, meta);
  111. page.lang = page.file.find_language(config)?;
  112. page.raw_content = content;
  113. let (word_count, reading_time) = get_reading_analytics(&page.raw_content);
  114. page.word_count = Some(word_count);
  115. page.reading_time = Some(reading_time);
  116. let mut slug_from_dated_filename = None;
  117. let file_path = if page.file.name == "index" {
  118. if let Some(parent) = page.file.path.parent() {
  119. parent.file_name().unwrap().to_str().unwrap().to_string()
  120. } else {
  121. page.file.name.replace(".md", "")
  122. }
  123. } else {
  124. page.file.name.replace(".md", "")
  125. };
  126. if let Some(ref caps) = RFC3339_DATE.captures(&file_path) {
  127. slug_from_dated_filename = Some(caps.name("slug").unwrap().as_str().to_string());
  128. if page.meta.date.is_none() {
  129. page.meta.date = Some(caps.name("datetime").unwrap().as_str().to_string());
  130. page.meta.date_to_datetime();
  131. }
  132. }
  133. page.slug = {
  134. if let Some(ref slug) = page.meta.slug {
  135. slug.trim().to_string()
  136. } else if page.file.name == "index" {
  137. if let Some(parent) = page.file.path.parent() {
  138. if let Some(slug) = slug_from_dated_filename {
  139. slugify(&slug)
  140. } else {
  141. slugify(parent.file_name().unwrap().to_str().unwrap())
  142. }
  143. } else {
  144. slugify(&page.file.name)
  145. }
  146. } else {
  147. if let Some(slug) = slug_from_dated_filename {
  148. slugify(&slug)
  149. } else {
  150. slugify(&page.file.name)
  151. }
  152. }
  153. };
  154. if let Some(ref p) = page.meta.path {
  155. page.path = p.trim().trim_start_matches('/').to_string();
  156. } else {
  157. let mut path = if page.file.components.is_empty() {
  158. page.slug.clone()
  159. } else {
  160. format!("{}/{}", page.file.components.join("/"), page.slug)
  161. };
  162. if page.lang != config.default_language {
  163. path = format!("{}/{}", page.lang, path);
  164. }
  165. page.path = path;
  166. }
  167. if !page.path.ends_with('/') {
  168. page.path = format!("{}/", page.path);
  169. }
  170. page.components = page
  171. .path
  172. .split('/')
  173. .map(|p| p.to_string())
  174. .filter(|p| !p.is_empty())
  175. .collect::<Vec<_>>();
  176. page.permalink = config.make_permalink(&page.path);
  177. Ok(page)
  178. }
  179. /// Read and parse a .md file into a Page struct
  180. pub fn from_file<P: AsRef<Path>>(path: P, config: &Config) -> Result<Page> {
  181. let path = path.as_ref();
  182. let content = read_file(path)?;
  183. let mut page = Page::parse(path, &content, config)?;
  184. if page.file.name == "index" {
  185. let parent_dir = path.parent().unwrap();
  186. let assets = find_related_assets(parent_dir);
  187. if let Some(ref globset) = config.ignored_content_globset {
  188. // `find_related_assets` only scans the immediate directory (it is not recursive) so our
  189. // filtering only needs to work against the file_name component, not the full suffix. If
  190. // `find_related_assets` was changed to also return files in subdirectories, we could
  191. // use `PathBuf.strip_prefix` to remove the parent directory and then glob-filter
  192. // against the remaining path. Note that the current behaviour effectively means that
  193. // the `ignored_content` setting in the config file is limited to single-file glob
  194. // patterns (no "**" patterns).
  195. page.assets = assets
  196. .into_iter()
  197. .filter(|path| match path.file_name() {
  198. None => true,
  199. Some(file) => !globset.is_match(file),
  200. })
  201. .collect();
  202. } else {
  203. page.assets = assets;
  204. }
  205. page.serialized_assets = page.serialize_assets();
  206. } else {
  207. page.assets = vec![];
  208. }
  209. Ok(page)
  210. }
  211. /// We need access to all pages url to render links relative to content
  212. /// so that can't happen at the same time as parsing
  213. pub fn render_markdown(
  214. &mut self,
  215. permalinks: &HashMap<String, String>,
  216. tera: &Tera,
  217. config: &Config,
  218. anchor_insert: InsertAnchor,
  219. ) -> Result<()> {
  220. let mut context =
  221. RenderContext::new(tera, config, &self.permalink, permalinks, anchor_insert);
  222. context.tera_context.insert("page", &SerializingPage::from_page_basic(self, None));
  223. let res = render_content(&self.raw_content, &context).map_err(|e| {
  224. Error::chain(format!("Failed to render content of {}", self.file.path.display()), e)
  225. })?;
  226. self.summary = res.summary_len.map(|l| res.body[0..l].to_owned());
  227. self.content = res.body;
  228. self.toc = res.toc;
  229. Ok(())
  230. }
  231. /// Renders the page using the default layout, unless specified in front-matter
  232. pub fn render_html(&self, tera: &Tera, config: &Config, library: &Library) -> Result<String> {
  233. let tpl_name = match self.meta.template {
  234. Some(ref l) => l,
  235. None => "page.html",
  236. };
  237. let mut context = TeraContext::new();
  238. context.insert("config", config);
  239. context.insert("current_url", &self.permalink);
  240. context.insert("current_path", &self.path);
  241. context.insert("page", &self.to_serialized(library));
  242. context.insert("lang", &self.lang);
  243. context.insert("toc", &self.toc);
  244. render_template(&tpl_name, tera, context, &config.theme).map_err(|e| {
  245. Error::chain(format!("Failed to render page '{}'", self.file.path.display()), e)
  246. })
  247. }
  248. /// Creates a vectors of asset URLs.
  249. fn serialize_assets(&self) -> Vec<String> {
  250. self.assets
  251. .iter()
  252. .filter_map(|asset| asset.file_name())
  253. .filter_map(|filename| filename.to_str())
  254. .map(|filename| self.path.clone() + filename)
  255. .collect()
  256. }
  257. pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
  258. SerializingPage::from_page(self, library)
  259. }
  260. pub fn to_serialized_basic<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
  261. SerializingPage::from_page_basic(self, Some(library))
  262. }
  263. }
  264. impl Default for Page {
  265. fn default() -> Page {
  266. Page {
  267. file: FileInfo::default(),
  268. meta: PageFrontMatter::default(),
  269. ancestors: vec![],
  270. raw_content: "".to_string(),
  271. assets: vec![],
  272. serialized_assets: vec![],
  273. content: "".to_string(),
  274. slug: "".to_string(),
  275. path: "".to_string(),
  276. components: vec![],
  277. permalink: "".to_string(),
  278. summary: None,
  279. earlier: None,
  280. later: None,
  281. lighter: None,
  282. heavier: None,
  283. toc: vec![],
  284. word_count: None,
  285. reading_time: None,
  286. lang: String::new(),
  287. translations: Vec::new(),
  288. }
  289. }
  290. }
  291. #[cfg(test)]
  292. mod tests {
  293. use std::collections::HashMap;
  294. use std::fs::{create_dir, File};
  295. use std::io::Write;
  296. use std::path::Path;
  297. use globset::{Glob, GlobSetBuilder};
  298. use tempfile::tempdir;
  299. use tera::Tera;
  300. use super::Page;
  301. use config::{Config, Language};
  302. use front_matter::InsertAnchor;
  303. #[test]
  304. fn test_can_parse_a_valid_page() {
  305. let content = r#"
  306. +++
  307. title = "Hello"
  308. description = "hey there"
  309. slug = "hello-world"
  310. +++
  311. Hello world"#;
  312. let res = Page::parse(Path::new("post.md"), content, &Config::default());
  313. assert!(res.is_ok());
  314. let mut page = res.unwrap();
  315. page.render_markdown(
  316. &HashMap::default(),
  317. &Tera::default(),
  318. &Config::default(),
  319. InsertAnchor::None,
  320. )
  321. .unwrap();
  322. assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
  323. assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
  324. assert_eq!(page.raw_content, "Hello world".to_string());
  325. assert_eq!(page.content, "<p>Hello world</p>\n".to_string());
  326. }
  327. #[test]
  328. fn test_can_make_url_from_sections_and_slug() {
  329. let content = r#"
  330. +++
  331. slug = "hello-world"
  332. +++
  333. Hello world"#;
  334. let mut conf = Config::default();
  335. conf.base_url = "http://hello.com/".to_string();
  336. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &conf);
  337. assert!(res.is_ok());
  338. let page = res.unwrap();
  339. assert_eq!(page.path, "posts/intro/hello-world/");
  340. assert_eq!(page.components, vec!["posts", "intro", "hello-world"]);
  341. assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world/");
  342. }
  343. #[test]
  344. fn can_make_url_from_slug_only() {
  345. let content = r#"
  346. +++
  347. slug = "hello-world"
  348. +++
  349. Hello world"#;
  350. let config = Config::default();
  351. let res = Page::parse(Path::new("start.md"), content, &config);
  352. assert!(res.is_ok());
  353. let page = res.unwrap();
  354. assert_eq!(page.path, "hello-world/");
  355. assert_eq!(page.components, vec!["hello-world"]);
  356. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  357. }
  358. #[test]
  359. fn can_make_url_from_path() {
  360. let content = r#"
  361. +++
  362. path = "hello-world"
  363. +++
  364. Hello world"#;
  365. let config = Config::default();
  366. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
  367. assert!(res.is_ok());
  368. let page = res.unwrap();
  369. assert_eq!(page.path, "hello-world/");
  370. assert_eq!(page.components, vec!["hello-world"]);
  371. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  372. }
  373. #[test]
  374. fn can_make_url_from_path_starting_slash() {
  375. let content = r#"
  376. +++
  377. path = "/hello-world"
  378. +++
  379. Hello world"#;
  380. let config = Config::default();
  381. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
  382. assert!(res.is_ok());
  383. let page = res.unwrap();
  384. assert_eq!(page.path, "hello-world/");
  385. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  386. }
  387. #[test]
  388. fn errors_on_invalid_front_matter_format() {
  389. // missing starting +++
  390. let content = r#"
  391. title = "Hello"
  392. description = "hey there"
  393. slug = "hello-world"
  394. +++
  395. Hello world"#;
  396. let res = Page::parse(Path::new("start.md"), content, &Config::default());
  397. assert!(res.is_err());
  398. }
  399. #[test]
  400. fn can_make_slug_from_non_slug_filename() {
  401. let config = Config::default();
  402. let res = Page::parse(Path::new(" file with space.md"), "+++\n+++", &config);
  403. assert!(res.is_ok());
  404. let page = res.unwrap();
  405. assert_eq!(page.slug, "file-with-space");
  406. assert_eq!(page.permalink, config.make_permalink(&page.slug));
  407. }
  408. #[test]
  409. fn can_specify_summary() {
  410. let config = Config::default();
  411. let content = r#"
  412. +++
  413. +++
  414. Hello world
  415. <!-- more -->"#
  416. .to_string();
  417. let res = Page::parse(Path::new("hello.md"), &content, &config);
  418. assert!(res.is_ok());
  419. let mut page = res.unwrap();
  420. page.render_markdown(&HashMap::default(), &Tera::default(), &config, InsertAnchor::None)
  421. .unwrap();
  422. assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
  423. }
  424. #[test]
  425. fn page_with_assets_gets_right_info() {
  426. let tmp_dir = tempdir().expect("create temp dir");
  427. let path = tmp_dir.path();
  428. create_dir(&path.join("content")).expect("create content temp dir");
  429. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  430. let nested_path = path.join("content").join("posts").join("with-assets");
  431. create_dir(&nested_path).expect("create nested temp dir");
  432. let mut f = File::create(nested_path.join("index.md")).unwrap();
  433. f.write_all(b"+++\n+++\n").unwrap();
  434. File::create(nested_path.join("example.js")).unwrap();
  435. File::create(nested_path.join("graph.jpg")).unwrap();
  436. File::create(nested_path.join("fail.png")).unwrap();
  437. let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default());
  438. assert!(res.is_ok());
  439. let page = res.unwrap();
  440. assert_eq!(page.file.parent, path.join("content").join("posts"));
  441. assert_eq!(page.slug, "with-assets");
  442. assert_eq!(page.assets.len(), 3);
  443. assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
  444. }
  445. #[test]
  446. fn page_with_assets_and_slug_overrides_path() {
  447. let tmp_dir = tempdir().expect("create temp dir");
  448. let path = tmp_dir.path();
  449. create_dir(&path.join("content")).expect("create content temp dir");
  450. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  451. let nested_path = path.join("content").join("posts").join("with-assets");
  452. create_dir(&nested_path).expect("create nested temp dir");
  453. let mut f = File::create(nested_path.join("index.md")).unwrap();
  454. f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
  455. File::create(nested_path.join("example.js")).unwrap();
  456. File::create(nested_path.join("graph.jpg")).unwrap();
  457. File::create(nested_path.join("fail.png")).unwrap();
  458. let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default());
  459. assert!(res.is_ok());
  460. let page = res.unwrap();
  461. assert_eq!(page.file.parent, path.join("content").join("posts"));
  462. assert_eq!(page.slug, "hey");
  463. assert_eq!(page.assets.len(), 3);
  464. assert_eq!(page.permalink, "http://a-website.com/posts/hey/");
  465. }
  466. // https://github.com/getzola/zola/issues/607
  467. #[test]
  468. fn page_with_assets_and_date_in_folder_name() {
  469. let tmp_dir = tempdir().expect("create temp dir");
  470. let path = tmp_dir.path();
  471. create_dir(&path.join("content")).expect("create content temp dir");
  472. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  473. let nested_path = path.join("content").join("posts").join("2013-06-02_with-assets");
  474. create_dir(&nested_path).expect("create nested temp dir");
  475. let mut f = File::create(nested_path.join("index.md")).unwrap();
  476. f.write_all(b"+++\n\n+++\n").unwrap();
  477. File::create(nested_path.join("example.js")).unwrap();
  478. File::create(nested_path.join("graph.jpg")).unwrap();
  479. File::create(nested_path.join("fail.png")).unwrap();
  480. let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default());
  481. assert!(res.is_ok());
  482. let page = res.unwrap();
  483. assert_eq!(page.file.parent, path.join("content").join("posts"));
  484. assert_eq!(page.slug, "with-assets");
  485. assert_eq!(page.meta.date, Some("2013-06-02".to_string()));
  486. assert_eq!(page.assets.len(), 3);
  487. assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
  488. }
  489. #[test]
  490. fn page_with_ignored_assets_filters_out_correct_files() {
  491. let tmp_dir = tempdir().expect("create temp dir");
  492. let path = tmp_dir.path();
  493. create_dir(&path.join("content")).expect("create content temp dir");
  494. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  495. let nested_path = path.join("content").join("posts").join("with-assets");
  496. create_dir(&nested_path).expect("create nested temp dir");
  497. let mut f = File::create(nested_path.join("index.md")).unwrap();
  498. f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
  499. File::create(nested_path.join("example.js")).unwrap();
  500. File::create(nested_path.join("graph.jpg")).unwrap();
  501. File::create(nested_path.join("fail.png")).unwrap();
  502. let mut gsb = GlobSetBuilder::new();
  503. gsb.add(Glob::new("*.{js,png}").unwrap());
  504. let mut config = Config::default();
  505. config.ignored_content_globset = Some(gsb.build().unwrap());
  506. let res = Page::from_file(nested_path.join("index.md").as_path(), &config);
  507. assert!(res.is_ok());
  508. let page = res.unwrap();
  509. assert_eq!(page.assets.len(), 1);
  510. assert_eq!(page.assets[0].file_name().unwrap().to_str(), Some("graph.jpg"));
  511. }
  512. #[test]
  513. fn can_get_date_from_short_date_in_filename() {
  514. let config = Config::default();
  515. let content = r#"
  516. +++
  517. +++
  518. Hello world
  519. <!-- more -->"#
  520. .to_string();
  521. let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config);
  522. assert!(res.is_ok());
  523. let page = res.unwrap();
  524. assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
  525. assert_eq!(page.slug, "hello");
  526. }
  527. #[test]
  528. fn can_get_date_from_full_rfc3339_date_in_filename() {
  529. let config = Config::default();
  530. let content = r#"
  531. +++
  532. +++
  533. Hello world
  534. <!-- more -->"#
  535. .to_string();
  536. let res = Page::parse(Path::new("2018-10-02T15:00:00Z-hello.md"), &content, &config);
  537. assert!(res.is_ok());
  538. let page = res.unwrap();
  539. assert_eq!(page.meta.date, Some("2018-10-02T15:00:00Z".to_string()));
  540. assert_eq!(page.slug, "hello");
  541. }
  542. #[test]
  543. fn frontmatter_date_override_filename_date() {
  544. let config = Config::default();
  545. let content = r#"
  546. +++
  547. date = 2018-09-09
  548. +++
  549. Hello world
  550. <!-- more -->"#
  551. .to_string();
  552. let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config);
  553. assert!(res.is_ok());
  554. let page = res.unwrap();
  555. assert_eq!(page.meta.date, Some("2018-09-09".to_string()));
  556. assert_eq!(page.slug, "hello");
  557. }
  558. #[test]
  559. fn can_specify_language_in_filename() {
  560. let mut config = Config::default();
  561. config.languages.push(Language { code: String::from("fr"), rss: false });
  562. let content = r#"
  563. +++
  564. +++
  565. Bonjour le monde"#
  566. .to_string();
  567. let res = Page::parse(Path::new("hello.fr.md"), &content, &config);
  568. assert!(res.is_ok());
  569. let page = res.unwrap();
  570. assert_eq!(page.lang, "fr".to_string());
  571. assert_eq!(page.slug, "hello");
  572. assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
  573. }
  574. #[test]
  575. fn can_specify_language_in_filename_with_date() {
  576. let mut config = Config::default();
  577. config.languages.push(Language { code: String::from("fr"), rss: false });
  578. let content = r#"
  579. +++
  580. +++
  581. Bonjour le monde"#
  582. .to_string();
  583. let res = Page::parse(Path::new("2018-10-08_hello.fr.md"), &content, &config);
  584. assert!(res.is_ok());
  585. let page = res.unwrap();
  586. assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
  587. assert_eq!(page.lang, "fr".to_string());
  588. assert_eq!(page.slug, "hello");
  589. assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
  590. }
  591. #[test]
  592. fn i18n_frontmatter_path_overrides_default_permalink() {
  593. let mut config = Config::default();
  594. config.languages.push(Language { code: String::from("fr"), rss: false });
  595. let content = r#"
  596. +++
  597. path = "bonjour"
  598. +++
  599. Bonjour le monde"#
  600. .to_string();
  601. let res = Page::parse(Path::new("hello.fr.md"), &content, &config);
  602. assert!(res.is_ok());
  603. let page = res.unwrap();
  604. assert_eq!(page.lang, "fr".to_string());
  605. assert_eq!(page.slug, "hello");
  606. assert_eq!(page.permalink, "http://a-website.com/bonjour/");
  607. }
  608. }