Skip to content

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

6.6 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package treenode

import (
	"crypto/sha256"
	"fmt"
	"io/fs"
	"path/filepath"
	"sort"
	"strconv"
	"strings"

	"github.com/88250/gulu"
	"github.com/88250/lute"
	"github.com/88250/lute/ast"
	"github.com/88250/lute/parse"
	"github.com/siyuan-note/filelock"
	"github.com/siyuan-note/logging"
	"github.com/siyuan-note/siyuan/kernel/documentaddressing"
	"github.com/siyuan-note/siyuan/kernel/util"
)

func NodeHash(node *ast.Node, tree *parse.Tree, luteEngine *lute.Lute) string {
	return NodeHashWithHeadingParent(node, tree, luteEngine, HeadingParent(node))
}

// NodeHashWithHeadingParent 复用本次投影已解析的语义父级,避免重复查找前面的标题。
func NodeHashWithHeadingParent(node *ast.Node, tree *parse.Tree, luteEngine *lute.Lute, headingParent *ast.Node) string {
	var ialArray [][]string
	if 0 < len(node.KramdownIAL) {
		ialArray = make([][]string, len(node.KramdownIAL))
		copy(ialArray, node.KramdownIAL)
		sort.Slice(ialArray, func(i, j int) bool {
			return ialArray[i][0] < ialArray[j][0]
		})
	}
	ial := parse.IAL2Tokens(ialArray)
	var md string
	if ast.NodeDocument != node.Type {
		md = FormatNode(node, luteEngine)
	}
	hpath := tree.HPath
	data := tree.Box + tree.Path + hpath + string(ial) + md
	var parentID string
	if nil != node.Parent {
		parentID = node.Parent.ID
	}
	if headingParent != nil {
		parentID = headingParent.ID
	}
	data += parentID
	return fmt.Sprintf("%x", sha256.Sum256(gulu.Str.ToBytes(data)))[:7]
}

func TreeRoot(node *ast.Node) *ast.Node {
	for p := node; nil != p; p = p.Parent {
		if ast.NodeDocument == p.Type {
			return p
		}
	}
	return &ast.Node{Type: ast.NodeDocument}
}

func NewTree(boxID, p, hp, title string) *parse.Tree {
	id := util.GetTreeID(p)
	root := &ast.Node{Type: ast.NodeDocument, ID: id, Spec: "1", Box: boxID, Path: p}
	root.SetIALAttr("title", title)
	root.SetIALAttr("id", id)
	root.SetIALAttr("updated", util.TimeFromID(id))
	ret := &parse.Tree{Root: root, ID: id, Box: boxID, Path: p, HPath: hp}
	ret.Root.Spec = BaseSpec
	newPara := &ast.Node{Type: ast.NodeParagraph, ID: ast.NewNodeID(), Box: boxID, Path: p}
	newPara.SetIALAttr("id", newPara.ID)
	newPara.SetIALAttr("updated", util.TimeFromID(newPara.ID))
	ret.Root.AppendChild(newPara)
	return ret
}

func IALStr(n *ast.Node) string {
	if 1 > len(n.KramdownIAL) {
		return ""
	}
	// 这里不能进行转义,否则会导致从数据库中读取后转换为 IAL 时解析错误
	// 所以 Some symbols should not be escaped to avoid inaccurate searches https://github.com/siyuan-note/siyuan/issues/10185 无法被修复了
	return string(parse.IAL2Tokens(n.KramdownIAL))
}

func RootChildIDs(rootID string) (ret []string) {
	root := GetBlockTree(rootID)
	if nil == root {
		for _, encBoxID := range GetOpenedEncryptedBoxIDs() {
			if encRoot := GetBlockTreeInBox(rootID, encBoxID); nil != encRoot {
				root = encRoot
				break
			}
		}
	}
	if nil == root {
		return
	}

	ret = append(ret, rootID)
	boxLocalPath := filepath.Join(util.DataDir, root.BoxID)
	subFolder := filepath.Join(boxLocalPath, strings.TrimSuffix(root.Path, ".sy"))
	if !gulu.File.IsDir(subFolder) {
		return
	}
	filelock.Walk(subFolder, func(path string, d fs.DirEntry, err error) error {
		if strings.HasSuffix(path, ".sy") {
			name := filepath.Base(path)
			id := strings.TrimSuffix(name, ".sy")
			ret = append(ret, id)
		}
		return nil
	})
	return
}

func NewParagraph(id string) (ret *ast.Node) {
	newID := id
	if "" == newID {
		newID = ast.NewNodeID()
	}
	ret = &ast.Node{ID: newID, Type: ast.NodeParagraph}
	ret.SetIALAttr("id", newID)
	ret.SetIALAttr("updated", newID[:14])
	return
}

func NewSpanAnchor(id string) (ret *ast.Node) {
	return &ast.Node{Type: ast.NodeInlineHTML, Tokens: []byte("<span id=\"" + id + "\" style=\"display: none;\"></span>")}
}

func ContainOnlyDefaultIAL(tree *parse.Tree) bool {
	return 5 > len(tree.Root.KramdownIAL)
}

const BaseSpec = "2"

const DocumentTitleSpec = "6"

var CurrentSpec = DocumentTitleSpec

var ErrSpecTooNew = fmt.Errorf("the document spec is too new")

// CheckSpecJSON 按当前发行版能力校验独立源(如导入和历史),不能代替授权与认证预检。
// 在解析节点之前检查,避免未知节点被容错解析后覆盖原始内容。
func CheckSpecJSON(data []byte) error {
	spec, err := checkDocumentSourceJSON(data)
	if err != nil {
		return err
	}
	return checkDocumentSpecJSON(data, spec)
}

func CheckSpec(tree *parse.Tree) (err error) {
	err = nil
	if err = ValidateTableCellRich(tree.Root); nil != err {
		return
	}
	if CurrentSpec == tree.Root.Spec || "" == tree.Root.Spec {
		return
	}

	spec, err := strconv.Atoi(tree.Root.Spec)
	if nil != err {
		logging.LogErrorf("parse spec [%s] failed: %s", tree.Root.Spec, err)
		return
	}
	if 1 > spec {
		return fmt.Errorf("invalid document spec [%s]", tree.Root.Spec)
	}

	currentSpec, _ := strconv.Atoi(CurrentSpec)
	if spec > currentSpec {
		logging.LogErrorf("tree spec [%s] is newer than current spec [%s]", tree.Root.Spec, CurrentSpec)
		return ErrSpecTooNew
	}
	return
}

func UpgradeSpec(tree *parse.Tree) (upgraded bool) {
	oldSpec := tree.Root.Spec
	upgradeSpec1(tree)
	upgradeSpec2(tree)
	if "2" == tree.Root.Spec {
		ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
			if entering && (ast.NodeTabs == node.Type || ast.NodeTabItem == node.Type) {
				tree.Root.Spec = "3"
				return ast.WalkStop
			}
			return ast.WalkContinue
		})
	}
	if "2" == tree.Root.Spec || "3" == tree.Root.Spec {
		ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
			if entering && nil != node.TableCellRich {
				tree.Root.Spec = TableCellRichDocumentSpec
				return ast.WalkStop
			}
			return ast.WalkContinue
		})
	}
	if "2" == tree.Root.Spec || "3" == tree.Root.Spec || "4" == tree.Root.Spec {
		ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
			if entering && (ast.NodeMindmap == node.Type || ast.NodeMindmapItem == node.Type) {
				tree.Root.Spec = "5"
				return ast.WalkStop
			}
			return ast.WalkContinue
		})
	}
	if documentTitleSpecUpgradeRequired(tree.Root.Spec) &&
		documentaddressing.TitleRequiresSpec(tree.Root.IALAttr("title")) {
		tree.Root.Spec = DocumentTitleSpec
	}
	return oldSpec != tree.Root.Spec
}

func upgradeSpec2(tree *parse.Tree) {
	oldSpec, err := strconv.Atoi(tree.Root.Spec)
	if nil != err {
		logging.LogErrorf("parse spec [%s] failed: %s", tree.Root.Spec, err)
		return
	}

	if 2 <= oldSpec {
		return
	}

	// 增加了 Callout

	tree.Root.Spec = "2"
}

func upgradeSpec1(tree *parse.Tree) {
	if "" != tree.Root.Spec {
		return
	}

	parse.NestedInlines2FlattedSpans(tree, false)
	tree.Root.Spec = "1"
}