Expose relative path of pages & sections

Closes #485
This commit is contained in:
Vincent Prouillet 2018-10-18 18:00:39 +02:00
parent a86a6ae8c4
commit 83b04a561c
8 changed files with 23 additions and 4 deletions

View file

@ -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)

View file

@ -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,

View file

@ -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<String>,
@ -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: &section.file.relative,
ancestors,
content: &section.content,
permalink: &section.permalink,
@ -77,6 +79,7 @@ impl<'a> SerializingSection<'a> {
};
SerializingSection {
relative_path: &section.file.relative,
ancestors,
content: &section.content,
permalink: &section.permalink,

View file

@ -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"));

View file

@ -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.

View file

@ -49,6 +49,8 @@ assets: Array<String>;
// 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<String>;
// The relative path from the `content` directory to the markdown file
relative_path: String;
```
## Section variables
@ -89,6 +91,8 @@ assets: Array<String>;
// 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<String>;
// The relative path from the `content` directory to the markdown file
relative_path: String;
```
## Table of contents

View file

@ -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 %}

View file

@ -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}}