This commit is contained in:
Daniel 2025-03-26 11:51:37 +08:00
parent a09d460401
commit 251ce2b2b8
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 75 additions and 13 deletions

View file

@ -35,6 +35,59 @@ import (
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
) )
func moveLocalShorthands(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
notebook := arg["notebook"].(string)
if util.InvalidIDPattern(notebook, ret) {
return
}
var parentID string
parentIDArg := arg["parentID"]
if nil != parentIDArg {
parentID = parentIDArg.(string)
}
id := ast.NewNodeID()
idArg := arg["id"]
if nil != idArg {
id = idArg.(string)
}
hPath := arg["path"].(string)
baseName := path.Base(hPath)
dir := path.Dir(hPath)
r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
baseName = r.ReplaceAllString(baseName, "")
if 512 < utf8.RuneCountInString(baseName) {
baseName = gulu.Str.SubStr(baseName, 512)
}
hPath = path.Join(dir, baseName)
if !strings.HasPrefix(hPath, "/") {
hPath = "/" + hPath
}
id, err := model.MoveLocalShorthands(notebook, hPath, parentID, id)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
return
}
ret.Data = id
model.FlushTxQueue()
box := model.Conf.Box(notebook)
b, _ := model.GetBlock(id, nil)
pushCreate(box, b.Path, arg)
}
func listDocTree(c *gin.Context) { func listDocTree(c *gin.Context) {
// Add kernel API `/api/filetree/listDocTree` https://github.com/siyuan-note/siyuan/issues/10482 // Add kernel API `/api/filetree/listDocTree` https://github.com/siyuan-note/siyuan/issues/10482

View file

@ -44,7 +44,6 @@ func StartCron() {
go every(30*time.Second, model.HookDesktopUIProcJob) go every(30*time.Second, model.HookDesktopUIProcJob)
go every(24*time.Hour, model.AutoPurgeRepoJob) go every(24*time.Hour, model.AutoPurgeRepoJob)
go every(30*time.Minute, model.AutoCheckMicrosoftDefender) go every(30*time.Minute, model.AutoCheckMicrosoftDefender)
go every(7*time.Second, model.ShortcutsAppendToDailynote)
} }
func every(interval time.Duration, f func()) { func every(interval time.Duration, f func()) {

View file

@ -27,15 +27,15 @@ import (
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
) )
func ShortcutsAppendToDailynote() { func MoveLocalShorthands(boxID, hPath, parentID, id string) (retID string, err error) {
dailynotesDir := filepath.Join(util.ShortcutsPath, "dailynotes") shorthandsDir := filepath.Join(util.ShortcutsPath, "shorthands")
if !gulu.File.IsDir(dailynotesDir) { if !gulu.File.IsDir(shorthandsDir) {
return return
} }
dir, err := os.ReadDir(dailynotesDir) dir, err := os.ReadDir(shorthandsDir)
if nil != err { if nil != err {
logging.LogErrorf("read dir [%s] failed: %s", dailynotesDir, err) logging.LogErrorf("read dir [%s] failed: %s", shorthandsDir, err)
return return
} }
@ -50,7 +50,7 @@ func ShortcutsAppendToDailynote() {
continue continue
} }
p := filepath.Join(dailynotesDir, entry.Name()) p := filepath.Join(shorthandsDir, entry.Name())
data, readErr := os.ReadFile(p) data, readErr := os.ReadFile(p)
if nil != readErr { if nil != readErr {
logging.LogErrorf("read file [%s] failed: %s", p, readErr) logging.LogErrorf("read file [%s] failed: %s", p, readErr)
@ -59,22 +59,32 @@ func ShortcutsAppendToDailynote() {
buff.Write(bytes.TrimSpace(data)) buff.Write(bytes.TrimSpace(data))
buff.WriteString("\n\n") buff.WriteString("\n\n")
logging.LogInfof("read dailynote [%s] content [%s]", p, data) logging.LogInfof("read shorthand [%s] content [%s]", p, data)
toRemoves = append(toRemoves, p) toRemoves = append(toRemoves, p)
} }
defer func() { clearShorthand := func(toRemoves []string) {
for _, p := range toRemoves { for _, p := range toRemoves {
if err := os.Remove(p); nil != err { if removeErr := os.Remove(p); nil != removeErr {
logging.LogErrorf("remove file [%s] failed: %s", p, err) logging.LogErrorf("remove file [%s] failed: %s", p, removeErr)
} }
} }
}() }
content := strings.TrimSpace(buff.String()) content := strings.TrimSpace(buff.String())
if 1 > len(content) { if 1 > len(content) {
clearShorthand(toRemoves)
return return
} }
logging.LogInfof("append dailynote content [%s]", content) logging.LogInfof("shorthand content [%s]", content)
retID, err = CreateWithMarkdown("", boxID, hPath, content, parentID, id, false, "")
if nil != err {
logging.LogErrorf("create doc failed: %s", err)
return
}
clearShorthand(toRemoves)
return
} }