Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Respect hidden documents in the pinned section https://github.com/siyuan-note/siyuan/issues/19431

Daniel68035d507732
diff --git a/docs/PINNED-DOCUMENTS.md b/docs/PINNED-DOCUMENTS.md
index 1cb8e6522d..a3e1e6f318 100644
--- a/docs/PINNED-DOCUMENTS.md
+++ b/docs/PINNED-DOCUMENTS.md
@@ -25,6 +25,7 @@ The section appears when it contains pinned documents and hides automatically wh
| Collapse the document panel | Collapse the pinned section and clear its descendant expansion state; keep the pinned entries and their order |
| Rename or move | Reload the source document for every entry; cross-notebook moves maintain the notebook identifier |
| Close an ordinary notebook | Retain unavailable entries and allow unpinning; keep titles already read in the current session, or show the document ID when the title is unavailable |
+| Hide a document | Omit the entry but preserve its stored position; restore it when unhidden, and reject new pins while hidden. Unhide it before unpinning through the pinned section. The notebook root document's internal hidden attribute is exempt. Hidden children do not count toward the expansion arrow |
| Delete a document or notebook | Remove related entries; list reads also filter missing source documents |
| Encrypted notebook | Hide the pin operation; the server rejects additions and filters any existing invalid entries |
diff --git a/docs/PINNED-DOCUMENTS.zh-CN.md b/docs/PINNED-DOCUMENTS.zh-CN.md
index 2ae1a5c692..e218d357be 100644
--- a/docs/PINNED-DOCUMENTS.zh-CN.md
+++ b/docs/PINNED-DOCUMENTS.zh-CN.md
@@ -25,6 +25,7 @@
| 文档面板折叠 | 同时折叠置顶区域并清除其子文档展开状态,保留置顶文档及顺序 |
| 重命名或移动 | 所有入口重新读取源文档,跨笔记本移动维护笔记本标识 |
| 关闭普通笔记本 | 保留不可用入口,允许取消置顶;当前会话保留已读取的标题,无法读取标题时显示文档 ID |
+| 隐藏文档 | 隐藏入口但保留置顶记录和顺序,取消隐藏后恢复;隐藏期间不能新增置顶,需要先取消隐藏才能在置顶区操作取消置顶;笔记本根文档的内部隐藏标记不受此限制;隐藏子文档不计入展开箭头的子文档数量 |
| 删除文档或笔记本 | 清理相关入口;读取列表时也过滤不存在的源文档 |
| 加密笔记本 | 不显示置顶操作,服务端拒绝新增,也不返回已有异常入口 |
diff --git a/kernel/model/box_doc.go b/kernel/model/box_doc.go
index 55e597431e..239c9abb1c 100644
--- a/kernel/model/box_doc.go
+++ b/kernel/model/box_doc.go
@@ -288,29 +288,9 @@ func BoxDocSubFileCountForPublish(boxID string, publishAccess PublishAccess) int
}
func boxDocSubFileCount(boxID string, include func(string) bool) int {
- entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID))
- if err != nil {
- return 0
- }
- ret := 0
- for _, entry := range entries {
- if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sy") {
- continue
- }
- id := strings.TrimSuffix(entry.Name(), ".sy")
- if id == boxID || !ast.IsNodeIDPattern(id) {
- continue
- }
- p := "/" + entry.Name()
- if nil != include && !include(p) {
- continue
- }
- ial := filesys.DocIAL(filepath.Join(util.DataDir, boxID, entry.Name()))
- if "true" == ial[DocHiddenAttr] {
- continue
- }
- ret++
- }
+ ret, _ := visibleDocCount(boxID, "/", func(p string) map[string]string {
+ return filesys.DocIAL(filepath.Join(util.DataDir, boxID, p))
+ }, include)
return ret
}
diff --git a/kernel/model/doc_count.go b/kernel/model/doc_count.go
new file mode 100644
index 0000000000..7050a95252
--- /dev/null
+++ b/kernel/model/doc_count.go
@@ -0,0 +1,37 @@
+package model
+
+import (
+ "os"
+ "path"
+ "path/filepath"
+ "strings"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// 统计可见的直接子文档,属性读取沿用调用方的笔记本访问规则。
+func visibleDocCount(boxID, parentPath string, readIAL func(string) map[string]string, include func(string) bool) (int, error) {
+ entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID, parentPath))
+ if os.IsNotExist(err) {
+ return 0, nil
+ }
+ if err != nil {
+ return 0, err
+ }
+ count := 0
+ for _, entry := range entries {
+ id := strings.TrimSuffix(entry.Name(), ".sy")
+ if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sy") || !ast.IsNodeIDPattern(id) || id == boxID {
+ continue
+ }
+ p := path.Join(parentPath, entry.Name())
+ if include != nil && !include(p) {
+ continue
+ }
+ if readIAL(p)[DocHiddenAttr] != "true" {
+ count++
+ }
+ }
+ return count, nil
+}
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 4500468d5a..96ea3e4cf0 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -535,19 +535,7 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
if nil == doc {
continue
}
- subFiles, err := os.ReadDir(filepath.Join(boxLocalPath, file.path))
- if err == nil {
- for _, subFile := range subFiles {
- subDocFilePath := path.Join(file.path, subFile.Name())
- if subIAL := box.docIAL(subDocFilePath); "true" == subIAL[DocHiddenAttr] {
- continue
- }
-
- if strings.HasSuffix(subFile.Name(), ".sy") {
- doc.SubFileCount++
- }
- }
- }
+ doc.SubFileCount, _ = visibleDocCount(box.ID, file.path, box.docIAL, nil)
if flashcard {
rootID := util.GetTreeID(parentDocPath)
diff --git a/kernel/model/pinned_docs.go b/kernel/model/pinned_docs.go
index fd455f3dc0..7e656f5d27 100644
--- a/kernel/model/pinned_docs.go
+++ b/kernel/model/pinned_docs.go
@@ -129,6 +129,9 @@ func GetPinnedDocs() (ret []PinnedDoc, err error) {
if ial == nil {
return nil, fmt.Errorf("cannot read pinned document [%s]", ref.ID)
}
+ if ref.ID != boxID && ial[DocHiddenAttr] == "true" {
+ continue
+ }
file := box.docFromFileInfo(info, ial)
if file == nil {
return nil, fmt.Errorf("cannot read pinned document [%s]", ref.ID)
@@ -138,17 +141,12 @@ func GetPinnedDocs() (ret []PinnedDoc, err error) {
if file.TitleEmpty {
doc.Name = Conf.Language(16)
}
- childrenPath := strings.TrimSuffix(bt.Path, ".sy")
if ref.ID == boxID {
- childrenPath = "/"
- }
- entries, readErr := os.ReadDir(filepath.Join(util.DataDir, boxID, childrenPath))
- if readErr != nil && !os.IsNotExist(readErr) {
- return nil, readErr
- }
- for _, entry := range entries {
- if !entry.IsDir() && entry.Name() != ref.ID+".sy" && strings.HasSuffix(entry.Name(), ".sy") && ast.IsNodeIDPattern(strings.TrimSuffix(entry.Name(), ".sy")) {
- doc.SubFileCount++
+ doc.SubFileCount = BoxDocSubFileCount(boxID)
+ } else {
+ doc.SubFileCount, err = visibleDocCount(boxID, strings.TrimSuffix(bt.Path, ".sy"), box.docIAL, nil)
+ if err != nil {
+ return nil, err
}
}
ret = append(ret, doc)
@@ -194,6 +192,13 @@ func UpdatePinnedDocs(ids []string, action, targetID string, after bool) error {
if box == nil || box.Stat(bt.Path) == nil {
return fmt.Errorf("document [%s] is unavailable", id)
}
+ ial := box.docIAL(bt.Path)
+ if ial == nil {
+ return fmt.Errorf("cannot read pinned document [%s]", id)
+ }
+ if id != bt.BoxID && ial[DocHiddenAttr] == "true" {
+ return fmt.Errorf("document [%s] cannot be pinned", id)
+ }
refs = append(refs, pinnedDocRef{ID: id, Notebook: bt.BoxID})
}
if action == "pin" && selected[targetID] {
diff --git a/kernel/model/pinned_docs_test.go b/kernel/model/pinned_docs_test.go
index 2b1e2f91ff..9d13c2ff5f 100644
--- a/kernel/model/pinned_docs_test.go
+++ b/kernel/model/pinned_docs_test.go
@@ -138,7 +138,9 @@ func TestPinnedDocsClosedAndEncryptedNotebook(t *testing.T) {
func TestPinnedDocsSupportNotebookRoot(t *testing.T) {
f := setupFileOperationTest(t)
+ addFileOperationTestDoc(t, f, "20260718000005-abcdefg", "Hidden", true)
root := treenode.NewTree(f.box.ID, "/"+f.box.ID+".sy", "/Notebook", "Notebook")
+ root.Root.SetIALAttr(DocHiddenAttr, "true")
if _, err := filesys.WriteTree(root); err != nil {
t.Fatal(err)
}
@@ -168,3 +170,78 @@ func TestPinnedDocsSupportNotebookRoot(t *testing.T) {
t.Fatalf("notebook root unpin failed: %+v, %v", docs, err)
}
}
+
+func TestPinnedDocsHiddenVisibilityAndCounts(t *testing.T) {
+ f := setupFileOperationTest(t)
+ parent := addFileOperationTestDoc(t, f, "20260718000003-abcdefg", "Parent", false)
+ child := treenode.NewTree(f.box.ID, "/"+parent.ID+"/20260718000004-abcdefg.sy", "/Parent/Child", "Child")
+ writeChild := func() {
+ t.Helper()
+ if _, err := filesys.WriteTree(child); err != nil {
+ t.Fatal(err)
+ }
+ cache.RemoveDocIAL(child.Path)
+ }
+ writeChild()
+ t.Cleanup(func() { cache.RemoveTreeData(child.ID); cache.RemoveDocIAL(child.Path) })
+ if err := UpdatePinnedDocs([]string{parent.ID}, "pin", "", false); err != nil {
+ t.Fatal(err)
+ }
+ for _, hidden := range []bool{false, true, false} {
+ if hidden {
+ child.Root.SetIALAttr(DocHiddenAttr, "true")
+ } else {
+ child.Root.RemoveIALAttr(DocHiddenAttr)
+ }
+ writeChild()
+ docs, err := GetPinnedDocs()
+ children, _, listErr := ListDocTree(f.box.ID, "/"+parent.ID, util.SortModeNameASC, false, false, 100)
+ want := 1
+ if hidden {
+ want = 0
+ }
+ if len(children) != want {
+ t.Fatalf("expected %d visible children, got %d", want, len(children))
+ }
+ if err != nil || listErr != nil || len(docs) != 1 || docs[0].SubFileCount != len(children) {
+ t.Fatalf("pin count differs from visible children: %+v, %v, %v", docs, err, listErr)
+ }
+ top, _, err := ListDocTree(f.box.ID, "/", util.SortModeNameASC, false, false, 100)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, doc := range top {
+ if doc.ID == parent.ID && doc.SubFileCount != len(children) {
+ t.Fatalf("source count differs from pin: %+v", doc)
+ }
+ }
+ }
+ storagePath := filepath.Join(util.DataDir, "storage", "pinned-docs.json")
+ before, _ := os.ReadFile(storagePath)
+ for _, hidden := range []bool{true, false} {
+ if hidden {
+ parent.Root.SetIALAttr(DocHiddenAttr, "true")
+ } else {
+ parent.Root.RemoveIALAttr(DocHiddenAttr)
+ }
+ if _, err := filesys.WriteTree(parent); err != nil {
+ t.Fatal(err)
+ }
+ cache.RemoveDocIAL(parent.Path)
+ docs, err := GetPinnedDocs()
+ if err != nil || hidden && len(docs) != 0 || !hidden && len(docs) != 1 {
+ t.Fatalf("unexpected hidden pin visibility: %+v, %v", docs, err)
+ }
+ if hidden && UpdatePinnedDocs([]string{parent.ID}, "pin", "", false) == nil {
+ t.Fatal("hidden document accepted for pinning")
+ }
+ top, _, err := ListDocTree(f.box.ID, "/", util.SortModeNameASC, false, false, 100)
+ if err != nil || BoxDocSubFileCount(f.box.ID) != len(top) {
+ t.Fatalf("notebook count differs: %v", err)
+ }
+ after, _ := os.ReadFile(storagePath)
+ if string(before) != string(after) {
+ t.Fatal("visibility changed stored pins")
+ }
+ }
+}