fix(wiki): keep folder names that look like page-type labels when placing pages
A folder named 概念/实体/Concepts was stripped from category_path as model noise, so a page moved into it kept depth 0 and a root-level wiki_path and rendered at the root of the directory tree. Folder paths are authoritative and now map to category_path verbatim; the model-label cleaner still applies to pages without a folder. Fixes #3053 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DFSMrLZZ3oq1dKmJFAofz6main
parent
5be8d13fc6
commit
9e8673816e
|
|
@ -365,16 +365,18 @@ func (r *wikiPageRepository) List(ctx context.Context, req *types.WikiPageListRe
|
|||
// Directory filters are pushed to SQL so the DB does the counting and
|
||||
// pagination instead of loading every page of the type into memory. `depth`
|
||||
// is a cached column (= len(category_path)); `category_path` is a JSON column
|
||||
// whose stored text is json.Marshal of the cleaned path, so we compare
|
||||
// against the same encoding. Postgres needs an explicit jsonb cast for array
|
||||
// equality; SQLite stores JSON as TEXT and compares directly.
|
||||
// whose stored text is json.Marshal of the folder path segments, so we
|
||||
// compare against the same encoding (literal segments, since the filter is a
|
||||
// folder path and folder names are authoritative). Postgres needs an explicit
|
||||
// jsonb cast for array equality; SQLite stores JSON as TEXT and compares
|
||||
// directly.
|
||||
if req.FolderID != nil {
|
||||
query = query.Where("folder_id = ?", *req.FolderID)
|
||||
}
|
||||
if req.CategoryDepth != nil {
|
||||
query = query.Where("depth = ?", *req.CategoryDepth)
|
||||
}
|
||||
if wantPath := types.CleanWikiCategoryPath(req.CategoryPath); len(wantPath) > 0 {
|
||||
if wantPath := types.TrimWikiFolderSegments(req.CategoryPath); len(wantPath) > 0 {
|
||||
if encoded, err := json.Marshal([]string(wantPath)); err == nil {
|
||||
if r.db.Dialector != nil && r.db.Dialector.Name() == "postgres" {
|
||||
query = query.Where("category_path::jsonb = ?::jsonb", string(encoded))
|
||||
|
|
|
|||
|
|
@ -1281,7 +1281,16 @@ func normalizeWikiHierarchy(page *types.WikiPage) {
|
|||
}
|
||||
page.ParentSlug = strings.TrimSpace(page.ParentSlug)
|
||||
|
||||
cleanPath := types.StringArray(types.CleanWikiCategoryPath(page.CategoryPath))
|
||||
// A page filed in a folder mirrors the folder tree exactly: its
|
||||
// category_path was derived from the validated folder path, so the
|
||||
// model-noise cleaning (which drops type-like labels such as "概念") must
|
||||
// not rewrite it. Only pages without a folder carry model-authored labels.
|
||||
var cleanPath types.StringArray
|
||||
if strings.TrimSpace(page.FolderID) != "" {
|
||||
cleanPath = types.StringArray(types.TrimWikiFolderSegments(page.CategoryPath))
|
||||
} else {
|
||||
cleanPath = types.StringArray(types.CleanWikiCategoryPath(page.CategoryPath))
|
||||
}
|
||||
page.CategoryPath = cleanPath
|
||||
page.Depth = len(cleanPath)
|
||||
|
||||
|
|
@ -1366,13 +1375,12 @@ func (s *wikiPageService) UpdateIssueStatus(ctx context.Context, issueID string,
|
|||
|
||||
// --- Folder tree (wiki_folders) ---
|
||||
|
||||
// wikiFolderSegments splits a materialized folder path ("AI/RAG") into cleaned
|
||||
// segments. Empty/blank path yields nil (the wiki root).
|
||||
// wikiFolderSegments splits a materialized folder path ("AI/RAG") into its
|
||||
// literal segments. Empty/blank path yields nil (the wiki root). Folder names
|
||||
// are authoritative, so a folder named like a page type ("概念", "Concepts")
|
||||
// is kept rather than dropped as model noise.
|
||||
func wikiFolderSegments(path string) []string {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return nil
|
||||
}
|
||||
return types.CleanWikiCategoryPath(strings.Split(path, "/"))
|
||||
return types.WikiFolderPathSegments(path)
|
||||
}
|
||||
|
||||
// applyFolderToPage refreshes a page's derived category_path cache from its
|
||||
|
|
|
|||
|
|
@ -646,3 +646,63 @@ func TestFindPagesByNormalizedTitleMatchesWhitespace(t *testing.T) {
|
|||
slugs := []string{batched[0].Slug, batched[1].Slug}
|
||||
require.ElementsMatch(t, []string{"entity/confucius", "entity/mencius"}, slugs)
|
||||
}
|
||||
|
||||
func TestNormalizeWikiHierarchyKeepsFolderBackedPathVerbatim(t *testing.T) {
|
||||
page := &types.WikiPage{
|
||||
Slug: "concept/plan",
|
||||
Title: "将计就计",
|
||||
PageType: types.WikiPageTypeConcept,
|
||||
FolderID: "folder-concepts",
|
||||
CategoryPath: types.StringArray{"概念", " 概念 ", "Concepts", ""},
|
||||
}
|
||||
|
||||
normalizeWikiHierarchy(page)
|
||||
|
||||
want := types.StringArray{"概念", "概念", "Concepts"}
|
||||
require.Equal(t, want, page.CategoryPath)
|
||||
require.Equal(t, 3, page.Depth)
|
||||
require.Equal(t, "concept/概念/概念/Concepts/将计就计", page.WikiPath)
|
||||
}
|
||||
|
||||
func TestMovePageIntoTypeLabelNamedFolderKeepsHierarchy(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&types.WikiFolder{}, &types.WikiPage{}, &types.WikiPageRevision{}))
|
||||
|
||||
ctx := context.Background()
|
||||
repo := repository.NewWikiPageRepository(db)
|
||||
svc := NewWikiPageService(repo, nil, nil, nil, nil)
|
||||
now := time.Now()
|
||||
|
||||
folder, err := svc.CreateFolder(ctx, "kb-move", 1, types.WikiFolderRootID, "概念")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, repo.Create(ctx, &types.WikiPage{
|
||||
ID: "page-plan", TenantID: 1, KnowledgeBaseID: "kb-move", Slug: "concept/plan",
|
||||
Title: "将计就计", PageType: types.WikiPageTypeConcept, Status: types.WikiPageStatusPublished,
|
||||
Version: 1, CreatedAt: now, UpdatedAt: now,
|
||||
}))
|
||||
|
||||
moved, err := svc.MovePage(ctx, "kb-move", "concept/plan", folder.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, folder.ID, moved.FolderID)
|
||||
|
||||
stored, err := repo.GetBySlug(ctx, "kb-move", "concept/plan")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, folder.ID, stored.FolderID)
|
||||
require.Equal(t, types.StringArray{"概念"}, stored.CategoryPath)
|
||||
require.Equal(t, 1, stored.Depth)
|
||||
require.Equal(t, "concept/概念/将计就计", stored.WikiPath)
|
||||
|
||||
// Reading the page back through ListPages must not strip the folder name
|
||||
// either: the directory tree places pages by these fields.
|
||||
folderID := folder.ID
|
||||
listed, err := svc.ListPages(ctx, &types.WikiPageListRequest{
|
||||
KnowledgeBaseID: "kb-move",
|
||||
FolderID: &folderID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, listed.Pages, 1)
|
||||
require.Equal(t, types.StringArray{"概念"}, listed.Pages[0].CategoryPath)
|
||||
require.Equal(t, 1, listed.Pages[0].Depth)
|
||||
require.Equal(t, "concept/概念/将计就计", listed.Pages[0].WikiPath)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,6 +63,30 @@ func CleanWikiCategoryPath(parts []string) []string {
|
|||
return cleaned
|
||||
}
|
||||
|
||||
// TrimWikiFolderSegments keeps folder path segments verbatim, dropping only
|
||||
// blank entries. Folder names are validated on creation (no separators), and
|
||||
// the folder tree is the source of truth for a page's placement, so unlike
|
||||
// CleanWikiCategoryPath no page-type filtering, deduplication, or depth cap
|
||||
// applies: a user may legitimately name a folder "概念" or "Concepts".
|
||||
func TrimWikiFolderSegments(parts []string) []string {
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if part = strings.TrimSpace(part); part != "" {
|
||||
out = append(out, part)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// WikiFolderPathSegments splits a materialized folder path ("AI/RAG") into its
|
||||
// literal segments. An empty/blank path yields nil (the wiki root).
|
||||
func WikiFolderPathSegments(path string) []string {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return nil
|
||||
}
|
||||
return TrimWikiFolderSegments(strings.Split(path, "/"))
|
||||
}
|
||||
|
||||
// SplitWikiPageTypes parses a page_type value that may carry several
|
||||
// comma-separated types (e.g. "entity,concept") into a deduplicated slice,
|
||||
// dropping blanks. An empty/whitespace-only input yields nil ("no filter").
|
||||
|
|
|
|||
|
|
@ -309,3 +309,24 @@ func TestWikiConfig_JSONRoundTrip_WithGranularity(t *testing.T) {
|
|||
t.Errorf("legacy row should normalize to standard")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWikiFolderPathSegmentsKeepTypeLikeFolderNames(t *testing.T) {
|
||||
got := WikiFolderPathSegments("概念/ 概念 /Concepts//实体")
|
||||
want := []string{"概念", "概念", "Concepts", "实体"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("segments = %v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("segments[%d] = %q, want %q", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
if WikiFolderPathSegments(" ") != nil {
|
||||
t.Fatalf("blank path must yield nil")
|
||||
}
|
||||
// The model-label cleaner still drops the same names, which is why folder
|
||||
// paths must not go through it.
|
||||
if cleaned := CleanWikiCategoryPath([]string{"概念"}); len(cleaned) != 0 {
|
||||
t.Fatalf("CleanWikiCategoryPath(概念) = %v, want empty", cleaned)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue