From 83b04a561c0718a394b023dbf99b607a48c406a4 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 18 Oct 2018 18:00:39 +0200 Subject: [PATCH] Expose relative path of pages & sections Closes #485 --- CHANGELOG.md | 1 + components/library/src/content/page.rs | 3 +++ components/library/src/content/section.rs | 3 +++ components/site/tests/site.rs | 5 ++++- docs/content/documentation/content/table-of-contents.md | 9 ++++++--- docs/content/documentation/templates/pages-sections.md | 4 ++++ test_site/templates/page.html | 1 + test_site/templates/section.html | 1 + 8 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c068946d..e5e9ba41 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 ca83c7ba..403a43e4 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 5f0a58f5..efd788d8 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 9e714d5d..65016060 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 45d52152..04d648b6 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 df900457..4ea519f3 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 8f4373af..275de869 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 a8d0d9a4..724795a6 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}}