diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index b3ca3cd..cb1a0c4 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -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) diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index b8b12ad..380f5cd 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -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) diff --git a/components/library/src/content/ser.rs b/components/library/src/content/ser.rs index f28ccc0..4d7938d 100644 --- a/components/library/src/content/ser.rs +++ b/components/library/src/content/ser.rs @@ -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, + toc: &'a [Heading], word_count: Option, reading_time: Option, 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, path: &'a str, components: &'a [String], + toc: &'a [Heading], word_count: Option, reading_time: Option, lang: &'a str, @@ -244,6 +249,7 @@ impl<'a> SerializingSection<'a> { extra: §ion.meta.extra, path: §ion.path, components: §ion.components, + toc: §ion.toc, word_count: section.word_count, reading_time: section.reading_time, assets: §ion.serialized_assets, @@ -280,6 +286,7 @@ impl<'a> SerializingSection<'a> { extra: §ion.meta.extra, path: §ion.path, components: §ion.components, + toc: §ion.toc, word_count: section.word_count, reading_time: section.reading_time, assets: §ion.serialized_assets, diff --git a/docs/content/documentation/content/table-of-contents.md b/docs/content/documentation/content/table-of-contents.md index 5d2b35c..6e4d04e 100644 --- a/docs/content/documentation/content/table-of-contents.md +++ b/docs/content/documentation/content/table-of-contents.md @@ -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
    -{% for h1 in toc %} +{% for h1 in page.toc %}
  • {{ h1.title }} {% if h1.children %} diff --git a/docs/content/documentation/templates/pages-sections.md b/docs/content/documentation/templates/pages-sections.md index bbcf46a..d79bec5 100644 --- a/docs/content/documentation/templates/pages-sections.md +++ b/docs/content/documentation/templates/pages-sections.md @@ -27,6 +27,7 @@ permalink: String; summary: String?; taxonomies: HashMap>; extra: HashMap; +toc: Array
    , // 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; // 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; +toc: Array
    , // Unicode word count word_count: Number; // Based on https://help.medium.com/hc/en-us/articles/214991667-Read-time diff --git a/test_site/templates/page.html b/test_site/templates/page.html index d0e0f3e..e8cf68d 100644 --- a/test_site/templates/page.html +++ b/test_site/templates/page.html @@ -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 %}