A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
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 treenode
18
19import (
20 "crypto/sha256"
21 "fmt"
22 "io/fs"
23 "path/filepath"
24 "sort"
25 "strconv"
26 "strings"
27
28 "github.com/88250/gulu"
29 "github.com/88250/lute"
30 "github.com/88250/lute/ast"
31 "github.com/88250/lute/parse"
32 "github.com/siyuan-note/filelock"
33 "github.com/siyuan-note/logging"
34 "github.com/siyuan-note/siyuan/kernel/util"
35)
36
37func NodeHash(node *ast.Node, tree *parse.Tree, luteEngine *lute.Lute) string {
38 ialArray := node.KramdownIAL
39 sort.Slice(ialArray, func(i, j int) bool {
40 return ialArray[i][0] < ialArray[j][0]
41 })
42 ial := parse.IAL2Tokens(ialArray)
43 var md string
44 if ast.NodeDocument != node.Type {
45 md = FormatNode(node, luteEngine)
46 }
47 hpath := tree.HPath
48 data := tree.Box + tree.Path + hpath + string(ial) + md
49 var parentID string
50 if nil != node.Parent {
51 parentID = node.Parent.ID
52 }
53 if h := HeadingParent(node); nil != h {
54 parentID = h.ID
55 }
56 data += parentID
57 return fmt.Sprintf("%x", sha256.Sum256(gulu.Str.ToBytes(data)))[:7]
58}
59
60func TreeRoot(node *ast.Node) *ast.Node {
61 for p := node; nil != p; p = p.Parent {
62 if ast.NodeDocument == p.Type {
63 return p
64 }
65 }
66 return &ast.Node{Type: ast.NodeDocument}
67}
68
69func NewTree(boxID, p, hp, title string) *parse.Tree {
70 id := util.GetTreeID(p)
71 root := &ast.Node{Type: ast.NodeDocument, ID: id, Spec: "1", Box: boxID, Path: p}
72 root.SetIALAttr("title", title)
73 root.SetIALAttr("id", id)
74 root.SetIALAttr("updated", util.TimeFromID(id))
75 ret := &parse.Tree{Root: root, ID: id, Box: boxID, Path: p, HPath: hp}
76 ret.Root.Spec = CurrentSpec
77 newPara := &ast.Node{Type: ast.NodeParagraph, ID: ast.NewNodeID(), Box: boxID, Path: p}
78 newPara.SetIALAttr("id", newPara.ID)
79 newPara.SetIALAttr("updated", util.TimeFromID(newPara.ID))
80 ret.Root.AppendChild(newPara)
81 return ret
82}
83
84func IALStr(n *ast.Node) string {
85 if 1 > len(n.KramdownIAL) {
86 return ""
87 }
88 // 这里不能进行转义,否则会导致从数据库中读取后转换为 IAL 时解析错误
89 // 所以 Some symbols should not be escaped to avoid inaccurate searches https://github.com/siyuan-note/siyuan/issues/10185 无法被修复了
90 return string(parse.IAL2Tokens(n.KramdownIAL))
91}
92
93func RootChildIDs(rootID string) (ret []string) {
94 root := GetBlockTree(rootID)
95 if nil == root {
96 return
97 }
98
99 ret = append(ret, rootID)
100 boxLocalPath := filepath.Join(util.DataDir, root.BoxID)
101 subFolder := filepath.Join(boxLocalPath, strings.TrimSuffix(root.Path, ".sy"))
102 if !gulu.File.IsDir(subFolder) {
103 return
104 }
105 filelock.Walk(subFolder, func(path string, d fs.DirEntry, err error) error {
106 if strings.HasSuffix(path, ".sy") {
107 name := filepath.Base(path)
108 id := strings.TrimSuffix(name, ".sy")
109 ret = append(ret, id)
110 }
111 return nil
112 })
113 return
114}
115
116func NewParagraph(id string) (ret *ast.Node) {
117 newID := id
118 if "" == newID {
119 newID = ast.NewNodeID()
120 }
121 ret = &ast.Node{ID: newID, Type: ast.NodeParagraph}
122 ret.SetIALAttr("id", newID)
123 ret.SetIALAttr("updated", newID[:14])
124 return
125}
126
127func NewSpanAnchor(id string) (ret *ast.Node) {
128 return &ast.Node{Type: ast.NodeInlineHTML, Tokens: []byte("<span id=\"" + id + "\" style=\"display: none;\"></span>")}
129}
130
131func ContainOnlyDefaultIAL(tree *parse.Tree) bool {
132 return 5 > len(tree.Root.KramdownIAL)
133}
134
135var CurrentSpec = "2"
136
137var ErrSpecTooNew = fmt.Errorf("the document spec is too new")
138
139func CheckSpec(tree *parse.Tree) (err error) {
140 if CurrentSpec == tree.Root.Spec || "" == tree.Root.Spec {
141 return
142 }
143
144 spec, err := strconv.Atoi(tree.Root.Spec)
145 if nil != err {
146 logging.LogErrorf("parse spec [%s] failed: %s", tree.Root.Spec, err)
147 return
148 }
149
150 currentSpec, _ := strconv.Atoi(CurrentSpec)
151 if spec > currentSpec {
152 logging.LogErrorf("tree spec [%s] is newer than current spec [%s]", tree.Root.Spec, CurrentSpec)
153 return ErrSpecTooNew
154 }
155 return
156}
157
158func UpgradeSpec(tree *parse.Tree) (upgraded bool) {
159 if CurrentSpec == tree.Root.Spec {
160 return
161 }
162
163 upgradeSpec1(tree)
164 upgradeSpec2(tree)
165 return true
166}
167
168func upgradeSpec2(tree *parse.Tree) {
169 oldSpec, err := strconv.Atoi(tree.Root.Spec)
170 if nil != err {
171 logging.LogErrorf("parse spec [%s] failed: %s", tree.Root.Spec, err)
172 return
173 }
174
175 if 2 <= oldSpec {
176 return
177 }
178
179 // 增加了 Callout
180
181 tree.Root.Spec = "2"
182}
183
184func upgradeSpec1(tree *parse.Tree) {
185 if "" != tree.Root.Spec {
186 return
187 }
188
189 parse.NestedInlines2FlattedSpans(tree, false)
190 tree.Root.Spec = "1"
191 return
192}