Browse Source

Expose relative path of pages & sections

Closes #485
index-subcmd
Vincent Prouillet 5 years ago
parent
commit
83b04a561c
8 changed files with 23 additions and 4 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +3
    -0
      components/library/src/content/page.rs
  3. +3
    -0
      components/library/src/content/section.rs
  4. +4
    -1
      components/site/tests/site.rs
  5. +6
    -3
      docs/content/documentation/content/table-of-contents.md
  6. +4
    -0
      docs/content/documentation/templates/pages-sections.md
  7. +1
    -0
      test_site/templates/page.html
  8. +1
    -0
      test_site/templates/section.html

+ 1
- 0
CHANGELOG.md 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)



+ 3
- 0
components/library/src/content/page.rs 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,


+ 3
- 0
components/library/src/content/section.rs 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,


+ 4
- 1
components/site/tests/site.rs 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"));


+ 6
- 3
docs/content/documentation/content/table-of-contents.md 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.

+ 4
- 0
docs/content/documentation/templates/pages-sections.md 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


+ 1
- 0
test_site/templates/page.html 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 %}


+ 1
- 0
test_site/templates/section.html 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}}


Loading…
Cancel
Save