Browse Source

Let toc is visable through Page & Section variables in templates (#818)

* Let toc is visable through Page & Section variables in templates

* Removed the current toc variable from page & section
index-subcmd
Bob Vincent Prouillet 5 years ago
parent
commit
4aa2ba84fc
6 changed files with 12 additions and 5 deletions
  1. +0
    -1
      components/library/src/content/page.rs
  2. +0
    -1
      components/library/src/content/section.rs
  3. +7
    -0
      components/library/src/content/ser.rs
  4. +2
    -2
      docs/content/documentation/content/table-of-contents.md
  5. +2
    -0
      docs/content/documentation/templates/pages-sections.md
  6. +1
    -1
      test_site/templates/page.html

+ 0
- 1
components/library/src/content/page.rs View File

@@ -290,7 +290,6 @@ impl Page {
context.insert("current_path", &self.path); context.insert("current_path", &self.path);
context.insert("page", &self.to_serialized(library)); context.insert("page", &self.to_serialized(library));
context.insert("lang", &self.lang); context.insert("lang", &self.lang);
context.insert("toc", &self.toc);


render_template(&tpl_name, tera, context, &config.theme).map_err(|e| { render_template(&tpl_name, tera, context, &config.theme).map_err(|e| {
Error::chain(format!("Failed to render page '{}'", self.file.path.display()), e) Error::chain(format!("Failed to render page '{}'", self.file.path.display()), e)


+ 0
- 1
components/library/src/content/section.rs View File

@@ -219,7 +219,6 @@ impl Section {
context.insert("current_path", &self.path); context.insert("current_path", &self.path);
context.insert("section", &self.to_serialized(library)); context.insert("section", &self.to_serialized(library));
context.insert("lang", &self.lang); context.insert("lang", &self.lang);
context.insert("toc", &self.toc);


render_template(tpl_name, tera, context, &config.theme).map_err(|e| { render_template(tpl_name, tera, context, &config.theme).map_err(|e| {
Error::chain(format!("Failed to render section '{}'", self.file.path.display()), e) Error::chain(format!("Failed to render section '{}'", self.file.path.display()), e)


+ 7
- 0
components/library/src/content/ser.rs View File

@@ -5,6 +5,7 @@ use tera::{Map, Value};


use content::{Page, Section}; use content::{Page, Section};
use library::Library; use library::Library;
use rendering::Heading;


#[derive(Clone, Debug, PartialEq, Serialize)] #[derive(Clone, Debug, PartialEq, Serialize)]
pub struct TranslatedContent<'a> { pub struct TranslatedContent<'a> {
@@ -64,6 +65,7 @@ pub struct SerializingPage<'a> {
path: &'a str, path: &'a str,
components: &'a [String], components: &'a [String],
summary: &'a Option<String>, summary: &'a Option<String>,
toc: &'a [Heading],
word_count: Option<usize>, word_count: Option<usize>,
reading_time: Option<usize>, reading_time: Option<usize>,
assets: &'a [String], assets: &'a [String],
@@ -125,6 +127,7 @@ impl<'a> SerializingPage<'a> {
path: &page.path, path: &page.path,
components: &page.components, components: &page.components,
summary: &page.summary, summary: &page.summary,
toc: &page.toc,
word_count: page.word_count, word_count: page.word_count,
reading_time: page.reading_time, reading_time: page.reading_time,
assets: &page.serialized_assets, assets: &page.serialized_assets,
@@ -180,6 +183,7 @@ impl<'a> SerializingPage<'a> {
path: &page.path, path: &page.path,
components: &page.components, components: &page.components,
summary: &page.summary, summary: &page.summary,
toc: &page.toc,
word_count: page.word_count, word_count: page.word_count,
reading_time: page.reading_time, reading_time: page.reading_time,
assets: &page.serialized_assets, assets: &page.serialized_assets,
@@ -205,6 +209,7 @@ pub struct SerializingSection<'a> {
extra: &'a HashMap<String, Value>, extra: &'a HashMap<String, Value>,
path: &'a str, path: &'a str,
components: &'a [String], components: &'a [String],
toc: &'a [Heading],
word_count: Option<usize>, word_count: Option<usize>,
reading_time: Option<usize>, reading_time: Option<usize>,
lang: &'a str, lang: &'a str,
@@ -244,6 +249,7 @@ impl<'a> SerializingSection<'a> {
extra: &section.meta.extra, extra: &section.meta.extra,
path: &section.path, path: &section.path,
components: &section.components, components: &section.components,
toc: &section.toc,
word_count: section.word_count, word_count: section.word_count,
reading_time: section.reading_time, reading_time: section.reading_time,
assets: &section.serialized_assets, assets: &section.serialized_assets,
@@ -280,6 +286,7 @@ impl<'a> SerializingSection<'a> {
extra: &section.meta.extra, extra: &section.meta.extra,
path: &section.path, path: &section.path,
components: &section.components, components: &section.components,
toc: &section.toc,
word_count: section.word_count, word_count: section.word_count,
reading_time: section.reading_time, reading_time: section.reading_time,
assets: &section.serialized_assets, assets: &section.serialized_assets,


+ 2
- 2
docs/content/documentation/content/table-of-contents.md View File

@@ -5,7 +5,7 @@ weight = 60


Each page/section will automatically generate a table of contents for itself based on the headers present. Each page/section will automatically generate a table of contents for itself based on the headers present.


It is available in the template through the `toc` variable.
It is available in the template through the `page.toc` or `section.toc` variable.
You can view the [template variables](@/documentation/templates/pages-sections.md#table-of-contents) You can view the [template variables](@/documentation/templates/pages-sections.md#table-of-contents)
documentation for information on its structure. documentation for information on its structure.


@@ -13,7 +13,7 @@ Here is an example of using that field to render a 2-level table of contents:


```jinja2 ```jinja2
<ul> <ul>
{% for h1 in toc %}
{% for h1 in page.toc %}
<li> <li>
<a href="{{h1.permalink | safe}}">{{ h1.title }}</a> <a href="{{h1.permalink | safe}}">{{ h1.title }}</a>
{% if h1.children %} {% if h1.children %}


+ 2
- 0
docs/content/documentation/templates/pages-sections.md View File

@@ -27,6 +27,7 @@ permalink: String;
summary: String?; summary: String?;
taxonomies: HashMap<String, Array<String>>; taxonomies: HashMap<String, Array<String>>;
extra: HashMap<String, Any>; extra: HashMap<String, Any>;
toc: Array<Header>,
// Naive word count, will not work for languages without whitespace // Naive word count, will not work for languages without whitespace
word_count: Number; word_count: Number;
// Based on https://help.medium.com/hc/en-us/articles/214991667-Read-time // Based on https://help.medium.com/hc/en-us/articles/214991667-Read-time
@@ -81,6 +82,7 @@ pages: Array<Page>;
// This only contains the path to use in the `get_section` Tera function to get // This only contains the path to use in the `get_section` Tera function to get
// the actual section object if you need it // the actual section object if you need it
subsections: Array<String>; subsections: Array<String>;
toc: Array<Header>,
// Unicode word count // Unicode word count
word_count: Number; word_count: Number;
// Based on https://help.medium.com/hc/en-us/articles/214991667-Read-time // Based on https://help.medium.com/hc/en-us/articles/214991667-Read-time


+ 1
- 1
test_site/templates/page.html View File

@@ -3,7 +3,7 @@
{% block content %} {% block content %}
{{ page.content | safe }} {{ page.content | safe }}
{{ page.relative_path | safe }} {{ page.relative_path | safe }}
{{ toc }}
{{ page.toc }}


{% if page.earlier %}Previous article: {{ page.earlier.permalink }}{% endif %} {% if page.earlier %}Previous article: {{ page.earlier.permalink }}{% endif %}
{% if page.later %}Next article: {{ page.later.permalink }}{% endif %} {% if page.later %}Next article: {{ page.later.permalink }}{% endif %}


Loading…
Cancel
Save