Skip to content

Commit 054ce4e

Browse files
committed
Add bookmarklet to our range-diff page for easy of use
1 parent d445c9a commit 054ce4e

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,
@@ -26,6 +27,7 @@ use crate::{github, handlers::Context, utils::AppError};
2627
pub async fn gh_range_diff(
2728
Path((owner, repo, basehead)): Path<(String, String, String)>,
2829
State(ctx): State<Arc<Context>>,
30+
Host(host): Host,
2931
) -> axum::response::Result<impl IntoResponse, AppError> {
3032
let Some((oldhead, newhead)) = basehead.split_once("..") else {
3133
return Ok((
@@ -140,6 +142,9 @@ pub async fn gh_range_diff(
140142
// Create the HTML buffer with a very rough approximation for the capacity
141143
let mut html: String = String::with_capacity(800 + old.files.len() * 100);
142144

145+
// Compute the bookmarklet for the current host
146+
let bookmarklet = bookmarklet(&host);
147+
143148
// Write HTML header, style, ...
144149
writeln!(
145150
&mut html,
@@ -173,6 +178,7 @@ pub async fn gh_range_diff(
173178
</head>
174179
<body>
175180
<h3>range-diff of {oldbase}...{oldhead} {newbase}...{newhead}</h3>
181+
<p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, and use me on GitHub compare page.">range-diff</a> <span title="This javascript bookmark can be used to access this page with the right URL. To use it drag-on-drop the range-diff link to your bookmarks bar and click on it when you are on GitHub's compare page to use range-diff compare.">&#128712;</span></p>
176182
"#
177183
)?;
178184

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

0 commit comments

Comments
 (0)