From 9f3efd79211e152f3c38c18047062ce01364f2db Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Sun, 12 Jan 2025 15:47:26 -0800 Subject: [PATCH] Add `insert_anchor_links` back to `config.toml` `insert_anchor_links` was a valid `config.toml` setting before [0.0.6] but was changed to a section option. [0.0.6]: https://github.com/getzola/zola/blob/master/CHANGELOG.md#006-2017-05-24 It was mentioned in the documentation for `config.toml` until 2021 (#1477). In 2021, @Keats said that "`insert_anchor_links` could probably be a config option in `config.toml`": https://github.com/getzola/zola/issues/634#issuecomment-882775151 In the same thread, several other users requested this feature. @Artoria2e5 said: > These sort of settings become annoying to copy-paste when a site ends > up with many sections. @ctron said: > But even more annoying is that it seems I can't add > insert_anchor_links on the top section, but need to place it in every > section. > > I think having this as a default configuration for the site makes > total sense. --- components/config/src/config/markup.rs | 5 ++++ .../content/src/front_matter/section.rs | 4 +-- components/content/src/section.rs | 2 +- components/site/src/lib.rs | 13 ++++++---- components/site/tests/site.rs | 26 +++++++++++++++++++ docs/content/documentation/content/linking.md | 2 +- .../getting-started/configuration.md | 8 ++++++ test_site/content/_index.md | 2 ++ test_site/templates/index.html | 2 ++ test_site/templates/section.html | 2 +- .../templates/section-specific-extends.html | 1 + 11 files changed, 57 insertions(+), 10 deletions(-) diff --git a/components/config/src/config/markup.rs b/components/config/src/config/markup.rs index 318524b24f..0a875eb7e7 100644 --- a/components/config/src/config/markup.rs +++ b/components/config/src/config/markup.rs @@ -8,6 +8,7 @@ use libs::syntect::{ use serde::{Deserialize, Serialize}; use errors::{bail, Result}; +use utils::types::InsertAnchor; use crate::highlighting::{CLASS_STYLE, THEME_SET}; @@ -59,6 +60,9 @@ pub struct Markdown { pub extra_theme_set: Arc>, /// Add loading="lazy" decoding="async" to img tags. When turned on, the alt text must be plain text. Defaults to false pub lazy_async_image: bool, + /// Whether to insert a link for each header like the ones you can see in this site if you hover one + /// The default template can be overridden by creating a `anchor-link.html` in the `templates` directory + pub insert_anchor_links: InsertAnchor, } impl Markdown { @@ -232,6 +236,7 @@ impl Default for Markdown { extra_syntax_set: None, extra_theme_set: Arc::new(None), lazy_async_image: false, + insert_anchor_links: InsertAnchor::None, } } } diff --git a/components/content/src/front_matter/section.rs b/components/content/src/front_matter/section.rs index bed9cd0758..2eea5dd006 100644 --- a/components/content/src/front_matter/section.rs +++ b/components/content/src/front_matter/section.rs @@ -41,7 +41,7 @@ pub struct SectionFrontMatter { pub paginate_path: String, /// Whether to insert a link for each header like the ones you can see in this site if you hover one /// The default template can be overridden by creating a `anchor-link.html` in the `templates` directory - pub insert_anchor_links: InsertAnchor, + pub insert_anchor_links: Option, /// Whether to render that section or not. Defaults to `true`. /// Useful when the section is only there to organize things but is not meant /// to be used directly, like a posts section in a personal site @@ -108,7 +108,7 @@ impl Default for SectionFrontMatter { paginate_path: DEFAULT_PAGINATE_PATH.to_string(), render: true, redirect_to: None, - insert_anchor_links: InsertAnchor::None, + insert_anchor_links: None, in_search_index: true, transparent: false, page_template: None, diff --git a/components/content/src/section.rs b/components/content/src/section.rs index d4c84a9bd3..69a22a03d4 100644 --- a/components/content/src/section.rs +++ b/components/content/src/section.rs @@ -157,7 +157,7 @@ impl Section { &self.lang, &self.permalink, permalinks, - self.meta.insert_anchor_links, + self.meta.insert_anchor_links.unwrap_or(config.markdown.insert_anchor_links), ); context.set_shortcode_definitions(shortcode_definitions); context.set_current_page_path(&self.file.relative); diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index cd48c219e4..5fc684f911 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -539,7 +539,7 @@ impl Site { } /// Finds the insert_anchor for the parent section of the directory at `path`. - /// Defaults to `AnchorInsert::None` if no parent section found + /// Defaults to the global setting if no parent section found pub fn find_parent_section_insert_anchor( &self, parent_path: &Path, @@ -550,10 +550,13 @@ impl Site { } else { parent_path.join("_index.md") }; - match self.library.read().unwrap().sections.get(&parent) { - Some(s) => s.meta.insert_anchor_links, - None => InsertAnchor::None, - } + self.library + .read() + .unwrap() + .sections + .get(&parent) + .and_then(|s| s.meta.insert_anchor_links) + .unwrap_or(self.config.markdown.insert_anchor_links) } /// Find out the direct subsections of each subsection if there are some diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index bf0811e15a..99d848ba29 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -10,6 +10,7 @@ use content::Page; use libs::ahash::AHashMap; use site::sitemap; use site::Site; +use utils::types::InsertAnchor; #[test] fn can_parse_site() { @@ -377,6 +378,31 @@ fn can_build_site_and_insert_anchor_links() { )); } +#[test] +fn can_build_site_insert_anchor_links_none_by_default() { + let (_, _tmp_dir, public) = build_site("test_site"); + + assert!(Path::new(&public).exists()); + // anchor link not inserted + assert!(file_contains!(public, "index.html", r#"

Heading 1

"#)); +} + +#[test] +fn can_build_site_and_insert_anchor_links_global_config() { + let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| { + site.config.markdown.insert_anchor_links = InsertAnchor::Right; + (site, true) + }); + + assert!(Path::new(&public).exists()); + // anchor link inserted + assert!(file_contains!( + public, + "index.html", + r##"

Heading 1🔗

"## + )); +} + #[test] fn can_build_site_with_pagination_for_section() { let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| { diff --git a/docs/content/documentation/content/linking.md b/docs/content/documentation/content/linking.md index 5bc3899a67..0676049990 100644 --- a/docs/content/documentation/content/linking.md +++ b/docs/content/documentation/content/linking.md @@ -34,7 +34,7 @@ links working. It is possible to have Zola automatically insert anchor links next to the heading, as you can see on this documentation if you hover a title or covering the full heading text. -This option is set at the section level: the `insert_anchor_links` variable on the +This option is set in the global [`config.toml`](@/documentation/getting-started/configuration.md)'s `[markdown]` section and can be overridden at the section level with the `insert_anchor_links` variable on the [section front matter page](@/documentation/content/section.md#front-matter). The default template is very basic and will need CSS tweaks in your project to look decent. diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index 5b83712db2..74c0bdae25 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -153,6 +153,14 @@ lazy_async_image = false # Whether footnotes are rendered in the GitHub-style (at the bottom, with back references) or plain (in the place, where they are defined) bottom_footnotes = false +# This determines whether to insert a link for each header like the ones you can see on this site if you hover over +# a header. +# The default template can be overridden by creating an `anchor-link.html` file in the `templates` directory. +# This value can be "left", "right", "heading" or "none". +# "heading" means the full heading becomes the text of the anchor. +# See "Internal links & deep linking" in the documentation for more information. +insert_anchor_links = "none" + # Configuration of the link checker. [link_checker] # Skip link checking for external URLs that start with these prefixes diff --git a/test_site/content/_index.md b/test_site/content/_index.md index ac36e06227..bc25b5f35b 100644 --- a/test_site/content/_index.md +++ b/test_site/content/_index.md @@ -1,2 +1,4 @@ +++ +++ + +# Heading 1 diff --git a/test_site/templates/index.html b/test_site/templates/index.html index 0c19bb0867..07fd82e19b 100644 --- a/test_site/templates/index.html +++ b/test_site/templates/index.html @@ -10,6 +10,8 @@

{{ page.title }}

<<<

+ + {{ section.content | safe }} {% endblock content %} {% block script %} diff --git a/test_site/templates/section.html b/test_site/templates/section.html index 3a262d3b45..c9b7869d95 100644 --- a/test_site/templates/section.html +++ b/test_site/templates/section.html @@ -1,6 +1,6 @@ {% extends "sample/templates/section-specific-extends.html" %} {% block content %} - {% for page in section.pages %} + {% for page in section.pages %} {{page.title}} {% endfor %} {{ super() }} diff --git a/test_site/themes/sample/templates/section-specific-extends.html b/test_site/themes/sample/templates/section-specific-extends.html index 7124bf1a2e..8007d4028b 100644 --- a/test_site/themes/sample/templates/section-specific-extends.html +++ b/test_site/themes/sample/templates/section-specific-extends.html @@ -1,6 +1,7 @@ {% extends "index.html" %} {% block content %} {{ section.relative_path | safe }} + {{ section.content | safe }} {% for sub in section.subsections %} {% set subsection = get_section(path=sub) %} {{subsection.title}}