From 705a30aa8d2e15a48df98944992e56d7d2ca83e2 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 9 Feb 2019 20:49:18 +0100 Subject: [PATCH] Move toc to be a rendering page/section variable level --- CHANGELOG.md | 2 ++ components/library/src/content/page.rs | 1 + components/library/src/content/section.rs | 1 + components/library/src/content/ser.rs | 6 ------ docs/content/documentation/templates/pages-sections.md | 6 +----- test_site/templates/page.html | 1 + 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbf56d5..becfe36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### Breaking - `earlier/later` and `lighter/heavier` are not set anymore on pages when rendering a section +- The table of content for a page/section is now only available as the `toc` variable when +rendering it and not anymore on the `page`/`section` variable ### Other - Add support for content in multiple languages diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index c344d50..8383e5b 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -270,6 +270,7 @@ 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 3da005e..84166e8 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -189,6 +189,7 @@ 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 e8f311b..83388d6 100644 --- a/components/library/src/content/ser.rs +++ b/components/library/src/content/ser.rs @@ -67,7 +67,6 @@ pub struct SerializingPage<'a> { summary: &'a Option, word_count: Option, reading_time: Option, - toc: &'a [Header], assets: &'a [String], draft: bool, lang: &'a str, @@ -129,7 +128,6 @@ impl<'a> SerializingPage<'a> { summary: &page.summary, word_count: page.word_count, reading_time: page.reading_time, - toc: &page.toc, assets: &page.serialized_assets, draft: page.is_draft(), lang: &page.lang, @@ -185,7 +183,6 @@ impl<'a> SerializingPage<'a> { summary: &page.summary, word_count: page.word_count, reading_time: page.reading_time, - toc: &page.toc, assets: &page.serialized_assets, draft: page.is_draft(), lang: &page.lang, @@ -212,7 +209,6 @@ pub struct SerializingSection<'a> { word_count: Option, reading_time: Option, lang: &'a str, - toc: &'a [Header], assets: &'a [String], pages: Vec>, subsections: Vec<&'a str>, @@ -251,7 +247,6 @@ impl<'a> SerializingSection<'a> { components: §ion.components, word_count: section.word_count, reading_time: section.reading_time, - toc: §ion.toc, assets: §ion.serialized_assets, lang: §ion.lang, pages, @@ -290,7 +285,6 @@ impl<'a> SerializingSection<'a> { components: §ion.components, word_count: section.word_count, reading_time: section.reading_time, - toc: §ion.toc, assets: §ion.serialized_assets, lang: §ion.lang, pages: vec![], diff --git a/docs/content/documentation/templates/pages-sections.md b/docs/content/documentation/templates/pages-sections.md index c21ed77..c1402d4 100644 --- a/docs/content/documentation/templates/pages-sections.md +++ b/docs/content/documentation/templates/pages-sections.md @@ -39,8 +39,6 @@ later: Page?; // and only set when rendering the page itself heavier: Page?; lighter: Page?; -// See the Table of contents section below for more details -toc: Array
; // Year/month/day is only set if the page has a date and month/day are 1-indexed year: Number?; month: Number?; @@ -89,8 +87,6 @@ subsections: Array; word_count: Number; // Based on https://help.medium.com/hc/en-us/articles/214991667-Read-time reading_time: Number; -// See the Table of contents section below for more details -toc: Array
; // Paths of colocated assets, relative to the content directory assets: Array; // The relative paths of the parent sections until the index onef for use with the `get_section` Tera function @@ -107,7 +103,7 @@ translations: Array; ## Table of contents -Both page and section have a `toc` field which corresponds to an array of `Header`. +Both page and section templates have a `toc` variable which corresponds to an array of `Header`. A `Header` has the following fields: ```ts diff --git a/test_site/templates/page.html b/test_site/templates/page.html index 275de86..d0e0f3e 100644 --- a/test_site/templates/page.html +++ b/test_site/templates/page.html @@ -3,6 +3,7 @@ {% block content %} {{ page.content | safe }} {{ page.relative_path | safe }} + {{ toc }} {% if page.earlier %}Previous article: {{ page.earlier.permalink }}{% endif %} {% if page.later %}Next article: {{ page.later.permalink }}{% endif %}