Fix issue where rendering stops after the first invalid parmalink

This commit is contained in:
Mai-Lapyst 2024-04-19 18:21:21 +02:00
parent 5b6b3f3fb3
commit e9eacdecd2
No known key found for this signature in database
GPG key ID: F88D929C09E239F8
3 changed files with 77 additions and 28 deletions

View file

@ -35,24 +35,36 @@ type FilePreview struct {
isTruncated bool isTruncated bool
} }
func NewFilePreview(ctx *RenderContext, node *html.Node, locale translation.Locale) *FilePreview { func NewFilePreviews(ctx *RenderContext, node *html.Node, locale translation.Locale) []*FilePreview {
if setting.FilePreviewMaxLines == 0 { if setting.FilePreviewMaxLines == 0 {
// Feature is disabled // Feature is disabled
return nil return nil
} }
preview := &FilePreview{} mAll := filePreviewPattern.FindAllStringSubmatchIndex(node.Data, -1)
if mAll == nil {
m := filePreviewPattern.FindStringSubmatchIndex(node.Data)
if m == nil {
return nil return nil
} }
// Ensure that every group has a match result := make([]*FilePreview, 0)
for _, m := range mAll {
if slices.Contains(m, -1) { if slices.Contains(m, -1) {
return nil continue
} }
preview := newFilePreview(ctx, node, locale, m)
if preview != nil {
result = append(result, preview)
}
}
return result
}
func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Locale, m []int) *FilePreview {
preview := &FilePreview{}
urlFull := node.Data[m[0]:m[1]] urlFull := node.Data[m[0]:m[1]]
// Ensure that we only use links to local repositories // Ensure that we only use links to local repositories

View file

@ -1073,17 +1073,20 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
next := node.NextSibling next := node.NextSibling
for node != nil && node != next { for node != nil && node != next {
preview := NewFilePreview(ctx, node, locale) previews := NewFilePreviews(ctx, node, locale)
if preview == nil { if previews == nil {
node = node.NextSibling node = node.NextSibling
continue continue
} }
offset := 0
for _, preview := range previews {
previewNode := preview.CreateHTML(locale) previewNode := preview.CreateHTML(locale)
// Specialized version of replaceContent, so the parent paragraph element is not destroyed from our div // Specialized version of replaceContent, so the parent paragraph element is not destroyed from our div
before := node.Data[:preview.start] before := node.Data[:(preview.start - offset)]
after := node.Data[preview.end:] after := node.Data[(preview.end - offset):]
offset += preview.end - 3
node.Data = before node.Data = before
nextSibling := node.NextSibling nextSibling := node.NextSibling
node.Parent.InsertBefore(&html.Node{ node.Parent.InsertBefore(&html.Node{
@ -1091,10 +1094,13 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
Data: "</p>", Data: "</p>",
}, nextSibling) }, nextSibling)
node.Parent.InsertBefore(previewNode, nextSibling) node.Parent.InsertBefore(previewNode, nextSibling)
node.Parent.InsertBefore(&html.Node{ afterNode := &html.Node{
Type: html.RawNode, Type: html.RawNode,
Data: "<p>" + after, Data: "<p>" + after,
}, nextSibling) }
node.Parent.InsertBefore(afterNode, nextSibling)
node = afterNode
}
node = node.NextSibling node = node.NextSibling
} }

View file

@ -828,6 +828,37 @@ func TestRender_FilePreview(t *testing.T) {
`<p></p>`, `<p></p>`,
localMetas, localMetas,
) )
testRender(
"first without sub "+commitFilePreview+" second "+urlWithSub,
`<p>first without sub <a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20/path/to/file.go#L2-L3" rel="nofollow"><code>190d949293/path/to/file.go (L2-L3)</code></a> second </p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/sub/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20/path/to/file.go#L2-L3" class="muted" rel="nofollow">path/to/file.go</a>`+
`</div>`+
`<span class="text small grey">`+
`Lines 2 to 3 in <a href="http://localhost:3000/sub/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20" class="text black" rel="nofollow">190d949</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="2"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">B</span>`+"\n"+`</code></td>`+
`</tr>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="3"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">C</span>`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p></p>`,
localMetas,
)
}) })
t.Run("multiples", func(t *testing.T) { t.Run("multiples", func(t *testing.T) {