| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
use quasi_declare::declare; |
| 32 |
use quasi_router::RegionKind; |
| 33 |
use quasi_webview::Webview; |
| 34 |
|
| 35 |
use crate::routes::git::notes_view::CommitNote; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
pub const REGION: &str = "notes"; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#[must_use] |
| 51 |
pub fn body_region(namespace: &str) -> String { |
| 52 |
format!("note-body-{}", slug(namespace)) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
fn slug(namespace: &str) -> String { |
| 57 |
namespace |
| 58 |
.chars() |
| 59 |
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' }) |
| 60 |
.collect() |
| 61 |
} |
| 62 |
|
| 63 |
declare! { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[must_use] |
| 70 |
pub shape region(notes: &[CommitNote]) -> Option<Node>; |
| 71 |
|
| 72 |
region REGION as Group unless notes.is_empty() { |
| 73 |
for note in notes { |
| 74 |
include one(note); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[must_use] |
| 85 |
pub fn html(notes: &[CommitNote]) -> String { |
| 86 |
use quasi_axum::Serves as _; |
| 87 |
|
| 88 |
let Some(node) = region(notes) else { |
| 89 |
return String::new(); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
fill(Webview::new(), notes).fragment(&node) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
#[must_use] |
| 102 |
pub fn fill(mut webview: Webview, notes: &[CommitNote]) -> Webview { |
| 103 |
for note in notes { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
webview = webview.with_fill( |
| 110 |
body_region(¬e.namespace), |
| 111 |
format!( |
| 112 |
"<div class=\"git-note-body git-readme-body\">{}</div>", |
| 113 |
note.html |
| 114 |
), |
| 115 |
); |
| 116 |
} |
| 117 |
webview |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
fn attribution(note: &CommitNote) -> String { |
| 131 |
let Some(attribution) = ¬e.attribution else { |
| 132 |
return String::new(); |
| 133 |
}; |
| 134 |
if attribution.exact { |
| 135 |
format!("{} - {}", attribution.by, attribution.when) |
| 136 |
} else { |
| 137 |
format!( |
| 138 |
"edited since {} - {}", |
| 139 |
attribution.short_commit, attribution.when |
| 140 |
) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
declare! { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
shape one(note: &CommitNote) -> Slot; |
| 152 |
|
| 153 |
region "note-{slug(¬e.namespace)}" as Group { |
| 154 |
region "note-header-{slug(¬e.namespace)}" as Group { |
| 155 |
across Wrap { |
| 156 |
beside Essential literal ¬e.namespace; |
| 157 |
beside Secondary text "written by makenot.work" when note.read_only; |
| 158 |
beside Secondary text attribution(note) when note.attribution.is_some(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
region body_region(¬e.namespace) as RegionKind::handover("a rendered note") {} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
#[cfg(test)] |
| 167 |
mod tests { |
| 168 |
use super::*; |
| 169 |
use crate::routes::git::notes_view::NoteAttribution; |
| 170 |
|
| 171 |
fn note(namespace: &str, read_only: bool, attribution: Option<NoteAttribution>) -> CommitNote { |
| 172 |
CommitNote { |
| 173 |
namespace: namespace.into(), |
| 174 |
html: format!("<p>the note in {namespace}</p>"), |
| 175 |
source: format!("the note in {namespace}"), |
| 176 |
attribution, |
| 177 |
read_only, |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
fn attributed(exact: bool) -> NoteAttribution { |
| 182 |
NoteAttribution { |
| 183 |
short_commit: "abc1234".into(), |
| 184 |
by: "ada".into(), |
| 185 |
when: "3 days ago".into(), |
| 186 |
exact, |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
#[test] |
| 193 |
fn no_notes_is_no_panel() { |
| 194 |
assert!(region(&[]).is_none()); |
| 195 |
assert_eq!(html(&[]), ""); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
#[test] |
| 201 |
fn the_rendered_note_is_what_the_reader_gets() { |
| 202 |
let html = html(&[note("commits", false, None)]); |
| 203 |
|
| 204 |
assert!(html.contains("<p>the note in commits</p>"), "{html}"); |
| 205 |
assert!(html.contains("git-note-body"), "{html}"); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#[test] |
| 211 |
fn a_namespace_with_a_slash_is_still_an_id() { |
| 212 |
let html = html(&[note("mnw/builds", true, None)]); |
| 213 |
|
| 214 |
assert!(html.contains("id=\"note-body-mnw-builds\""), "{html}"); |
| 215 |
assert!(!html.contains("id=\"note-body-mnw/builds\""), "{html}"); |
| 216 |
|
| 217 |
assert!(html.contains("mnw/builds"), "{html}"); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
#[test] |
| 222 |
fn a_note_written_here_says_where_it_came_from() { |
| 223 |
assert!(html(&[note("mnw/builds", true, None)]).contains("written by makenot.work")); |
| 224 |
assert!(!html(&[note("commits", false, None)]).contains("written by makenot.work")); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#[test] |
| 230 |
fn an_inexact_attribution_names_no_one() { |
| 231 |
let exact = html(&[note("commits", false, Some(attributed(true)))]); |
| 232 |
assert!(exact.contains("ada"), "{exact}"); |
| 233 |
|
| 234 |
let inexact = html(&[note("commits", false, Some(attributed(false)))]); |
| 235 |
assert!(inexact.contains("edited since abc1234"), "{inexact}"); |
| 236 |
assert!(!inexact.contains("ada"), "{inexact}"); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
#[test] |
| 243 |
fn every_note_is_its_own_region() { |
| 244 |
let html = html(&[ |
| 245 |
note("commits", false, None), |
| 246 |
note("mnw/builds", false, None), |
| 247 |
]); |
| 248 |
|
| 249 |
assert!(html.contains("id=\"note-commits\""), "{html}"); |
| 250 |
assert!(html.contains("id=\"note-header-commits\""), "{html}"); |
| 251 |
assert!(html.contains("id=\"note-mnw-builds\""), "{html}"); |
| 252 |
assert!(html.contains("id=\"note-header-mnw-builds\""), "{html}"); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
#[test] |
| 258 |
fn the_namespace_is_monospace() { |
| 259 |
let html = html(&[note("commits", false, None)]); |
| 260 |
|
| 261 |
assert!( |
| 262 |
html.contains("<code class=\"code\">commits</code>"), |
| 263 |
"{html}" |
| 264 |
); |
| 265 |
} |
| 266 |
} |
| 267 |
|