Skip to content

Commit ac70dd9

Browse files
committed
Add bookmarklet to our range-diff page for easy of use
1 parent a61c8bb commit ac70dd9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Cargo.lock

Lines changed: 23 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
@@ -50,6 +50,7 @@ subtle = "2.6.1"
5050
sha2 = "0.10.9"
5151
imara-diff = "0.2.0"
5252
pulldown-cmark-escape = "0.11.0"
53+
axum-extra = { version = "0.10.1", default-features = false }
5354

5455
[dependencies.serde]
5556
version = "1"

src/gh_range_diff.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use axum::{
88
http::HeaderValue,
99
response::IntoResponse,
1010
};
11+
use axum_extra::extract::Host;
1112
use hyper::header::CACHE_CONTROL;
1213
use hyper::{
1314
HeaderMap, StatusCode,
@@ -23,6 +24,7 @@ use crate::{github, handlers::Context, utils::AppError};
2324
pub async fn gh_range_diff(
2425
Path((owner, repo, basehead)): Path<(String, String, String)>,
2526
State(ctx): State<Arc<Context>>,
27+
Host(host): Host,
2628
) -> axum::response::Result<impl IntoResponse, AppError> {
2729
let Some((oldhead, newhead)) = basehead.split_once("..") else {
2830
return Ok((
@@ -137,6 +139,9 @@ pub async fn gh_range_diff(
137139
// Create the HTML buffer with a very rough approximation for the capacity
138140
let mut html: String = String::with_capacity(800 + old.files.len() * 100);
139141

142+
// Compute the bookmarklet for the current host
143+
let bookmarklet = bookmarklet(&host);
144+
140145
// Write HTML header, style, ...
141146
writeln!(
142147
&mut html,
@@ -170,6 +175,7 @@ pub async fn gh_range_diff(
170175
</head>
171176
<body>
172177
<h3>range-diff of {oldbase}..{oldhead} {newbase}..{newhead}</h3>
178+
<p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, so I can be used on GitHub compare page">range-diff</a></p>
173179
"#
174180
)?;
175181

@@ -311,3 +317,21 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
311317
Ok(())
312318
}
313319
}
320+
321+
// Create the javascript bookmarklet based on the host
322+
fn bookmarklet(host: &str) -> String {
323+
let protocol = if host.starts_with("localhost:") {
324+
"http"
325+
} else {
326+
"https"
327+
};
328+
329+
format!(
330+
r"javascript:(() => {{
331+
const githubUrlPattern = /^https:\/\/github\.com\/([^\/]+)\/([^\/]+)\/compare\/([^\/]+[.]{{2}}[^\/]+)$/;
332+
const match = window.location.href.match(githubUrlPattern);
333+
if (!match) {{alert('Invalid GitHub Compare URL format.\nExpected: https://github.com/ORG_NAME/REPO_NAME/compare/BASESHA..HEADSHA'); return;}}
334+
const [, orgName, repoName, basehead] = match; window.location = `{protocol}://{host}/gh-range-diff/${{orgName}}/${{repoName}}/${{basehead}}`;
335+
}})();"
336+
)
337+
}

0 commit comments

Comments
 (0)