fix processor node range

This commit is contained in:
wxiaoguang 2024-05-02 13:15:13 +08:00
parent 4a11446e83
commit 86abfdc925
3 changed files with 19 additions and 15 deletions

View File

@ -10,6 +10,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"slices"
"strings" "strings"
"sync" "sync"
@ -1006,7 +1007,12 @@ func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil { if ctx.Metas == nil {
return return
} }
for node != nil { nodeStop := node.NextSibling
for node != nodeStop {
if node.Type != html.TextNode {
node = node.NextSibling
continue
}
ret, ok := anyHashPatternExtract(node.Data) ret, ok := anyHashPatternExtract(node.Data)
if !ok { if !ok {
node = node.NextSibling node = node.NextSibling
@ -1028,19 +1034,16 @@ func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil { if ctx.Metas == nil {
return return
} }
nodeStop := node.NextSibling
next := node.NextSibling for node != nodeStop {
for node != nil && node != next { if node.Type != html.TextNode {
m := comparePattern.FindStringSubmatchIndex(node.Data) node = node.NextSibling
if m == nil { continue
return
} }
m := comparePattern.FindStringSubmatchIndex(node.Data)
// Ensure that every group (m[0]...m[7]) has a match if m == nil || slices.Contains(m[:8], -1) { // ensure that every group (m[0]...m[7]) has a match
for i := 0; i < 8; i++ { node = node.NextSibling
if m[i] == -1 { continue
return
}
} }
urlFull := node.Data[m[0]:m[1]] urlFull := node.Data[m[0]:m[1]]

View File

@ -60,7 +60,8 @@ func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosSt
} }
func codePreviewPatternProcessor(ctx *RenderContext, node *html.Node) { func codePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
for node != nil { nodeStop := node.NextSibling
for node != nodeStop {
if node.Type != html.TextNode { if node.Type != html.TextNode {
node = node.NextSibling node = node.NextSibling
continue continue

View File

@ -700,7 +700,7 @@ func TestIssue18471(t *testing.T) {
}, strings.NewReader(data), &res) }, strings.NewReader(data), &res)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "<a href=\"http://domain/org/repo/compare/783b039...da951ce\" class=\"compare\"><code class=\"nohighlight\">783b039...da951ce</code></a>", res.String()) assert.Equal(t, `<a href="http://domain/org/repo/compare/783b039...da951ce" class="compare"><code class="nohighlight">783b039...da951ce</code></a>`, res.String())
} }
func TestIsFullURL(t *testing.T) { func TestIsFullURL(t *testing.T) {