Render anchor link
This commit is contained in:
parent
22392ff9ed
commit
04da527e53
|
@ -30,6 +30,10 @@ pub struct Config {
|
||||||
pub generate_tags_pages: Option<bool>,
|
pub generate_tags_pages: Option<bool>,
|
||||||
/// Whether to generate categories and individual tag categories if some pages have them. Defaults to true
|
/// Whether to generate categories and individual tag categories if some pages have them. Defaults to true
|
||||||
pub generate_categories_pages: Option<bool>,
|
pub generate_categories_pages: Option<bool>,
|
||||||
|
/// Whether to insert a link for each header like in Github READMEs. Defaults to false
|
||||||
|
/// The default template can be overridden by creating a `anchor-link.html` template and CSS will need to be
|
||||||
|
/// written if you turn that on.
|
||||||
|
pub insert_anchor_links: Option<bool>,
|
||||||
|
|
||||||
/// All user params set in [extra] in the config
|
/// All user params set in [extra] in the config
|
||||||
pub extra: Option<HashMap<String, Toml>>,
|
pub extra: Option<HashMap<String, Toml>>,
|
||||||
|
@ -67,6 +71,7 @@ impl Config {
|
||||||
set_default!(config.generate_rss, false);
|
set_default!(config.generate_rss, false);
|
||||||
set_default!(config.generate_tags_pages, true);
|
set_default!(config.generate_tags_pages, true);
|
||||||
set_default!(config.generate_categories_pages, true);
|
set_default!(config.generate_categories_pages, true);
|
||||||
|
set_default!(config.insert_anchor_links, false);
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
@ -104,6 +109,7 @@ impl Default for Config {
|
||||||
generate_rss: Some(false),
|
generate_rss: Some(false),
|
||||||
generate_tags_pages: Some(true),
|
generate_tags_pages: Some(true),
|
||||||
generate_categories_pages: Some(true),
|
generate_categories_pages: Some(true),
|
||||||
|
insert_anchor_links: Some(false),
|
||||||
extra: None,
|
extra: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -201,7 +201,14 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
|
||||||
if in_header {
|
if in_header {
|
||||||
let id = find_anchor(&anchors, slugify(&text), 0);
|
let id = find_anchor(&anchors, slugify(&text), 0);
|
||||||
anchors.push(id.clone());
|
anchors.push(id.clone());
|
||||||
return Event::Html(Owned(format!(r#"id="{}">{}"#, id, text)));
|
let anchor_link = if config.insert_anchor_links.unwrap() {
|
||||||
|
let mut context = Context::new();
|
||||||
|
context.add("id", &id);
|
||||||
|
tera.render("anchor-link.html", &context).unwrap()
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
|
return Event::Html(Owned(format!(r#"id="{}">{}{}"#, id, anchor_link, text)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Business as usual
|
// Business as usual
|
||||||
|
|
|
@ -23,6 +23,7 @@ lazy_static! {
|
||||||
("rss.xml", include_str!("templates/rss.xml")),
|
("rss.xml", include_str!("templates/rss.xml")),
|
||||||
("sitemap.xml", include_str!("templates/sitemap.xml")),
|
("sitemap.xml", include_str!("templates/sitemap.xml")),
|
||||||
("robots.txt", include_str!("templates/robots.txt")),
|
("robots.txt", include_str!("templates/robots.txt")),
|
||||||
|
("anchor-link.html", include_str!("templates/anchor-link.html")),
|
||||||
|
|
||||||
("shortcodes/youtube.html", include_str!("templates/shortcodes/youtube.html")),
|
("shortcodes/youtube.html", include_str!("templates/shortcodes/youtube.html")),
|
||||||
("shortcodes/vimeo.html", include_str!("templates/shortcodes/vimeo.html")),
|
("shortcodes/vimeo.html", include_str!("templates/shortcodes/vimeo.html")),
|
||||||
|
|
3
src/templates/anchor-link.html
Normal file
3
src/templates/anchor-link.html
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<a class="anchor" href="#{{ id }}" aria-label="Anchor link for: {{ id }}">
|
||||||
|
🔗
|
||||||
|
</a>
|
|
@ -6,3 +6,5 @@ date = "2017-01-01"
|
||||||
+++
|
+++
|
||||||
|
|
||||||
A simple page with a slug defined
|
A simple page with a slug defined
|
||||||
|
|
||||||
|
# Title
|
||||||
|
|
Loading…
Reference in a new issue