mirror of
https://github.com/usememos/memos.git
synced 2024-11-11 01:12:40 +08:00
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"memos/api"
|
||
|
"net/http"
|
||
|
"regexp"
|
||
|
|
||
|
"github.com/labstack/echo/v4"
|
||
|
)
|
||
|
|
||
|
func (s *Server) registerTagRoutes(g *echo.Group) {
|
||
|
g.GET("/tag", func(c echo.Context) error {
|
||
|
userID := c.Get(getUserIDContextKey()).(int)
|
||
|
contentSearch := "#"
|
||
|
memoFind := api.MemoFind{
|
||
|
CreatorID: &userID,
|
||
|
ContentSearch: &contentSearch,
|
||
|
}
|
||
|
|
||
|
memoList, err := s.Store.FindMemoList(&memoFind)
|
||
|
if err != nil {
|
||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
||
|
}
|
||
|
|
||
|
tagMapSet := make(map[string]bool)
|
||
|
|
||
|
r, err := regexp.Compile("#(.+?) ")
|
||
|
if err != nil {
|
||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compile regexp").SetInternal(err)
|
||
|
}
|
||
|
for _, memo := range memoList {
|
||
|
for _, rawTag := range r.FindAllString(memo.Content, -1) {
|
||
|
tag := r.ReplaceAllString(rawTag, "$1")
|
||
|
tagMapSet[tag] = true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
tagList := []string{}
|
||
|
for tag := range tagMapSet {
|
||
|
tagList = append(tagList, tag)
|
||
|
}
|
||
|
|
||
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
||
|
if err := json.NewEncoder(c.Response().Writer).Encode(tagList); err != nil {
|
||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
}
|