Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add insert_anchor_links back to config.toml #2767

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/config/src/config/markup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -59,6 +60,9 @@ pub struct Markdown {
pub extra_theme_set: Arc<Option<ThemeSet>>,
/// 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 {
Expand Down Expand Up @@ -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,
}
}
}
4 changes: 2 additions & 2 deletions components/content/src/front_matter/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InsertAnchor>,
/// 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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion components/content/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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#"<h1 id="heading-1">Heading 1</h1>"#));
}

#[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##"<h1 id="heading-1">Heading 1<a class="zola-anchor" href="#heading-1" aria-label="Anchor link for: heading-1">🔗</a></h1>"##
));
}

#[test]
fn can_build_site_with_pagination_for_section() {
let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| {
Expand Down
2 changes: 1 addition & 1 deletion docs/content/documentation/content/linking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions docs/content/documentation/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test_site/content/_index.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
+++
+++

# Heading 1
2 changes: 2 additions & 0 deletions test_site/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ <h3 class="post__title"><a href="{{ page.permalink }}">{{ page.title }}</a></h3>
</div>
<!-- Next line is meant to test inner html chars (see https://github.com/getzola/zola/issues/1152) -->
<p> <<< </p>

{{ section.content | safe }}
{% endblock content %}

{% block script %}
Expand Down
2 changes: 1 addition & 1 deletion test_site/templates/section.html
Original file line number Diff line number Diff line change
@@ -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() }}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down