@@ -8,6 +8,7 @@ use axum::{
8
8
http:: HeaderValue ,
9
9
response:: IntoResponse ,
10
10
} ;
11
+ use axum_extra:: extract:: Host ;
11
12
use hyper:: header:: CACHE_CONTROL ;
12
13
use hyper:: {
13
14
HeaderMap , StatusCode ,
@@ -23,6 +24,7 @@ use crate::{github, handlers::Context, utils::AppError};
23
24
pub async fn gh_range_diff (
24
25
Path ( ( owner, repo, basehead) ) : Path < ( String , String , String ) > ,
25
26
State ( ctx) : State < Arc < Context > > ,
27
+ Host ( host) : Host ,
26
28
) -> axum:: response:: Result < impl IntoResponse , AppError > {
27
29
let Some ( ( oldhead, newhead) ) = basehead. split_once ( ".." ) else {
28
30
return Ok ( (
@@ -137,6 +139,9 @@ pub async fn gh_range_diff(
137
139
// Create the HTML buffer with a very rough approximation for the capacity
138
140
let mut html: String = String :: with_capacity ( 800 + old. files . len ( ) * 100 ) ;
139
141
142
+ // Compute the bookmarklet for the current host
143
+ let bookmarklet = bookmarklet ( & host) ;
144
+
140
145
// Write HTML header, style, ...
141
146
writeln ! (
142
147
& mut html,
@@ -170,6 +175,7 @@ pub async fn gh_range_diff(
170
175
</head>
171
176
<body>
172
177
<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>
173
179
"#
174
180
) ?;
175
181
@@ -311,3 +317,21 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
311
317
Ok ( ( ) )
312
318
}
313
319
}
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