mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit 5b4d76ad88fd679abfbdb2ed07f3639d50bf48c9
parent 6fdcb492097a53d512f37b9347f60ed2dd921985
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Tue, 30 Mar 2021 23:28:58 +0200

make line links permalinks

The commit a shorthand name of a ref points to may change thus
rendering a line link out of date. This can be fixed by instead
using the commit oid.

Diffstat:
Msrc/main.rs | 13+++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -428,12 +428,21 @@ async fn repo_file(req: Request<()>) -> tide::Result { LinesWithEndings::from(file_string) .for_each(|line| highlighter.parse_html_for_line_which_includes_newline(line)); + // use oid so it is a permalink + let prefix = format!( + "/{}/tree/{}/item/{}", + req.param("repo_name").unwrap(), + commit.id(), + path.display() + ); + let mut output = String::from("<pre>\n"); for (n, line) in highlighter.finalize().lines().enumerate() { output.push_str(&format!( - "<a href='#L{0}' id='L{0}' class='line'>{0}</a>{1}\n", + "<a href='{1}#L{0}' id='L{0}' class='line'>{0}</a>{2}\n", n + 1, - line + prefix, + line, )); } output.push_str("</pre>\n");