2023-06-24 20:39:55 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
2022-12-03 23:49:18 +08:00
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-04 13:38:50 +08:00
|
|
|
|
"errors"
|
2022-12-03 23:49:18 +08:00
|
|
|
|
"os"
|
2023-03-30 10:44:22 +08:00
|
|
|
|
"path"
|
2022-12-03 23:49:18 +08:00
|
|
|
|
"path/filepath"
|
2025-10-24 11:12:14 +08:00
|
|
|
|
"sort"
|
2022-12-03 23:49:18 +08:00
|
|
|
|
"sync"
|
2025-10-24 11:12:14 +08:00
|
|
|
|
"time"
|
2022-12-03 23:49:18 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
2022-12-12 22:22:47 +08:00
|
|
|
|
"github.com/88250/lute/parse"
|
2022-12-03 23:49:18 +08:00
|
|
|
|
"github.com/siyuan-note/filelock"
|
|
|
|
|
|
"github.com/siyuan-note/logging"
|
2022-12-12 22:22:47 +08:00
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
2022-12-03 23:49:18 +08:00
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2022-12-10 17:07:33 +08:00
|
|
|
|
type RecentDoc struct {
|
2025-10-24 11:29:49 +08:00
|
|
|
|
RootID string `json:"rootID"`
|
|
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
|
ViewedAt int64 `json:"viewedAt"` // 浏览时间字段
|
|
|
|
|
|
ClosedAt int64 `json:"closedAt"` // 关闭时间字段
|
|
|
|
|
|
OpenAt int64 `json:"openAt"` // 文档第一次从文档树加载到页签的时间
|
2022-12-10 17:07:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 10:01:47 +08:00
|
|
|
|
type OutlineDoc struct {
|
|
|
|
|
|
DocID string `json:"docID"`
|
|
|
|
|
|
Data map[string]interface{} `json:"data"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-10 17:07:33 +08:00
|
|
|
|
var recentDocLock = sync.Mutex{}
|
|
|
|
|
|
|
2022-12-10 22:17:17 +08:00
|
|
|
|
func RemoveRecentDoc(ids []string) {
|
|
|
|
|
|
recentDocLock.Lock()
|
|
|
|
|
|
defer recentDocLock.Unlock()
|
|
|
|
|
|
|
2025-10-24 11:41:18 +08:00
|
|
|
|
recentDocs, err := getRecentDocs("")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-10 22:17:17 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ids = gulu.Str.RemoveDuplicatedElem(ids)
|
|
|
|
|
|
for i, doc := range recentDocs {
|
|
|
|
|
|
if gulu.Str.Contains(doc.RootID, ids) {
|
|
|
|
|
|
recentDocs = append(recentDocs[:i], recentDocs[i+1:]...)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = setRecentDocs(recentDocs)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-10 22:17:17 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-23 21:52:04 +08:00
|
|
|
|
func setRecentDocByTree(tree *parse.Tree) {
|
2022-12-10 22:46:24 +08:00
|
|
|
|
recentDoc := &RecentDoc{
|
2025-10-24 11:12:14 +08:00
|
|
|
|
RootID: tree.Root.ID,
|
|
|
|
|
|
Icon: tree.Root.IALAttr("icon"),
|
|
|
|
|
|
Title: tree.Root.IALAttr("title"),
|
|
|
|
|
|
ViewedAt: time.Now().Unix(), // 使用当前时间作为浏览时间
|
2025-10-24 11:29:49 +08:00
|
|
|
|
ClosedAt: 0, // 初始化关闭时间为0,表示未关闭
|
2025-10-24 11:12:14 +08:00
|
|
|
|
OpenAt: time.Now().Unix(), // 设置文档打开时间
|
2022-12-10 22:46:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-10 17:07:33 +08:00
|
|
|
|
recentDocLock.Lock()
|
|
|
|
|
|
defer recentDocLock.Unlock()
|
|
|
|
|
|
|
2025-10-24 11:41:18 +08:00
|
|
|
|
recentDocs, err := getRecentDocs("")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i, c := range recentDocs {
|
2024-06-23 21:52:04 +08:00
|
|
|
|
if c.RootID == recentDoc.RootID {
|
2022-12-12 22:27:48 +08:00
|
|
|
|
recentDocs = append(recentDocs[:i], recentDocs[i+1:]...)
|
2022-12-10 17:07:33 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-12-12 22:27:48 +08:00
|
|
|
|
|
2024-06-23 21:52:04 +08:00
|
|
|
|
recentDocs = append([]*RecentDoc{recentDoc}, recentDocs...)
|
2022-12-10 22:20:55 +08:00
|
|
|
|
if 32 < len(recentDocs) {
|
2022-12-11 09:18:41 +08:00
|
|
|
|
recentDocs = recentDocs[:32]
|
2022-12-10 17:07:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = setRecentDocs(recentDocs)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 11:29:49 +08:00
|
|
|
|
// UpdateRecentDocOpenTime 更新文档打开时间(只在第一次从文档树加载到页签时调用)
|
|
|
|
|
|
func UpdateRecentDocOpenTime(rootID string) (err error) {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
recentDocLock.Lock()
|
|
|
|
|
|
defer recentDocLock.Unlock()
|
2025-10-24 11:12:14 +08:00
|
|
|
|
|
2025-10-24 11:41:18 +08:00
|
|
|
|
recentDocs, err := getRecentDocs("")
|
2025-10-24 11:12:14 +08:00
|
|
|
|
if err != nil {
|
2025-10-24 11:29:49 +08:00
|
|
|
|
return
|
2025-10-24 11:12:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查找文档并更新打开时间
|
|
|
|
|
|
found := false
|
|
|
|
|
|
for _, doc := range recentDocs {
|
|
|
|
|
|
if doc.RootID == rootID {
|
|
|
|
|
|
doc.OpenAt = time.Now().Unix()
|
|
|
|
|
|
found = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if found {
|
|
|
|
|
|
err = setRecentDocs(recentDocs)
|
|
|
|
|
|
}
|
2025-10-24 11:29:49 +08:00
|
|
|
|
return
|
2025-10-24 11:12:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 11:29:49 +08:00
|
|
|
|
// UpdateRecentDocViewTime 更新文档浏览时间
|
|
|
|
|
|
func UpdateRecentDocViewTime(rootID string) (err error) {
|
2025-10-24 11:12:14 +08:00
|
|
|
|
recentDocLock.Lock()
|
|
|
|
|
|
defer recentDocLock.Unlock()
|
|
|
|
|
|
|
2025-10-24 11:41:18 +08:00
|
|
|
|
recentDocs, err := getRecentDocs("")
|
2025-10-24 11:12:14 +08:00
|
|
|
|
if err != nil {
|
2025-10-24 11:29:49 +08:00
|
|
|
|
return
|
2025-10-24 11:12:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查找文档并更新浏览时间
|
|
|
|
|
|
found := false
|
|
|
|
|
|
for _, doc := range recentDocs {
|
|
|
|
|
|
if doc.RootID == rootID {
|
|
|
|
|
|
doc.ViewedAt = time.Now().Unix()
|
|
|
|
|
|
found = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if found {
|
|
|
|
|
|
// 按浏览时间降序排序
|
|
|
|
|
|
sort.Slice(recentDocs, func(i, j int) bool {
|
|
|
|
|
|
return recentDocs[i].ViewedAt > recentDocs[j].ViewedAt
|
|
|
|
|
|
})
|
|
|
|
|
|
err = setRecentDocs(recentDocs)
|
|
|
|
|
|
}
|
2025-10-24 11:29:49 +08:00
|
|
|
|
return
|
2025-10-24 11:12:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 11:29:49 +08:00
|
|
|
|
// UpdateRecentDocCloseTime 更新文档关闭时间
|
|
|
|
|
|
func UpdateRecentDocCloseTime(rootID string) (err error) {
|
2025-10-24 11:12:14 +08:00
|
|
|
|
recentDocLock.Lock()
|
|
|
|
|
|
defer recentDocLock.Unlock()
|
|
|
|
|
|
|
2025-10-24 11:41:18 +08:00
|
|
|
|
recentDocs, err := getRecentDocs("")
|
2025-10-24 11:12:14 +08:00
|
|
|
|
if err != nil {
|
2025-10-24 11:29:49 +08:00
|
|
|
|
return
|
2025-10-24 11:12:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查找文档并更新关闭时间
|
|
|
|
|
|
found := false
|
|
|
|
|
|
for _, doc := range recentDocs {
|
|
|
|
|
|
if doc.RootID == rootID {
|
|
|
|
|
|
doc.ClosedAt = time.Now().Unix()
|
|
|
|
|
|
found = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if found {
|
|
|
|
|
|
err = setRecentDocs(recentDocs)
|
|
|
|
|
|
}
|
2025-10-24 11:29:49 +08:00
|
|
|
|
return
|
2025-10-24 11:12:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 11:41:18 +08:00
|
|
|
|
func GetRecentDocs(sortBy string) (ret []*RecentDoc, err error) {
|
2025-10-24 11:12:14 +08:00
|
|
|
|
recentDocLock.Lock()
|
|
|
|
|
|
defer recentDocLock.Unlock()
|
2025-10-24 11:41:18 +08:00
|
|
|
|
return getRecentDocs(sortBy)
|
2022-12-10 17:07:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func setRecentDocs(recentDocs []*RecentDoc) (err error) {
|
|
|
|
|
|
dirPath := filepath.Join(util.DataDir, "storage")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = os.MkdirAll(dirPath, 0755); err != nil {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
logging.LogErrorf("create storage [recent-doc] dir failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := gulu.JSON.MarshalIndentJSON(recentDocs, "", " ")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
logging.LogErrorf("marshal storage [recent-doc] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lsPath := filepath.Join(dirPath, "recent-doc.json")
|
|
|
|
|
|
err = filelock.WriteFile(lsPath, data)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
logging.LogErrorf("write storage [recent-doc] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 11:41:18 +08:00
|
|
|
|
func getRecentDocs(sortBy string) (ret []*RecentDoc, err error) {
|
2022-12-12 22:22:47 +08:00
|
|
|
|
tmp := []*RecentDoc{}
|
2022-12-10 17:07:33 +08:00
|
|
|
|
dataPath := filepath.Join(util.DataDir, "storage/recent-doc.json")
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(dataPath) {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := filelock.ReadFile(dataPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
logging.LogErrorf("read storage [recent-doc] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &tmp); err != nil {
|
2022-12-10 17:07:33 +08:00
|
|
|
|
logging.LogErrorf("unmarshal storage [recent-doc] failed: %s", err)
|
2025-10-31 10:12:56 +08:00
|
|
|
|
if err = setRecentDocs([]*RecentDoc{}); err != nil {
|
|
|
|
|
|
logging.LogErrorf("reset storage [recent-doc] failed: %s", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
ret = []*RecentDoc{}
|
2022-12-10 17:07:33 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-12-12 22:22:47 +08:00
|
|
|
|
|
2024-07-14 22:16:11 +08:00
|
|
|
|
var rootIDs []string
|
|
|
|
|
|
for _, doc := range tmp {
|
|
|
|
|
|
rootIDs = append(rootIDs, doc.RootID)
|
|
|
|
|
|
}
|
|
|
|
|
|
bts := treenode.GetBlockTrees(rootIDs)
|
2022-12-12 22:22:47 +08:00
|
|
|
|
var notExists []string
|
|
|
|
|
|
for _, doc := range tmp {
|
2024-07-14 22:16:11 +08:00
|
|
|
|
if bt := bts[doc.RootID]; nil != bt {
|
2023-03-30 10:44:22 +08:00
|
|
|
|
doc.Title = path.Base(bt.HPath) // Recent docs not updated after renaming https://github.com/siyuan-note/siyuan/issues/7827
|
2022-12-12 22:22:47 +08:00
|
|
|
|
ret = append(ret, doc)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
notExists = append(notExists, doc.RootID)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-24 11:29:49 +08:00
|
|
|
|
|
2022-12-12 22:22:47 +08:00
|
|
|
|
if 0 < len(notExists) {
|
|
|
|
|
|
setRecentDocs(ret)
|
|
|
|
|
|
}
|
2025-10-24 11:29:49 +08:00
|
|
|
|
|
2025-10-24 11:12:14 +08:00
|
|
|
|
// 根据排序参数进行排序
|
2025-10-24 11:41:18 +08:00
|
|
|
|
switch sortBy {
|
|
|
|
|
|
case "closedAt": // 按关闭时间排序
|
|
|
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
|
|
if ret[i].ClosedAt == 0 && ret[j].ClosedAt == 0 {
|
|
|
|
|
|
// 如果都没有关闭时间,按浏览时间排序
|
2025-10-24 11:12:14 +08:00
|
|
|
|
return ret[i].ViewedAt > ret[j].ViewedAt
|
2025-10-24 11:41:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
if ret[i].ClosedAt == 0 {
|
|
|
|
|
|
return false // 没有关闭时间的排在后面
|
|
|
|
|
|
}
|
|
|
|
|
|
if ret[j].ClosedAt == 0 {
|
|
|
|
|
|
return true // 有关闭时间的排在前面
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret[i].ClosedAt > ret[j].ClosedAt
|
|
|
|
|
|
})
|
|
|
|
|
|
case "openAt": // 按打开时间排序
|
|
|
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
|
|
if ret[i].OpenAt == 0 && ret[j].OpenAt == 0 {
|
|
|
|
|
|
// 如果都没有打开时间,按ID时间排序(ID包含时间信息)
|
|
|
|
|
|
return ret[i].RootID > ret[j].RootID
|
|
|
|
|
|
}
|
|
|
|
|
|
if ret[i].OpenAt == 0 {
|
|
|
|
|
|
return false // 没有打开时间的排在后面
|
|
|
|
|
|
}
|
|
|
|
|
|
if ret[j].OpenAt == 0 {
|
|
|
|
|
|
return true // 有打开时间的排在前面
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret[i].OpenAt > ret[j].OpenAt
|
|
|
|
|
|
})
|
|
|
|
|
|
default: // 默认按浏览时间排序
|
2025-10-24 11:12:14 +08:00
|
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
|
|
if ret[i].ViewedAt == 0 && ret[j].ViewedAt == 0 {
|
|
|
|
|
|
// 如果都没有浏览时间,按ID时间排序(ID包含时间信息)
|
|
|
|
|
|
return ret[i].RootID > ret[j].RootID
|
|
|
|
|
|
}
|
|
|
|
|
|
if ret[i].ViewedAt == 0 {
|
|
|
|
|
|
return false // 没有浏览时间的排在后面
|
|
|
|
|
|
}
|
|
|
|
|
|
if ret[j].ViewedAt == 0 {
|
|
|
|
|
|
return true // 有浏览时间的排在前面
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret[i].ViewedAt > ret[j].ViewedAt
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-12-10 17:07:33 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-03 23:49:18 +08:00
|
|
|
|
type Criterion struct {
|
2023-12-17 11:47:40 +08:00
|
|
|
|
Name string `json:"name"`
|
2024-06-23 21:52:22 +08:00
|
|
|
|
Sort int `json:"sort"` // 0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时)
|
2023-12-17 11:47:40 +08:00
|
|
|
|
Group int `json:"group"` // 0:不分组,1:按文档分组
|
|
|
|
|
|
HasReplace bool `json:"hasReplace"` // 是否有替换
|
2024-06-23 21:52:22 +08:00
|
|
|
|
Method int `json:"method"` // 0:文本,1:查询语法,2:SQL,3:正则表达式
|
2023-12-17 11:47:40 +08:00
|
|
|
|
HPath string `json:"hPath"`
|
|
|
|
|
|
IDPath []string `json:"idPath"`
|
|
|
|
|
|
K string `json:"k"` // 搜索关键字
|
|
|
|
|
|
R string `json:"r"` // 替换关键字
|
|
|
|
|
|
Types *CriterionTypes `json:"types"` // 类型过滤选项
|
|
|
|
|
|
ReplaceTypes *CriterionReplaceTypes `json:"replaceTypes"` // 替换类型过滤选项
|
2022-12-03 23:49:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type CriterionTypes struct {
|
2024-02-28 09:21:25 +08:00
|
|
|
|
MathBlock bool `json:"mathBlock"`
|
|
|
|
|
|
Table bool `json:"table"`
|
|
|
|
|
|
Blockquote bool `json:"blockquote"`
|
|
|
|
|
|
SuperBlock bool `json:"superBlock"`
|
|
|
|
|
|
Paragraph bool `json:"paragraph"`
|
|
|
|
|
|
Document bool `json:"document"`
|
|
|
|
|
|
Heading bool `json:"heading"`
|
|
|
|
|
|
List bool `json:"list"`
|
|
|
|
|
|
ListItem bool `json:"listItem"`
|
|
|
|
|
|
CodeBlock bool `json:"codeBlock"`
|
|
|
|
|
|
HtmlBlock bool `json:"htmlBlock"`
|
|
|
|
|
|
EmbedBlock bool `json:"embedBlock"`
|
|
|
|
|
|
DatabaseBlock bool `json:"databaseBlock"`
|
2024-03-22 17:25:48 +08:00
|
|
|
|
AudioBlock bool `json:"audioBlock"`
|
|
|
|
|
|
VideoBlock bool `json:"videoBlock"`
|
|
|
|
|
|
IFrameBlock bool `json:"iframeBlock"`
|
|
|
|
|
|
WidgetBlock bool `json:"widgetBlock"`
|
2022-12-03 23:49:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-17 11:47:40 +08:00
|
|
|
|
type CriterionReplaceTypes struct {
|
2024-11-04 10:50:31 +08:00
|
|
|
|
Text bool `json:"text"`
|
|
|
|
|
|
ImgText bool `json:"imgText"`
|
|
|
|
|
|
ImgTitle bool `json:"imgTitle"`
|
|
|
|
|
|
ImgSrc bool `json:"imgSrc"`
|
|
|
|
|
|
AText bool `json:"aText"`
|
|
|
|
|
|
ATitle bool `json:"aTitle"`
|
|
|
|
|
|
AHref bool `json:"aHref"`
|
|
|
|
|
|
Code bool `json:"code"`
|
|
|
|
|
|
Em bool `json:"em"`
|
|
|
|
|
|
Strong bool `json:"strong"`
|
|
|
|
|
|
InlineMath bool `json:"inlineMath"`
|
|
|
|
|
|
InlineMemo bool `json:"inlineMemo"`
|
|
|
|
|
|
BlockRef bool `json:"blockRef"`
|
|
|
|
|
|
FileAnnotationRef bool `json:"fileAnnotationRef"`
|
|
|
|
|
|
Kbd bool `json:"kbd"`
|
|
|
|
|
|
Mark bool `json:"mark"`
|
|
|
|
|
|
S bool `json:"s"`
|
|
|
|
|
|
Sub bool `json:"sub"`
|
|
|
|
|
|
Sup bool `json:"sup"`
|
|
|
|
|
|
Tag bool `json:"tag"`
|
|
|
|
|
|
U bool `json:"u"`
|
|
|
|
|
|
DocTitle bool `json:"docTitle"`
|
|
|
|
|
|
CodeBlock bool `json:"codeBlock"`
|
|
|
|
|
|
MathBlock bool `json:"mathBlock"`
|
|
|
|
|
|
HtmlBlock bool `json:"htmlBlock"`
|
2023-12-17 11:47:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-03 23:49:18 +08:00
|
|
|
|
var criteriaLock = sync.Mutex{}
|
|
|
|
|
|
|
2022-12-04 00:10:36 +08:00
|
|
|
|
func RemoveCriterion(name string) (err error) {
|
|
|
|
|
|
criteriaLock.Lock()
|
|
|
|
|
|
defer criteriaLock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
criteria, err := getCriteria()
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-04 00:10:36 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i, c := range criteria {
|
|
|
|
|
|
if c.Name == name {
|
|
|
|
|
|
criteria = append(criteria[:i], criteria[i+1:]...)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = setCriteria(criteria)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-03 23:49:18 +08:00
|
|
|
|
func SetCriterion(criterion *Criterion) (err error) {
|
2023-01-04 13:38:50 +08:00
|
|
|
|
if "" == criterion.Name {
|
|
|
|
|
|
return errors.New(Conf.Language(142))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-03 23:49:18 +08:00
|
|
|
|
criteriaLock.Lock()
|
|
|
|
|
|
defer criteriaLock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
criteria, err := getCriteria()
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
update := false
|
|
|
|
|
|
for i, c := range criteria {
|
|
|
|
|
|
if c.Name == criterion.Name {
|
|
|
|
|
|
criteria[i] = criterion
|
|
|
|
|
|
update = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !update {
|
|
|
|
|
|
criteria = append(criteria, criterion)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = setCriteria(criteria)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-23 17:06:13 +08:00
|
|
|
|
func GetCriteria() (ret []*Criterion) {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
criteriaLock.Lock()
|
|
|
|
|
|
defer criteriaLock.Unlock()
|
2022-12-23 17:06:13 +08:00
|
|
|
|
ret, _ = getCriteria()
|
|
|
|
|
|
return
|
2022-12-03 23:49:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func setCriteria(criteria []*Criterion) (err error) {
|
|
|
|
|
|
dirPath := filepath.Join(util.DataDir, "storage")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = os.MkdirAll(dirPath, 0755); err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("create storage [criteria] dir failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := gulu.JSON.MarshalIndentJSON(criteria, "", " ")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("marshal storage [criteria] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lsPath := filepath.Join(dirPath, "criteria.json")
|
|
|
|
|
|
err = filelock.WriteFile(lsPath, data)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("write storage [criteria] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getCriteria() (ret []*Criterion, err error) {
|
2022-12-03 23:57:21 +08:00
|
|
|
|
ret = []*Criterion{}
|
2022-12-03 23:49:18 +08:00
|
|
|
|
dataPath := filepath.Join(util.DataDir, "storage/criteria.json")
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(dataPath) {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := filelock.ReadFile(dataPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("read storage [criteria] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("unmarshal storage [criteria] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var localStorageLock = sync.Mutex{}
|
|
|
|
|
|
|
2023-03-22 15:21:23 +08:00
|
|
|
|
func RemoveLocalStorageVals(keys []string) (err error) {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
localStorageLock.Lock()
|
|
|
|
|
|
defer localStorageLock.Unlock()
|
|
|
|
|
|
|
2023-04-07 09:35:10 +08:00
|
|
|
|
localStorage := getLocalStorage()
|
2023-03-22 15:21:23 +08:00
|
|
|
|
for _, key := range keys {
|
|
|
|
|
|
delete(localStorage, key)
|
|
|
|
|
|
}
|
2022-12-03 23:49:18 +08:00
|
|
|
|
return setLocalStorage(localStorage)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SetLocalStorageVal(key string, val interface{}) (err error) {
|
|
|
|
|
|
localStorageLock.Lock()
|
|
|
|
|
|
defer localStorageLock.Unlock()
|
|
|
|
|
|
|
2023-04-07 09:35:10 +08:00
|
|
|
|
localStorage := getLocalStorage()
|
2022-12-03 23:49:18 +08:00
|
|
|
|
localStorage[key] = val
|
|
|
|
|
|
return setLocalStorage(localStorage)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SetLocalStorage(val interface{}) (err error) {
|
|
|
|
|
|
localStorageLock.Lock()
|
|
|
|
|
|
defer localStorageLock.Unlock()
|
|
|
|
|
|
return setLocalStorage(val)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-07 09:35:10 +08:00
|
|
|
|
func GetLocalStorage() (ret map[string]interface{}) {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
localStorageLock.Lock()
|
|
|
|
|
|
defer localStorageLock.Unlock()
|
|
|
|
|
|
return getLocalStorage()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func setLocalStorage(val interface{}) (err error) {
|
|
|
|
|
|
dirPath := filepath.Join(util.DataDir, "storage")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = os.MkdirAll(dirPath, 0755); err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("create storage [local] dir failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := gulu.JSON.MarshalIndentJSON(val, "", " ")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("marshal storage [local] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lsPath := filepath.Join(dirPath, "local.json")
|
|
|
|
|
|
err = filelock.WriteFile(lsPath, data)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("write storage [local] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-07 09:35:10 +08:00
|
|
|
|
func getLocalStorage() (ret map[string]interface{}) {
|
|
|
|
|
|
// When local.json is corrupted, clear the file to avoid being unable to enter the main interface https://github.com/siyuan-note/siyuan/issues/7911
|
2023-01-03 18:19:41 +08:00
|
|
|
|
ret = map[string]interface{}{}
|
2022-12-03 23:49:18 +08:00
|
|
|
|
lsPath := filepath.Join(util.DataDir, "storage/local.json")
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(lsPath) {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := filelock.ReadFile(lsPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("read storage [local] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2022-12-03 23:49:18 +08:00
|
|
|
|
logging.LogErrorf("unmarshal storage [local] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-15 10:01:47 +08:00
|
|
|
|
|
|
|
|
|
|
var outlineStorageLock = sync.Mutex{}
|
|
|
|
|
|
|
|
|
|
|
|
func GetOutlineStorage(docID string) (ret map[string]interface{}, err error) {
|
|
|
|
|
|
outlineStorageLock.Lock()
|
|
|
|
|
|
defer outlineStorageLock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
ret = map[string]interface{}{}
|
|
|
|
|
|
outlineDocs, err := getOutlineDocs()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, doc := range outlineDocs {
|
|
|
|
|
|
if doc.DocID == docID {
|
|
|
|
|
|
ret = doc.Data
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SetOutlineStorage(docID string, val interface{}) (err error) {
|
|
|
|
|
|
outlineStorageLock.Lock()
|
|
|
|
|
|
defer outlineStorageLock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
outlineDoc := &OutlineDoc{
|
|
|
|
|
|
DocID: docID,
|
|
|
|
|
|
Data: make(map[string]interface{}),
|
|
|
|
|
|
}
|
2025-10-19 09:46:55 +08:00
|
|
|
|
|
2025-10-15 10:01:47 +08:00
|
|
|
|
if valMap, ok := val.(map[string]interface{}); ok {
|
|
|
|
|
|
outlineDoc.Data = valMap
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
outlineDocs, err := getOutlineDocs()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果文档已存在,先移除旧的
|
|
|
|
|
|
for i, doc := range outlineDocs {
|
|
|
|
|
|
if doc.DocID == docID {
|
|
|
|
|
|
outlineDocs = append(outlineDocs[:i], outlineDocs[i+1:]...)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将新的文档信息添加到最前面
|
|
|
|
|
|
outlineDocs = append([]*OutlineDoc{outlineDoc}, outlineDocs...)
|
2025-10-19 09:46:55 +08:00
|
|
|
|
|
2025-10-15 10:01:47 +08:00
|
|
|
|
// 限制为2000个文档
|
|
|
|
|
|
if 2000 < len(outlineDocs) {
|
|
|
|
|
|
outlineDocs = outlineDocs[:2000]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = setOutlineDocs(outlineDocs)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func RemoveOutlineStorage(docID string) (err error) {
|
|
|
|
|
|
outlineStorageLock.Lock()
|
|
|
|
|
|
defer outlineStorageLock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
outlineDocs, err := getOutlineDocs()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i, doc := range outlineDocs {
|
|
|
|
|
|
if doc.DocID == docID {
|
|
|
|
|
|
outlineDocs = append(outlineDocs[:i], outlineDocs[i+1:]...)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = setOutlineDocs(outlineDocs)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func setOutlineDocs(outlineDocs []*OutlineDoc) (err error) {
|
|
|
|
|
|
dirPath := filepath.Join(util.DataDir, "storage")
|
|
|
|
|
|
if err = os.MkdirAll(dirPath, 0755); err != nil {
|
|
|
|
|
|
logging.LogErrorf("create storage [outline] dir failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := gulu.JSON.MarshalJSON(outlineDocs)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logging.LogErrorf("marshal storage [outline] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lsPath := filepath.Join(dirPath, "outline.json")
|
|
|
|
|
|
err = filelock.WriteFile(lsPath, data)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logging.LogErrorf("write storage [outline] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getOutlineDocs() (ret []*OutlineDoc, err error) {
|
|
|
|
|
|
ret = []*OutlineDoc{}
|
|
|
|
|
|
dataPath := filepath.Join(util.DataDir, "storage/outline.json")
|
|
|
|
|
|
if !filelock.IsExist(dataPath) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := filelock.ReadFile(dataPath)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logging.LogErrorf("read storage [outline] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
|
|
|
|
|
logging.LogErrorf("unmarshal storage [outline] failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|