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.

674 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. render_template(&tpl_name, tera, context, &config.theme).map_err(|e| {
  244. Error::chain(format!("Failed to render page '{}'", self.file.path.display()), e)
  245. })
  246. }
  247. /// Creates a vectors of asset URLs.
  248. fn serialize_assets(&self) -> Vec<String> {
  249. self.assets
  250. .iter()
  251. .filter_map(|asset| asset.file_name())
  252. .filter_map(|filename| filename.to_str())
  253. .map(|filename| self.path.clone() + filename)
  254. .collect()
  255. }
  256. pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
  257. SerializingPage::from_page(self, library)
  258. }
  259. pub fn to_serialized_basic<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
  260. SerializingPage::from_page_basic(self, Some(library))
  261. }
  262. }
  263. impl Default for Page {
  264. fn default() -> Page {
  265. Page {
  266. file: FileInfo::default(),
  267. meta: PageFrontMatter::default(),
  268. ancestors: vec![],
  269. raw_content: "".to_string(),
  270. assets: vec![],
  271. serialized_assets: vec![],
  272. content: "".to_string(),
  273. slug: "".to_string(),
  274. path: "".to_string(),
  275. components: vec![],
  276. permalink: "".to_string(),
  277. summary: None,
  278. earlier: None,
  279. later: None,
  280. lighter: None,
  281. heavier: None,
  282. toc: vec![],
  283. word_count: None,
  284. reading_time: None,
  285. lang: String::new(),
  286. translations: Vec::new(),
  287. }
  288. }
  289. }
  290. #[cfg(test)]
  291. mod tests {
  292. use std::collections::HashMap;
  293. use std::fs::{create_dir, File};
  294. use std::io::Write;
  295. use std::path::Path;
  296. use globset::{Glob, GlobSetBuilder};
  297. use tempfile::tempdir;
  298. use tera::Tera;
  299. use super::Page;
  300. use config::{Config, Language};
  301. use front_matter::InsertAnchor;
  302. #[test]
  303. fn test_can_parse_a_valid_page() {
  304. let content = r#"
  305. +++
  306. title = "Hello"
  307. description = "hey there"
  308. slug = "hello-world"
  309. +++
  310. Hello world"#;
  311. let res = Page::parse(Path::new("post.md"), content, &Config::default());
  312. assert!(res.is_ok());
  313. let mut page = res.unwrap();
  314. page.render_markdown(
  315. &HashMap::default(),
  316. &Tera::default(),
  317. &Config::default(),
  318. InsertAnchor::None,
  319. )
  320. .unwrap();
  321. assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
  322. assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
  323. assert_eq!(page.raw_content, "Hello world".to_string());
  324. assert_eq!(page.content, "<p>Hello world</p>\n".to_string());
  325. }
  326. #[test]
  327. fn test_can_make_url_from_sections_and_slug() {
  328. let content = r#"
  329. +++
  330. slug = "hello-world"
  331. +++
  332. Hello world"#;
  333. let mut conf = Config::default();
  334. conf.base_url = "http://hello.com/".to_string();
  335. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &conf);
  336. assert!(res.is_ok());
  337. let page = res.unwrap();
  338. assert_eq!(page.path, "posts/intro/hello-world/");
  339. assert_eq!(page.components, vec!["posts", "intro", "hello-world"]);
  340. assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world/");
  341. }
  342. #[test]
  343. fn can_make_url_from_slug_only() {
  344. let content = r#"
  345. +++
  346. slug = "hello-world"
  347. +++
  348. Hello world"#;
  349. let config = Config::default();
  350. let res = Page::parse(Path::new("start.md"), content, &config);
  351. assert!(res.is_ok());
  352. let page = res.unwrap();
  353. assert_eq!(page.path, "hello-world/");
  354. assert_eq!(page.components, vec!["hello-world"]);
  355. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  356. }
  357. #[test]
  358. fn can_make_url_from_path() {
  359. let content = r#"
  360. +++
  361. path = "hello-world"
  362. +++
  363. Hello world"#;
  364. let config = Config::default();
  365. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
  366. assert!(res.is_ok());
  367. let page = res.unwrap();
  368. assert_eq!(page.path, "hello-world/");
  369. assert_eq!(page.components, vec!["hello-world"]);
  370. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  371. }
  372. #[test]
  373. fn can_make_url_from_path_starting_slash() {
  374. let content = r#"
  375. +++
  376. path = "/hello-world"
  377. +++
  378. Hello world"#;
  379. let config = Config::default();
  380. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
  381. assert!(res.is_ok());
  382. let page = res.unwrap();
  383. assert_eq!(page.path, "hello-world/");
  384. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  385. }
  386. #[test]
  387. fn errors_on_invalid_front_matter_format() {
  388. // missing starting +++
  389. let content = r#"
  390. title = "Hello"
  391. description = "hey there"
  392. slug = "hello-world"
  393. +++
  394. Hello world"#;
  395. let res = Page::parse(Path::new("start.md"), content, &Config::default());
  396. assert!(res.is_err());
  397. }
  398. #[test]
  399. fn can_make_slug_from_non_slug_filename() {
  400. let config = Config::default();
  401. let res = Page::parse(Path::new(" file with space.md"), "+++\n+++", &config);
  402. assert!(res.is_ok());
  403. let page = res.unwrap();
  404. assert_eq!(page.slug, "file-with-space");
  405. assert_eq!(page.permalink, config.make_permalink(&page.slug));
  406. }
  407. #[test]
  408. fn can_specify_summary() {
  409. let config = Config::default();
  410. let content = r#"
  411. +++
  412. +++
  413. Hello world
  414. <!-- more -->"#
  415. .to_string();
  416. let res = Page::parse(Path::new("hello.md"), &content, &config);
  417. assert!(res.is_ok());
  418. let mut page = res.unwrap();
  419. page.render_markdown(&HashMap::default(), &Tera::default(), &config, InsertAnchor::None)
  420. .unwrap();
  421. assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
  422. }
  423. #[test]
  424. fn page_with_assets_gets_right_info() {
  425. let tmp_dir = tempdir().expect("create temp dir");
  426. let path = tmp_dir.path();
  427. create_dir(&path.join("content")).expect("create content temp dir");
  428. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  429. let nested_path = path.join("content").join("posts").join("with-assets");
  430. create_dir(&nested_path).expect("create nested temp dir");
  431. let mut f = File::create(nested_path.join("index.md")).unwrap();
  432. f.write_all(b"+++\n+++\n").unwrap();
  433. File::create(nested_path.join("example.js")).unwrap();
  434. File::create(nested_path.join("graph.jpg")).unwrap();
  435. File::create(nested_path.join("fail.png")).unwrap();
  436. let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default());
  437. assert!(res.is_ok());
  438. let page = res.unwrap();
  439. assert_eq!(page.file.parent, path.join("content").join("posts"));
  440. assert_eq!(page.slug, "with-assets");
  441. assert_eq!(page.assets.len(), 3);
  442. assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
  443. }
  444. #[test]
  445. fn page_with_assets_and_slug_overrides_path() {
  446. let tmp_dir = tempdir().expect("create temp dir");
  447. let path = tmp_dir.path();
  448. create_dir(&path.join("content")).expect("create content temp dir");
  449. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  450. let nested_path = path.join("content").join("posts").join("with-assets");
  451. create_dir(&nested_path).expect("create nested temp dir");
  452. let mut f = File::create(nested_path.join("index.md")).unwrap();
  453. f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
  454. File::create(nested_path.join("example.js")).unwrap();
  455. File::create(nested_path.join("graph.jpg")).unwrap();
  456. File::create(nested_path.join("fail.png")).unwrap();
  457. let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default());
  458. assert!(res.is_ok());
  459. let page = res.unwrap();
  460. assert_eq!(page.file.parent, path.join("content").join("posts"));
  461. assert_eq!(page.slug, "hey");
  462. assert_eq!(page.assets.len(), 3);
  463. assert_eq!(page.permalink, "http://a-website.com/posts/hey/");
  464. }
  465. // https://github.com/getzola/zola/issues/607
  466. #[test]
  467. fn page_with_assets_and_date_in_folder_name() {
  468. let tmp_dir = tempdir().expect("create temp dir");
  469. let path = tmp_dir.path();
  470. create_dir(&path.join("content")).expect("create content temp dir");
  471. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  472. let nested_path = path.join("content").join("posts").join("2013-06-02_with-assets");
  473. create_dir(&nested_path).expect("create nested temp dir");
  474. let mut f = File::create(nested_path.join("index.md")).unwrap();
  475. f.write_all(b"+++\n\n+++\n").unwrap();
  476. File::create(nested_path.join("example.js")).unwrap();
  477. File::create(nested_path.join("graph.jpg")).unwrap();
  478. File::create(nested_path.join("fail.png")).unwrap();
  479. let res = Page::from_file(nested_path.join("index.md").as_path(), &Config::default());
  480. assert!(res.is_ok());
  481. let page = res.unwrap();
  482. assert_eq!(page.file.parent, path.join("content").join("posts"));
  483. assert_eq!(page.slug, "with-assets");
  484. assert_eq!(page.meta.date, Some("2013-06-02".to_string()));
  485. assert_eq!(page.assets.len(), 3);
  486. assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
  487. }
  488. #[test]
  489. fn page_with_ignored_assets_filters_out_correct_files() {
  490. let tmp_dir = tempdir().expect("create temp dir");
  491. let path = tmp_dir.path();
  492. create_dir(&path.join("content")).expect("create content temp dir");
  493. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  494. let nested_path = path.join("content").join("posts").join("with-assets");
  495. create_dir(&nested_path).expect("create nested temp dir");
  496. let mut f = File::create(nested_path.join("index.md")).unwrap();
  497. f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
  498. File::create(nested_path.join("example.js")).unwrap();
  499. File::create(nested_path.join("graph.jpg")).unwrap();
  500. File::create(nested_path.join("fail.png")).unwrap();
  501. let mut gsb = GlobSetBuilder::new();
  502. gsb.add(Glob::new("*.{js,png}").unwrap());
  503. let mut config = Config::default();
  504. config.ignored_content_globset = Some(gsb.build().unwrap());
  505. let res = Page::from_file(nested_path.join("index.md").as_path(), &config);
  506. assert!(res.is_ok());
  507. let page = res.unwrap();
  508. assert_eq!(page.assets.len(), 1);
  509. assert_eq!(page.assets[0].file_name().unwrap().to_str(), Some("graph.jpg"));
  510. }
  511. #[test]
  512. fn can_get_date_from_short_date_in_filename() {
  513. let config = Config::default();
  514. let content = r#"
  515. +++
  516. +++
  517. Hello world
  518. <!-- more -->"#
  519. .to_string();
  520. let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config);
  521. assert!(res.is_ok());
  522. let page = res.unwrap();
  523. assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
  524. assert_eq!(page.slug, "hello");
  525. }
  526. #[test]
  527. fn can_get_date_from_full_rfc3339_date_in_filename() {
  528. let config = Config::default();
  529. let content = r#"
  530. +++
  531. +++
  532. Hello world
  533. <!-- more -->"#
  534. .to_string();
  535. let res = Page::parse(Path::new("2018-10-02T15:00:00Z-hello.md"), &content, &config);
  536. assert!(res.is_ok());
  537. let page = res.unwrap();
  538. assert_eq!(page.meta.date, Some("2018-10-02T15:00:00Z".to_string()));
  539. assert_eq!(page.slug, "hello");
  540. }
  541. #[test]
  542. fn frontmatter_date_override_filename_date() {
  543. let config = Config::default();
  544. let content = r#"
  545. +++
  546. date = 2018-09-09
  547. +++
  548. Hello world
  549. <!-- more -->"#
  550. .to_string();
  551. let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config);
  552. assert!(res.is_ok());
  553. let page = res.unwrap();
  554. assert_eq!(page.meta.date, Some("2018-09-09".to_string()));
  555. assert_eq!(page.slug, "hello");
  556. }
  557. #[test]
  558. fn can_specify_language_in_filename() {
  559. let mut config = Config::default();
  560. config.languages.push(Language { code: String::from("fr"), rss: false });
  561. let content = r#"
  562. +++
  563. +++
  564. Bonjour le monde"#
  565. .to_string();
  566. let res = Page::parse(Path::new("hello.fr.md"), &content, &config);
  567. assert!(res.is_ok());
  568. let page = res.unwrap();
  569. assert_eq!(page.lang, "fr".to_string());
  570. assert_eq!(page.slug, "hello");
  571. assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
  572. }
  573. #[test]
  574. fn can_specify_language_in_filename_with_date() {
  575. let mut config = Config::default();
  576. config.languages.push(Language { code: String::from("fr"), rss: false });
  577. let content = r#"
  578. +++
  579. +++
  580. Bonjour le monde"#
  581. .to_string();
  582. let res = Page::parse(Path::new("2018-10-08_hello.fr.md"), &content, &config);
  583. assert!(res.is_ok());
  584. let page = res.unwrap();
  585. assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
  586. assert_eq!(page.lang, "fr".to_string());
  587. assert_eq!(page.slug, "hello");
  588. assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
  589. }
  590. #[test]
  591. fn i18n_frontmatter_path_overrides_default_permalink() {
  592. let mut config = Config::default();
  593. config.languages.push(Language { code: String::from("fr"), rss: false });
  594. let content = r#"
  595. +++
  596. path = "bonjour"
  597. +++
  598. Bonjour le monde"#
  599. .to_string();
  600. let res = Page::parse(Path::new("hello.fr.md"), &content, &config);
  601. assert!(res.is_ok());
  602. let page = res.unwrap();
  603. assert_eq!(page.lang, "fr".to_string());
  604. assert_eq!(page.slug, "hello");
  605. assert_eq!(page.permalink, "http://a-website.com/bonjour/");
  606. }
  607. }