A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
at lambda-fork/main 220 lines 5.7 kB view raw
1// SiYuan - Refactor your thinking 2// Copyright (c) 2020-present, b3log.org 3// 4// This program is free software: you can redistribute it and/or modify 5// it under the terms of the GNU Affero General Public License as published by 6// the Free Software Foundation, either version 3 of the License, or 7// (at your option) any later version. 8// 9// This program is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU Affero General Public License for more details. 13// 14// You should have received a copy of the GNU Affero General Public License 15// along with this program. If not, see <https://www.gnu.org/licenses/>. 16 17package model 18 19import ( 20 "bytes" 21 "os" 22 "path/filepath" 23 "slices" 24 "strconv" 25 "strings" 26 "time" 27 28 "github.com/88250/gulu" 29 "github.com/88250/lute/ast" 30 "github.com/88250/lute/parse" 31 "github.com/siyuan-note/logging" 32 "github.com/siyuan-note/siyuan/kernel/treenode" 33 "github.com/siyuan-note/siyuan/kernel/util" 34) 35 36func MoveLocalShorthands(boxID, hPath, parentID string) (retIDs []string, err error) { 37 shorthandsDir := filepath.Join(util.ShortcutsPath, "shorthands") 38 if !gulu.File.IsDir(shorthandsDir) { 39 return 40 } 41 42 entries, err := os.ReadDir(shorthandsDir) 43 if nil != err { 44 logging.LogErrorf("read dir [%s] failed: %s", shorthandsDir, err) 45 return 46 } 47 48 assetsDir := filepath.Join(util.DataDir, "assets") 49 for _, entry := range entries { 50 if entry.IsDir() && "assets" == entry.Name() { 51 assetsEntries, readErr := os.ReadDir(filepath.Join(shorthandsDir, entry.Name())) 52 if nil != readErr { 53 logging.LogErrorf("read dir [%s] failed: %s", shorthandsDir, readErr) 54 continue 55 } 56 for _, assetEntry := range assetsEntries { 57 if assetEntry.IsDir() { 58 continue 59 } 60 61 p := filepath.Join(shorthandsDir, entry.Name(), assetEntry.Name()) 62 assetWritePath := filepath.Join(assetsDir, assetEntry.Name()) 63 if renameErr := os.Rename(p, assetWritePath); nil != renameErr { 64 logging.LogErrorf("rename file [%s] to [%s] failed: %s", p, assetWritePath, renameErr) 65 continue 66 } 67 } 68 } 69 } 70 71 var toRemoves []string 72 box := Conf.Box(boxID) 73 74 if "" == hPath { // hPath 为空的话每一个速记对应创建一个文档记录 75 for _, entry := range entries { 76 if filepath.Ext(entry.Name()) != ".md" { 77 continue 78 } 79 80 p := filepath.Join(shorthandsDir, entry.Name()) 81 data, readErr := os.ReadFile(p) 82 if nil != readErr { 83 logging.LogErrorf("read file [%s] failed: %s", p, readErr) 84 continue 85 } 86 87 content := string(bytes.TrimSpace(data)) 88 if "" == content { 89 toRemoves = append(toRemoves, p) 90 continue 91 } 92 93 t := strings.TrimSuffix(entry.Name(), ".md") 94 i, parseErr := strconv.ParseInt(t, 10, 64) 95 if nil != parseErr { 96 logging.LogErrorf("parse [%s] to int failed: %s", t, parseErr) 97 continue 98 } 99 hPath = "/" + time.UnixMilli(i).Format("2006-01-02 15:04:05") 100 var retID string 101 retID, err = CreateWithMarkdown("", boxID, hPath, content, parentID, "", false, "") 102 if nil != err { 103 logging.LogErrorf("create doc failed: %s", err) 104 return 105 } 106 107 retIDs = append(retIDs, retID) 108 toRemoves = append(toRemoves, p) 109 box.addMinSort("/", retID) 110 } 111 } else { // 不为空的话将所有速记合并到指定路径的文档中 112 if !strings.HasPrefix(hPath, "/") { 113 hPath = "/" + hPath 114 } 115 116 buff := bytes.Buffer{} 117 for _, entry := range entries { 118 if filepath.Ext(entry.Name()) != ".md" { 119 continue 120 } 121 122 p := filepath.Join(shorthandsDir, entry.Name()) 123 data, readErr := os.ReadFile(p) 124 if nil != readErr { 125 logging.LogErrorf("read file [%s] failed: %s", p, readErr) 126 continue 127 } 128 129 content := string(bytes.TrimSpace(data)) 130 if "" == content { 131 toRemoves = append(toRemoves, p) 132 continue 133 } 134 135 buff.WriteString(content) 136 buff.WriteString("\n\n") 137 toRemoves = append(toRemoves, p) 138 } 139 140 if 0 < buff.Len() { 141 bt := treenode.GetBlockTreeRootByHPath(boxID, hPath) 142 if nil == bt { 143 var retID string 144 retID, err = CreateWithMarkdown("", boxID, hPath, buff.String(), parentID, "", false, "") 145 if nil != err { 146 logging.LogErrorf("create doc failed: %s", err) 147 return 148 } 149 retIDs = append(retIDs, retID) 150 } else { 151 var tree *parse.Tree 152 tree, err = loadTreeByBlockTree(bt) 153 if nil != err { 154 logging.LogErrorf("load tree by block tree failed: %s", err) 155 return 156 } 157 var last *ast.Node 158 for c := tree.Root.FirstChild; nil != c; c = c.Next { 159 last = c 160 } 161 162 luteEngine := util.NewStdLute() 163 inputTree := parse.Parse("", buff.Bytes(), luteEngine.ParseOptions) 164 165 if nil != inputTree { 166 var nodes []*ast.Node 167 for c := inputTree.Root.FirstChild; nil != c; c = c.Next { 168 nodes = append(nodes, c) 169 } 170 slices.Reverse(nodes) 171 for _, node := range nodes { 172 last.InsertAfter(node) 173 } 174 } 175 176 indexWriteTreeIndexQueue(tree) 177 } 178 179 } 180 } 181 182 for _, p := range toRemoves { 183 if removeErr := os.Remove(p); nil != removeErr { 184 logging.LogErrorf("remove file [%s] failed: %s", p, removeErr) 185 } 186 } 187 return 188} 189 190func WatchLocalShorthands() { 191 shorthandsDir := filepath.Join(util.ShortcutsPath, "shorthands") 192 if !gulu.File.IsDir(shorthandsDir) { 193 return 194 } 195 196 entries, err := os.ReadDir(shorthandsDir) 197 if nil != err { 198 logging.LogErrorf("read dir [%s] failed: %s", shorthandsDir, err) 199 return 200 } 201 202 shorthandCount := 0 203 for _, entry := range entries { 204 if entry.IsDir() { 205 continue 206 } 207 208 if filepath.Ext(entry.Name()) != ".md" { 209 continue 210 } 211 212 shorthandCount++ 213 } 214 215 if 1 > shorthandCount { 216 return 217 } 218 219 util.PushLocalShorthandCount(shorthandCount) 220}