diff --git a/CHANGELOG.md b/CHANGELOG.md index c068946..e5e9ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ Tera function - Add `ancestors` to pages and sections pointing to the relative path of all ancestor sections up to the index to be used with the `get_section` Tera function - Add a `load_data` Tera function to load local CSV/TOML/JSON files +- Add `relative_path` to pages and sections in templates ## 0.4.2 (2018-09-03) diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index ca83c7b..403a43e 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -20,6 +20,7 @@ use content::file_info::FileInfo; /// What we are sending to the templates when rendering them #[derive(Clone, Debug, PartialEq, Serialize)] pub struct SerializingPage<'a> { + relative_path: &'a str, content: &'a str, permalink: &'a str, slug: &'a str, @@ -65,6 +66,7 @@ impl<'a> SerializingPage<'a> { let ancestors = page.ancestors.iter().map(|k| library.get_section_by_key(*k).file.relative.clone()).collect(); SerializingPage { + relative_path: &page.file.relative, ancestors, content: &page.content, permalink: &page.permalink, @@ -109,6 +111,7 @@ impl<'a> SerializingPage<'a> { }; SerializingPage { + relative_path: &page.file.relative, ancestors, content: &page.content, permalink: &page.permalink, diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index 5f0a58f..efd788d 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -19,6 +19,7 @@ use library::Library; #[derive(Clone, Debug, PartialEq, Serialize)] pub struct SerializingSection<'a> { + relative_path: &'a str, content: &'a str, permalink: &'a str, ancestors: Vec, @@ -51,6 +52,7 @@ impl<'a> SerializingSection<'a> { let ancestors = section.ancestors.iter().map(|k| library.get_section_by_key(*k).file.relative.clone()).collect(); SerializingSection { + relative_path: §ion.file.relative, ancestors, content: §ion.content, permalink: §ion.permalink, @@ -77,6 +79,7 @@ impl<'a> SerializingSection<'a> { }; SerializingSection { + relative_path: §ion.file.relative, ancestors, content: §ion.content, permalink: §ion.permalink, diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 9e714d5..6501606 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -142,7 +142,10 @@ fn can_build_site_without_live_reload() { assert!(file_exists!(public, "posts/tutorials/programming/index.html")); // Ensure subsection pages are correctly filled assert!(file_contains!(public, "posts/tutorials/index.html", "Sub-pages: 2")); - // TODO: add assertion for syntax highlighting + + // Pages and section get their relative path + assert!(file_contains!(public, "posts/tutorials/index.html", "posts/tutorials/_index.md")); + assert!(file_contains!(public, "posts/tutorials/devops/nix/index.html", "posts/tutorials/devops/nix.md")); // aliases work assert!(file_exists!(public, "an-old-url/old-page/index.html")); diff --git a/docs/content/documentation/content/table-of-contents.md b/docs/content/documentation/content/table-of-contents.md index 45d5215..04d648b 100644 --- a/docs/content/documentation/content/table-of-contents.md +++ b/docs/content/documentation/content/table-of-contents.md @@ -3,10 +3,10 @@ title = "Table of Contents" weight = 60 +++ -Each page/section will automatically generate a table of content for itself based on the headers present. +Each page/section will automatically generate a table of content for itself based on the headers present. -It is available in the template through `section.toc` and `page.toc`. -You can view the [template variables](./documentation/templates/pages-sections.md#table-of-contents) +It is available in the template through `section.toc` and `page.toc`. +You can view the [template variables](./documentation/templates/pages-sections.md#table-of-contents) documentation for information on its structure. Here is an example of using that field to render a 2-level table of content: @@ -31,3 +31,6 @@ Here is an example of using that field to render a 2-level table of content: ``` While headers are neatly ordered in that example, it will work just as well with disjoint headers. + +Note that all existing HTML tags from the title will NOT be present in the table of contents to +avoid various issues. diff --git a/docs/content/documentation/templates/pages-sections.md b/docs/content/documentation/templates/pages-sections.md index df90045..4ea519f 100644 --- a/docs/content/documentation/templates/pages-sections.md +++ b/docs/content/documentation/templates/pages-sections.md @@ -49,6 +49,8 @@ assets: Array; // The first item is the index section and the last one is the parent section // This is filled after rendering a page content so it will be empty in shortcodes ancestors: Array; +// The relative path from the `content` directory to the markdown file +relative_path: String; ``` ## Section variables @@ -89,6 +91,8 @@ assets: Array; // The first item is the index section and the last one is the parent section // This is filled after rendering a page content so it will be empty in shortcodes ancestors: Array; +// The relative path from the `content` directory to the markdown file +relative_path: String; ``` ## Table of contents diff --git a/test_site/templates/page.html b/test_site/templates/page.html index 8f4373a..275de86 100644 --- a/test_site/templates/page.html +++ b/test_site/templates/page.html @@ -2,6 +2,7 @@ {% block content %} {{ page.content | safe }} + {{ page.relative_path | safe }} {% if page.earlier %}Previous article: {{ page.earlier.permalink }}{% endif %} {% if page.later %}Next article: {{ page.later.permalink }}{% endif %} diff --git a/test_site/templates/section.html b/test_site/templates/section.html index a8d0d9a..724795a 100644 --- a/test_site/templates/section.html +++ b/test_site/templates/section.html @@ -4,6 +4,7 @@ {% for page in section.pages %} {{page.title}} {% endfor %} + {{ section.relative_path | safe }} {% for sub in section.subsections %} {% set subsection = get_section(path=sub) %} {{subsection.title}}