Render anchor link

This commit is contained in:
Vincent Prouillet 2017-04-06 17:07:53 +09:00
parent 22392ff9ed
commit 04da527e53
5 changed files with 20 additions and 1 deletions

View file

@ -30,6 +30,10 @@ pub struct Config {
pub generate_tags_pages: Option<bool>,
/// Whether to generate categories and individual tag categories if some pages have them. Defaults to true
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
pub extra: Option<HashMap<String, Toml>>,
@ -67,6 +71,7 @@ impl Config {
set_default!(config.generate_rss, false);
set_default!(config.generate_tags_pages, true);
set_default!(config.generate_categories_pages, true);
set_default!(config.insert_anchor_links, false);
Ok(config)
}
@ -104,6 +109,7 @@ impl Default for Config {
generate_rss: Some(false),
generate_tags_pages: Some(true),
generate_categories_pages: Some(true),
insert_anchor_links: Some(false),
extra: None,
}
}

View file

@ -201,7 +201,14 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
if in_header {
let id = find_anchor(&anchors, slugify(&text), 0);
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

View file

@ -23,6 +23,7 @@ lazy_static! {
("rss.xml", include_str!("templates/rss.xml")),
("sitemap.xml", include_str!("templates/sitemap.xml")),
("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/vimeo.html", include_str!("templates/shortcodes/vimeo.html")),

View file

@ -0,0 +1,3 @@
<a class="anchor" href="#{{ id }}" aria-label="Anchor link for: {{ id }}">
🔗
</a>

View file

@ -6,3 +6,5 @@ date = "2017-01-01"
+++
A simple page with a slug defined
# Title