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",
+ `
`+
+ ``+
+ ``+
+ `
`+
+ `
`+
+ ``+
+ ``+
+ ` | `+
+ `# 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);
});