mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 20:56:51 +00:00
feat(GODT-3121): refactored retrieval kb article index lookup.
This commit is contained in:
@ -20,6 +20,7 @@ package kb
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@ -27,6 +28,8 @@ import (
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
var ErrArticleNotFound = errors.New("KB article not found")
|
||||
|
||||
//go:embed kbArticleList.json
|
||||
var articleListString []byte
|
||||
|
||||
@ -75,6 +78,20 @@ func GetSuggestions(userInput string) (ArticleList, error) {
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// GetArticleIndex retrieve the index of an article from its url. if the article is not found, ErrArticleNotFound is returned.
|
||||
func GetArticleIndex(url string) (uint64, error) {
|
||||
articles, err := GetArticleList()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
index := xslices.IndexFunc(articles, func(article *Article) bool { return strings.EqualFold(article.URL, url) })
|
||||
if index == -1 {
|
||||
return 0, ErrArticleNotFound
|
||||
}
|
||||
return uint64(index), nil
|
||||
}
|
||||
|
||||
func simplifyUserInput(input string) string {
|
||||
// replace any sequence not matching of the following with a single space:
|
||||
// - letters in any language (accentuated or not)
|
||||
|
||||
Reference in New Issue
Block a user