From f315ed2d6c6e0aa18cef8debd1b174a48c9a800e Mon Sep 17 00:00:00 2001
From: Fuwn
Date: Thu, 4 Apr 2024 03:01:18 +0000
Subject: feat(html): condense links option
---
Configuration.md | 21 +++++++++++++++++++++
src/html.rs | 30 ++++++++++++++++++++++++++++--
2 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/Configuration.md b/Configuration.md
index e87678d..b461b7e 100644
--- a/Configuration.md
+++ b/Configuration.md
@@ -123,3 +123,24 @@ Any non-empty value other than `1` will enable this feature, while removing the
```dotenv
EMBED_IMAGES=2
```
+
+## `CONDENSE_LINKS`
+
+Condense adjacent links to a single line
+
+A value of `*` will condense all adjacent links to a single line.
+
+A comma-separated list of paths will condense adjacent links to a single line only on those paths.
+
+### Example
+
+```plaintext
+
+
+Link
+Link
+Link
+
+
+Link | Link | Link
+```
diff --git a/src/html.rs b/src/html.rs
index b86c496..07d979e 100644
--- a/src/html.rs
+++ b/src/html.rs
@@ -26,7 +26,7 @@ fn link_from_host_href(url: &Url, href: &str) -> Option {
))
}
-#[allow(clippy::too_many_lines)]
+#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
pub fn from_gemini(
response: &germ::request::Response,
url: &Url,
@@ -38,8 +38,32 @@ pub fn from_gemini(
let mut html = String::new();
let mut title = String::new();
let safe = html_escape::encode_text;
+ let mut previous_link = false;
+ let condense_links = {
+ let links = var("CONDENSE_LINKS").map_or_else(
+ |_| vec![],
+ |condense_links| {
+ condense_links
+ .split(',')
+ .map(std::string::ToString::to_string)
+ .collect()
+ },
+ );
+
+ links.contains(&url.path().to_string()) || links.contains(&"*".to_string())
+ };
for node in ast {
+ if previous_link && (!matches!(node, Node::Link { .. }) || !condense_links)
+ {
+ html.push_str("\n
");
+ previous_link = false;
+ } else if previous_link {
+ html.push_str(" | ");
+ } else if !previous_link && matches!(node, Node::Link { .. }) {
+ html.push_str("");
+ }
+
match node {
Node::Text(text) => html.push_str(&format!("
{}
", safe(text))),
Node::Link { to, text } => {
@@ -140,8 +164,10 @@ pub fn from_gemini(
}
}
+ previous_link = true;
+
html.push_str(&format!(
- "{}
\n",
+ "{}",
href,
safe(&text.clone().unwrap_or_default()),
));
--
cgit v1.2.3