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/<pull request number>.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 <gusted@noreply.codeberg.org>
Co-authored-by: Robert Wolff <mahlzahn@posteo.de>
Co-committed-by: Robert Wolff <mahlzahn@posteo.de>
This commit is contained in:
Robert Wolff 2025-09-04 22:51:22 +02:00 committed by Earl Warren
commit 18705e2fe0
4 changed files with 44 additions and 8 deletions

View file

@ -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);

View file

@ -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);
});