Browse Source

Render anchor link

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
04da527e53
5 changed files with 20 additions and 1 deletions
  1. +6
    -0
      src/config.rs
  2. +8
    -1
      src/markdown.rs
  3. +1
    -0
      src/site.rs
  4. +3
    -0
      src/templates/anchor-link.html
  5. +2
    -0
      test_site/content/posts/fixed-slug.md

+ 6
- 0
src/config.rs 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,
}
}


+ 8
- 1
src/markdown.rs 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


+ 1
- 0
src/site.rs 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")),


+ 3
- 0
src/templates/anchor-link.html View File

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

+ 2
- 0
test_site/content/posts/fixed-slug.md View File

@@ -6,3 +6,5 @@ date = "2017-01-01"
+++

A simple page with a slug defined

# Title

Loading…
Cancel
Save