From 18705e2fe09a49b0d45d6a0f256fd694207d18ab Mon Sep 17 00:00:00 2001 From: Robert Wolff Date: Thu, 4 Sep 2025 22:51:22 +0200 Subject: [PATCH] feat(ui): improve multiline file preview and anchor detection (#9145) This PR solves some little annoyance for me by allowing line ranges for inline file previews and source links to be of the form `L1-9` instead of necessarily `L1-L9`. For links to source files it allows also `n1-9` or `n1-n9` in agreement with already allowed single line anchors `n1`. ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [x] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9145 Reviewed-by: Gusted Co-authored-by: Robert Wolff Co-committed-by: Robert Wolff --- modules/markup/file_preview.go | 6 ++--- modules/markup/html_test.go | 33 +++++++++++++++++++++++++++ web_src/js/features/repo-code.js | 10 ++++---- web_src/js/features/repo-code.test.js | 3 +++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/modules/markup/file_preview.go b/modules/markup/file_preview.go index 8b1442ed08..dab6057cf4 100644 --- a/modules/markup/file_preview.go +++ b/modules/markup/file_preview.go @@ -25,7 +25,7 @@ import ( ) // filePreviewPattern matches "http://domain/org/repo/src/commit/COMMIT/filepath#L1-L2" -var filePreviewPattern = regexp.MustCompile(`https?://((?:\S+/){3})src/commit/([0-9a-f]{4,64})/(\S+)#(L\d+(?:-L\d+)?)`) +var filePreviewPattern = regexp.MustCompile(`https?://((?:\S+/){3})src/commit/([0-9a-f]{4,64})/(\S+)#(L\d+(?:-L?\d+)?)`) type FilePreview struct { fileContent []template.HTML @@ -78,17 +78,17 @@ func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Loca commitSha := node.Data[m[4]:m[5]] filePath := node.Data[m[6]:m[7]] + hash := node.Data[m[8]:m[9]] urlFullSource := urlFull if strings.HasSuffix(filePath, "?display=source") { filePath = strings.TrimSuffix(filePath, "?display=source") } else if Type(filePath) != "" { - urlFullSource = node.Data[m[0]:m[6]] + filePath + "?display=source#" + node.Data[m[8]:m[1]] + urlFullSource = node.Data[m[0]:m[6]] + filePath + "?display=source#" + hash } filePath, err := url.QueryUnescape(filePath) if err != nil { return nil } - hash := node.Data[m[8]:m[9]] preview.start = m[0] preview.end = m[1] diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 22c7e4ca5d..3794673e82 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -1109,6 +1109,39 @@ func TestRender_FilePreview(t *testing.T) { ) }) + t.Run("rendered file with lines L1-2 instead of L1-L2", func(t *testing.T) { + testRender( + commitFileURL+"#L1-2", + `

`+ + `
`+ + `
`+ + `
`+ + `path/to/file.md`+ + `
`+ + ``+ + `Lines 1 to 2 in c991312`+ + ``+ + `
`+ + `
`+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + `
# A`+"\n"+`
B`+"\n"+`
`+ + `
`+ + `
`+ + `

`, + localMetas, + ) + }) + commitFileURL = util.URLJoin(markup.TestRepoURL, "src", "commit", "190d9492934af498c3f669d6a2431dc5459e5b20", "path", "to", "file.go") t.Run("normal file with ?display=source", func(t *testing.T) { diff --git a/web_src/js/features/repo-code.js b/web_src/js/features/repo-code.js index 2ff9e611ae..9253603352 100644 --- a/web_src/js/features/repo-code.js +++ b/web_src/js/features/repo-code.js @@ -5,8 +5,8 @@ import {createTippy} from '../modules/tippy.js'; import {clippie} from 'clippie'; import {toAbsoluteUrl} from '../utils.js'; -export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/; -export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/; +export const singleAnchorRegex = /^#[Ln]([1-9][0-9]*)$/; +export const rangeAnchorRegex = /^#[Ln]([1-9][0-9]*)-[Ln]?([1-9][0-9]*)$/; function changeHash(hash) { if (window.history.pushState) { @@ -156,9 +156,9 @@ export function initRepoCodeView() { const $linesEls = $(getLineEls()); let $first; if (m) { - $first = $linesEls.filter(`[rel=${m[1]}]`); + $first = $linesEls.filter(`[rel=L${m[1]}]`); if ($first.length) { - const $last = $linesEls.filter(`[rel=${m[2]}]`); + const $last = $linesEls.filter(`[rel=L${m[2]}]`); selectRange($linesEls, $first, $last.length ? $last : $linesEls.last()); // show code view menu marker (don't show in blame page) @@ -172,7 +172,7 @@ export function initRepoCodeView() { } m = window.location.hash.match(singleAnchorRegex); if (m) { - $first = $linesEls.filter(`[rel=L${m[2]}]`); + $first = $linesEls.filter(`[rel=L${m[1]}]`); if ($first.length) { selectRange($linesEls, $first); diff --git a/web_src/js/features/repo-code.test.js b/web_src/js/features/repo-code.test.js index 0e0062a787..3d5d88a06f 100644 --- a/web_src/js/features/repo-code.test.js +++ b/web_src/js/features/repo-code.test.js @@ -14,4 +14,7 @@ test('rangeAnchorRegex', () => { expect(rangeAnchorRegex.test('#L1-L10')).toEqual(true); expect(rangeAnchorRegex.test('#L01-L10')).toEqual(false); expect(rangeAnchorRegex.test('#L1-L01')).toEqual(false); + expect(rangeAnchorRegex.test('#L1-10')).toEqual(true); + expect(rangeAnchorRegex.test('#n1-n10')).toEqual(true); + expect(rangeAnchorRegex.test('#n1-10')).toEqual(true); });