From 04da527e536fbe6095c9bca816604a58e20b25d4 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 6 Apr 2017 17:07:53 +0900 Subject: [PATCH] Render anchor link --- src/config.rs | 6 ++++++ src/markdown.rs | 9 ++++++++- src/site.rs | 1 + src/templates/anchor-link.html | 3 +++ test_site/content/posts/fixed-slug.md | 2 ++ 5 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/templates/anchor-link.html diff --git a/src/config.rs b/src/config.rs index 0f9a737d..2d8a3bfd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -30,6 +30,10 @@ pub struct Config { pub generate_tags_pages: Option, /// Whether to generate categories and individual tag categories if some pages have them. Defaults to true pub generate_categories_pages: Option, + /// 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, /// All user params set in [extra] in the config pub extra: Option>, @@ -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, } } diff --git a/src/markdown.rs b/src/markdown.rs index 736d8245..f4321794 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -201,7 +201,14 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, 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 diff --git a/src/site.rs b/src/site.rs index 59463718..50602128 100644 --- a/src/site.rs +++ b/src/site.rs @@ -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")), diff --git a/src/templates/anchor-link.html b/src/templates/anchor-link.html new file mode 100644 index 00000000..e6f480b8 --- /dev/null +++ b/src/templates/anchor-link.html @@ -0,0 +1,3 @@ + + 🔗 + diff --git a/test_site/content/posts/fixed-slug.md b/test_site/content/posts/fixed-slug.md index 3f1216ff..57f322f5 100644 --- a/test_site/content/posts/fixed-slug.md +++ b/test_site/content/posts/fixed-slug.md @@ -6,3 +6,5 @@ date = "2017-01-01" +++ A simple page with a slug defined + +# Title