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
This commit is contained in:
Bob 2019-10-14 21:12:19 +08:00 committed by Vincent Prouillet
parent b1ceb3e80e
commit 4aa2ba84fc
6 changed files with 12 additions and 5 deletions

View file

@ -290,7 +290,6 @@ impl Page {
context.insert("current_path", &self.path);
context.insert("page", &self.to_serialized(library));
context.insert("lang", &self.lang);
context.insert("toc", &self.toc);
render_template(&tpl_name, tera, context, &config.theme).map_err(|e| {
Error::chain(format!("Failed to render page '{}'", self.file.path.display()), e)

View file

@ -219,7 +219,6 @@ impl Section {
context.insert("current_path", &self.path);
context.insert("section", &self.to_serialized(library));
context.insert("lang", &self.lang);
context.insert("toc", &self.toc);
render_template(tpl_name, tera, context, &config.theme).map_err(|e| {
Error::chain(format!("Failed to render section '{}'", self.file.path.display()), e)

View file

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

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.
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)
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
<ul>
{% for h1 in toc %}
{% for h1 in page.toc %}
<li>
<a href="{{h1.permalink | safe}}">{{ h1.title }}</a>
{% if h1.children %}

View file

@ -27,6 +27,7 @@ permalink: String;
summary: String?;
taxonomies: HashMap<String, Array<String>>;
extra: HashMap<String, Any>;
toc: Array<Header>,
// Naive word count, will not work for languages without whitespace
word_count: Number;
// 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
// the actual section object if you need it
subsections: Array<String>;
toc: Array<Header>,
// Unicode word count
word_count: Number;
// Based on https://help.medium.com/hc/en-us/articles/214991667-Read-time

View file

@ -3,7 +3,7 @@
{% block content %}
{{ page.content | safe }}
{{ page.relative_path | safe }}
{{ toc }}
{{ page.toc }}
{% if page.earlier %}Previous article: {{ page.earlier.permalink }}{% endif %}
{% if page.later %}Next article: {{ page.later.permalink }}{% endif %}