Files
skillshare/internal/server/handler_search.go
T
Willie 88847ea3ae feat: add skill field to hub schema for multi-skill repo support
Hub JSON entries can now specify a `skill` field to target a specific
skill within a multi-skill repo. CLI auto-passes -s flag, Web UI
skips the picker modal when skill is specified.

Also adds Community Hub section to search docs with link to
runkids/skillshare-hub.
2026-02-13 14:21:37 +08:00

83 lines
2.0 KiB
Go

package server
import (
"encoding/json"
"net/http"
"path/filepath"
"strconv"
"skillshare/internal/hub"
"skillshare/internal/search"
)
func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("q")
hubParam := r.URL.Query().Get("hub")
limit := 20
if l := r.URL.Query().Get("limit"); l != "" {
if parsed, err := strconv.Atoi(l); err == nil {
limit = parsed
}
}
var results []search.SearchResult
var err error
switch {
case hubParam == "@builtin":
results, err = s.searchBuiltinIndex(query, limit)
case hubParam != "":
results, err = search.SearchFromIndexURL(query, limit, hubParam)
default:
results, err = search.Search(query, limit)
}
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
type resultItem struct {
Name string `json:"name"`
Description string `json:"description"`
Source string `json:"source"`
Skill string `json:"skill,omitempty"`
Stars int `json:"stars"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Tags []string `json:"tags,omitempty"`
}
items := make([]resultItem, 0, len(results))
for _, r := range results {
items = append(items, resultItem{
Name: r.Name,
Description: r.Description,
Source: r.Source,
Skill: r.Skill,
Stars: r.Stars,
Owner: r.Owner,
Repo: r.Repo,
Tags: r.Tags,
})
}
writeJSON(w, map[string]any{"results": items})
}
// searchBuiltinIndex builds the hub index from local skills and searches it in-memory.
func (s *Server) searchBuiltinIndex(query string, limit int) ([]search.SearchResult, error) {
sourcePath := s.cfg.Source
if s.IsProjectMode() {
sourcePath = filepath.Join(s.projectRoot, ".skillshare", "skills")
}
idx, err := hub.BuildIndex(sourcePath, false)
if err != nil {
return nil, err
}
data, err := json.Marshal(idx)
if err != nil {
return nil, err
}
return search.SearchFromIndexJSON(query, limit, data)
}