mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-02-03 09:57:24 +02:00
fixed excerpt modifier to properly add spaces after block tags
This commit is contained in:
parent
01e8c0f9f7
commit
b7a49efa88
@ -4,6 +4,8 @@
|
|||||||
An option to return also the empty found tokens was also added via `Tokenizer.KeepEmptyTokens(true)`.
|
An option to return also the empty found tokens was also added via `Tokenizer.KeepEmptyTokens(true)`.
|
||||||
_This should fix the parsing of whitespace charactes around view query column names when no quotes are used ([#3616](https://github.com/pocketbase/pocketbase/discussions/3616#discussioncomment-7398564))._
|
_This should fix the parsing of whitespace charactes around view query column names when no quotes are used ([#3616](https://github.com/pocketbase/pocketbase/discussions/3616#discussioncomment-7398564))._
|
||||||
|
|
||||||
|
- Fixed the `:excerpt(max, withEllipsis?)` `field` query param modifier to properly add space to the generated text fragment after block tags.
|
||||||
|
|
||||||
|
|
||||||
## v0.19.0
|
## v0.19.0
|
||||||
|
|
||||||
|
@ -78,8 +78,7 @@ func (m *excerptModifier) Modify(value any) (any, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var isNotEmpty bool
|
var hasPrevSpace bool
|
||||||
var needSpace bool
|
|
||||||
|
|
||||||
// for all node types and more details check
|
// for all node types and more details check
|
||||||
// https://pkg.go.dev/golang.org/x/net/html#Parse
|
// https://pkg.go.dev/golang.org/x/net/html#Parse
|
||||||
@ -87,37 +86,47 @@ func (m *excerptModifier) Modify(value any) (any, error) {
|
|||||||
stripTags = func(n *html.Node) {
|
stripTags = func(n *html.Node) {
|
||||||
switch n.Type {
|
switch n.Type {
|
||||||
case html.TextNode:
|
case html.TextNode:
|
||||||
if txt := strings.TrimSpace(whitespaceRegex.ReplaceAllString(n.Data, " ")); txt != "" {
|
// collapse multiple spaces into one
|
||||||
if isNotEmpty && needSpace {
|
txt := whitespaceRegex.ReplaceAllString(n.Data, " ")
|
||||||
needSpace = false
|
|
||||||
builder.WriteString(" ")
|
if hasPrevSpace {
|
||||||
|
txt = strings.TrimLeft(txt, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if txt != "" {
|
||||||
|
hasPrevSpace = strings.HasSuffix(txt, " ")
|
||||||
|
|
||||||
builder.WriteString(txt)
|
builder.WriteString(txt)
|
||||||
|
|
||||||
if !isNotEmpty {
|
|
||||||
isNotEmpty = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case html.ElementNode:
|
|
||||||
if !needSpace && !list.ExistInSlice(n.Data, inlineTags) {
|
|
||||||
needSpace = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if builder.Len() > m.max {
|
// excerpt max has been reached => no need to further iterate
|
||||||
|
// (+2 for the extra whitespace suffix/prefix that will be trimmed later)
|
||||||
|
if builder.Len() > m.max+2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||||
if c.Type != html.ElementNode || !list.ExistInSlice(c.Data, excludeTags) {
|
if c.Type != html.ElementNode || !list.ExistInSlice(c.Data, excludeTags) {
|
||||||
|
isBlock := c.Type == html.ElementNode && !list.ExistInSlice(c.Data, inlineTags)
|
||||||
|
|
||||||
|
if isBlock && !hasPrevSpace {
|
||||||
|
builder.WriteString(" ")
|
||||||
|
hasPrevSpace = true
|
||||||
|
}
|
||||||
|
|
||||||
stripTags(c)
|
stripTags(c)
|
||||||
|
|
||||||
|
if isBlock && !hasPrevSpace {
|
||||||
|
builder.WriteString(" ")
|
||||||
|
hasPrevSpace = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stripTags(doc)
|
stripTags(doc)
|
||||||
|
|
||||||
result := builder.String()
|
result := strings.TrimSpace(builder.String())
|
||||||
|
|
||||||
if len(result) > m.max {
|
if len(result) > m.max {
|
||||||
result = strings.TrimSpace(result[:m.max])
|
result = strings.TrimSpace(result[:m.max])
|
||||||
|
@ -86,9 +86,9 @@ func TestNewExcerptModifier(t *testing.T) {
|
|||||||
func TestExcerptModifierModify(t *testing.T) {
|
func TestExcerptModifierModify(t *testing.T) {
|
||||||
// plain text value: "Hello t est12 3 word"
|
// plain text value: "Hello t est12 3 word"
|
||||||
html := ` <script>var a = 123;</script> <p>Hello</p><div id="test_id">t est<b>12
|
html := ` <script>var a = 123;</script> <p>Hello</p><div id="test_id">t est<b>12
|
||||||
3</b></div> <h1>word </h1> `
|
3</b><span>456</span></div><span>word <b>7</b> 89<span>!<b>?</b><b> a </b><b>b </b>c</span>#<h1>title</h1>`
|
||||||
|
|
||||||
plainText := "Hello t est12 3 word"
|
plainText := "Hello t est12 3456 word 7 89!? a b c# title"
|
||||||
|
|
||||||
scenarios := []struct {
|
scenarios := []struct {
|
||||||
name string
|
name string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user