Skip to content

Commit 09b408a

Browse files
committed
Add "search confluence" action to results in Alfred
1 parent 0ad6e54 commit 09b408a

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ serde_json = "1.0.79"
1010
anyhow = "1.0.56"
1111
html-escape = "0.2.11"
1212
unicode-normalization = "0.1.19"
13+
serde_variant = "0.1.1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The current development setup assumes that it is built on a Mac with M1.
1212
### To do
1313
- [x] include id in results to alfred to ensure learning by selection
1414
- [x] possibility to copy url without opening result
15-
- [ ] include "search in confluence" option in results
15+
- [x] include "search in confluence" option in results
1616
- [x] fix &amp strings in results
1717
- [x] readme
1818
- [x] publish

src/main.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde::{Serialize, Deserialize};
2-
use reqwest::{Error, blocking::Client};
2+
use reqwest::blocking::Client;
33
use anyhow::{Context, Result};
44
use unicode_normalization::UnicodeNormalization;
5+
use serde_variant::to_variant_name;
56
use std::env;
67

78
fn main() -> Result<()> {
@@ -47,11 +48,32 @@ struct Match {
4748
id: Option<String>,
4849
name: String,
4950
href: String,
50-
class_name: String,
51+
class_name: MatchClassName,
5152
space_name: Option<String>,
5253
space_key: Option<String>,
5354
}
5455

56+
#[derive(Deserialize, Serialize, Debug, PartialEq)]
57+
enum MatchClassName {
58+
#[serde(rename = "content-type-page")]
59+
Page,
60+
#[serde(rename = "content-type-blogpost")]
61+
BlogPost,
62+
#[serde(rename = "search-for")]
63+
SearchFor,
64+
#[serde(other)]
65+
Unknown,
66+
}
67+
68+
impl MatchClassName {
69+
fn to_string(&self) -> String {
70+
match self {
71+
MatchClassName::Page | MatchClassName::BlogPost | MatchClassName::SearchFor => to_variant_name(self).unwrap().into(),
72+
MatchClassName::Unknown => panic!("Unsupported match class name"),
73+
}
74+
}
75+
}
76+
5577
#[derive(Serialize, Debug)]
5678
struct AlfredResult {
5779
uid: String,
@@ -86,13 +108,29 @@ impl AlfredResult {
86108
subtitle: confluence_match.space_name.unwrap(),
87109
arg: url.clone(),
88110
icon: AlfredResultIcon {
89-
path: format!("assets/{}.png", confluence_match.class_name),
111+
path: format!("assets/{}.png", confluence_match.class_name.to_string()),
90112
},
91113
text: AlfredResultText {
92114
copy: url,
93115
},
94116
}
95117
}
118+
119+
fn from_search_in_confluence_match(confluence_match: Match, base_url: &String) -> AlfredResult {
120+
let url = format!("{}{}", base_url, confluence_match.href);
121+
AlfredResult {
122+
uid: "search-item".to_string(),
123+
title: html_escape::decode_html_entities(&confluence_match.name).into_owned(),
124+
subtitle: "Use full Confluence Search".to_string(),
125+
arg: url.clone(),
126+
icon: AlfredResultIcon {
127+
path: format!("assets/{}.png", confluence_match.class_name.to_string()),
128+
},
129+
text: AlfredResultText {
130+
copy: confluence_match.name,
131+
},
132+
}
133+
}
96134
}
97135

98136
impl AlfredResultList {
@@ -102,9 +140,14 @@ impl AlfredResultList {
102140
.content_name_matches
103141
.into_iter()
104142
.flatten()
105-
.filter(|m| m.id.is_some())
106-
.filter(|m| m.class_name == "content-type-page" || m.class_name == "content-type-blogpost" || m.class_name == "search-for")
107-
.map(|m| AlfredResult::from(m, base_url))
143+
.filter(|m| m.class_name != MatchClassName::Unknown)
144+
.map(|m|
145+
match m.class_name {
146+
MatchClassName::Page | MatchClassName::BlogPost => AlfredResult::from(m, base_url),
147+
MatchClassName::SearchFor => AlfredResult::from_search_in_confluence_match(m, base_url),
148+
_ => panic!("Unsupported match class name"),
149+
}
150+
)
108151
.collect(),
109152
}
110153
}

0 commit comments

Comments
 (0)