From a2debf5b2cb551e1f881f6674883cf8847d2917c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Tue, 5 Oct 2021 22:34:29 +0200 Subject: [PATCH] add tag display to sidebar --- src/main.rs | 31 ++++++++++++++++++++++++++++--- templates/main.css | 4 ++++ templates/main.html | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index ff3e6a4..50cc6fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use percent_encoding::{percent_decode_str, utf8_percent_encode}; use pulldown_cmark::{html, Event, Options, Parser, Tag}; use regex::{Captures, Regex}; use slug::slugify; +use std::cmp::Reverse; use std::collections::HashMap; use std::fs::File; use std::io::Read; @@ -44,6 +45,7 @@ struct MutableState { struct GardenCache { pages: HashMap, files: Vec, + tags: HashMap, } impl Default for GardenCache { @@ -51,6 +53,7 @@ impl Default for GardenCache { GardenCache { pages: HashMap::new(), files: vec![], + tags: HashMap::new(), } } } @@ -297,6 +300,12 @@ async fn render( data.title.as_ref().unwrap_or(&"Digital Garden".to_string()), ); context.insert("files", &cache.files); + + let mut tags: Vec<(&String, &u32)> = cache.tags.iter().collect(); + tags.sort_by_key(|(t, _)| *t); + tags.sort_by_key(|(_, n)| Reverse(*n)); + context.insert("tags", &tags); + context.insert( "recently_changed", &recently_changed @@ -387,6 +396,7 @@ fn update_garden>( } let mut pages = current.pages; + let mut tags = current.tags; let markdown_paths = files .iter() @@ -428,9 +438,13 @@ fn update_garden>( }, }, ); + + result.tags.into_iter().for_each(|tag| { + *tags.entry(tag).or_insert(0) += 1; + }); } - let result = GardenCache { pages, files }; + let result = GardenCache { pages, files, tags }; trace!("{:#?}", result); Ok(result) } @@ -439,6 +453,7 @@ struct ParseResult { html: String, title: Option, links: Vec, + tags: Vec, } fn parse_garden>(text: S) -> anyhow::Result { @@ -448,10 +463,19 @@ fn parse_garden>(text: S) -> anyhow::Result { let mut last_nontext_event: Option = None; let mut links: Vec = vec![]; + let mut tags: Vec = vec![]; let parser = Parser::new_ext(text.as_ref(), Options::all()).map(|event| { - if let Event::Start(Tag::Link(_, str, _)) = &event { - links.push(str.to_string()); + if let Event::Start(Tag::Link(_, dest, _)) = &event { + links.push(dest.to_string()); + } + + if let Some(Event::Start(Tag::Link(_, dest, _))) = &last_nontext_event { + if let Event::Text(str) = &event { + if str.starts_with("#") { + tags.push(dest.to_string()); + } + } } if let Some(Event::Start(Tag::Heading(hl))) = last_nontext_event { @@ -476,6 +500,7 @@ fn parse_garden>(text: S) -> anyhow::Result { html, title: top_heading_text, links, + tags, }) } diff --git a/templates/main.css b/templates/main.css index 251b941..5271a96 100644 --- a/templates/main.css +++ b/templates/main.css @@ -183,6 +183,10 @@ nav .graph-view { text-align: center; } +.tag .count { + opacity: .5; +} + footer { padding: 1em 0; color: gray; diff --git a/templates/main.html b/templates/main.html index 982a902..46aa998 100644 --- a/templates/main.html +++ b/templates/main.html @@ -73,6 +73,20 @@ {% endfor %} + {% if tags %} +
+

Tags

+ +
+ {% endif %}
Graph view (beta)