Fix issue where rendering stops after the first invalid parmalink

(cherry picked from commit e9eacdecd2)
This commit is contained in:
Mai-Lapyst 2024-04-19 18:21:21 +02:00 committed by GitHub
parent 9c81060eb9
commit ae0615561a
3 changed files with 77 additions and 28 deletions

View file

@ -35,24 +35,36 @@ type FilePreview struct {
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 {
// Feature is disabled
return nil
}
mAll := filePreviewPattern.FindAllStringSubmatchIndex(node.Data, -1)
if mAll == nil {
return nil
}
result := make([]*FilePreview, 0)
for _, m := range mAll {
if slices.Contains(m, -1) {
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{}
m := filePreviewPattern.FindStringSubmatchIndex(node.Data)
if m == nil {
return nil
}
// Ensure that every group has a match
if slices.Contains(m, -1) {
return nil
}
urlFull := node.Data[m[0]:m[1]]
// Ensure that we only use links to local repositories

View file

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

View file

@ -828,6 +828,37 @@ func TestRender_FilePreview(t *testing.T) {
`<p></p>`,
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) {