chore: add memo content snippet
This commit is contained in:
parent
0053977b14
commit
5a26b76511
1 changed files with 23 additions and 0 deletions
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/google/cel-go/cel"
|
"github.com/google/cel-go/cel"
|
||||||
"github.com/lithammer/shortuuid/v4"
|
"github.com/lithammer/shortuuid/v4"
|
||||||
|
|
@ -1301,5 +1302,27 @@ func getMemoContentSnippet(content string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
plainText := renderer.NewStringRenderer().Render(nodes)
|
plainText := renderer.NewStringRenderer().Render(nodes)
|
||||||
|
if len(plainText) > 100 {
|
||||||
|
return substring(plainText, 100) + "...", nil
|
||||||
|
}
|
||||||
return plainText, nil
|
return plainText, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func substring(s string, length int) string {
|
||||||
|
if length <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
runeCount := 0
|
||||||
|
byteIndex := 0
|
||||||
|
for byteIndex < len(s) {
|
||||||
|
_, size := utf8.DecodeRuneInString(s[byteIndex:])
|
||||||
|
byteIndex += size
|
||||||
|
runeCount++
|
||||||
|
if runeCount == length {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s[:byteIndex]
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue