mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-21 22:44:06 +01:00
❤️ 完整开源界面和内核 https://github.com/siyuan-note/siyuan/issues/5013
This commit is contained in:
parent
e650b8100c
commit
f40ed985e1
1214 changed files with 345766 additions and 9 deletions
301
kernel/treenode/blocktree.go
Normal file
301
kernel/treenode/blocktree.go
Normal file
|
|
@ -0,0 +1,301 @@
|
|||
// SiYuan - Build Your Eternal Digital Garden
|
||||
// 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 treenode
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/88250/flock"
|
||||
"github.com/88250/gulu"
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/88250/lute/parse"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
var blockTrees = map[string]*BlockTree{}
|
||||
var blockTreesLock = sync.Mutex{}
|
||||
var blockTreesChanged = false
|
||||
|
||||
type BlockTree struct {
|
||||
ID string // 块 ID
|
||||
RootID string // 根 ID
|
||||
ParentID string // 父 ID
|
||||
BoxID string // 笔记本 ID
|
||||
Path string // 文档物理路径
|
||||
HPath string // 文档逻辑路径
|
||||
}
|
||||
|
||||
func GetBlockTrees() map[string]*BlockTree {
|
||||
return blockTrees
|
||||
}
|
||||
|
||||
func GetBlockTreeRootByPath(boxID, path string) *BlockTree {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
for _, blockTree := range blockTrees {
|
||||
if blockTree.BoxID == boxID && blockTree.Path == path && blockTree.RootID == blockTree.ID {
|
||||
return blockTree
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetBlockTreeRootByHPath(boxID, hPath string) *BlockTree {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
for _, blockTree := range blockTrees {
|
||||
if blockTree.BoxID == boxID && blockTree.HPath == hPath && blockTree.RootID == blockTree.ID {
|
||||
return blockTree
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetBlockTree(id string) *BlockTree {
|
||||
if "" == id {
|
||||
return nil
|
||||
}
|
||||
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
return blockTrees[id]
|
||||
}
|
||||
|
||||
func SetBlockTreePath(tree *parse.Tree) {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
for _, b := range blockTrees {
|
||||
if b.RootID == tree.ID {
|
||||
b.BoxID, b.Path, b.HPath = tree.Box, tree.Path, tree.HPath
|
||||
}
|
||||
}
|
||||
blockTreesChanged = true
|
||||
}
|
||||
|
||||
func RemoveBlockTreesByRootID(rootID string) {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
var ids []string
|
||||
for _, b := range blockTrees {
|
||||
if b.RootID == rootID {
|
||||
ids = append(ids, b.RootID)
|
||||
}
|
||||
}
|
||||
for _, id := range ids {
|
||||
delete(blockTrees, id)
|
||||
}
|
||||
blockTreesChanged = true
|
||||
}
|
||||
|
||||
func RemoveBlockTreesByPathPrefix(pathPrefix string) {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
var ids []string
|
||||
for _, b := range blockTrees {
|
||||
if strings.HasPrefix(b.Path, pathPrefix) {
|
||||
ids = append(ids, b.ID)
|
||||
}
|
||||
}
|
||||
for _, id := range ids {
|
||||
delete(blockTrees, id)
|
||||
}
|
||||
blockTreesChanged = true
|
||||
}
|
||||
|
||||
func RemoveBlockTreesByBoxID(boxID string) {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
var ids []string
|
||||
for _, b := range blockTrees {
|
||||
if b.BoxID == boxID {
|
||||
ids = append(ids, b.ID)
|
||||
}
|
||||
}
|
||||
for _, id := range ids {
|
||||
delete(blockTrees, id)
|
||||
}
|
||||
blockTreesChanged = true
|
||||
}
|
||||
|
||||
func RemoveBlockTree(id string) {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
delete(blockTrees, id)
|
||||
blockTreesChanged = true
|
||||
}
|
||||
|
||||
func ReindexBlockTree(tree *parse.Tree) {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
var ids []string
|
||||
for _, b := range blockTrees {
|
||||
if b.RootID == tree.ID {
|
||||
ids = append(ids, b.ID)
|
||||
}
|
||||
}
|
||||
for _, id := range ids {
|
||||
delete(blockTrees, id)
|
||||
}
|
||||
|
||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering || !n.IsBlock() {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
var parentID string
|
||||
if nil != n.Parent {
|
||||
parentID = n.Parent.ID
|
||||
}
|
||||
if "" == n.ID {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
blockTrees[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
blockTreesChanged = true
|
||||
}
|
||||
|
||||
func IndexBlockTree(tree *parse.Tree) {
|
||||
blockTreesLock.Lock()
|
||||
defer blockTreesLock.Unlock()
|
||||
|
||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering || !n.IsBlock() {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
var parentID string
|
||||
if nil != n.Parent {
|
||||
parentID = n.Parent.ID
|
||||
}
|
||||
if "" == n.ID {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
blockTrees[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
|
||||
// 新建索引不变更持久化文件,调用处会负责调用 SaveBlockTree()
|
||||
}
|
||||
|
||||
var blocktreeFileLock *flock.Flock
|
||||
|
||||
func AutoFlushBlockTree() {
|
||||
for {
|
||||
if blockTreesChanged {
|
||||
SaveBlockTree()
|
||||
blockTreesChanged = false
|
||||
}
|
||||
time.Sleep(7 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func ReadBlockTree() (err error) {
|
||||
start := time.Now()
|
||||
|
||||
if nil == blocktreeFileLock {
|
||||
blocktreeFileLock = flock.New(util.BlockTreePath)
|
||||
}
|
||||
|
||||
if err = blocktreeFileLock.Lock(); nil != err {
|
||||
util.LogErrorf("read block tree failed: %s", err)
|
||||
os.Exit(util.ExitCodeBlockTreeErr)
|
||||
return
|
||||
}
|
||||
|
||||
fh := blocktreeFileLock.Fh()
|
||||
if _, err = fh.Seek(0, io.SeekStart); nil != err {
|
||||
util.LogErrorf("read block tree failed: %s", err)
|
||||
os.Exit(util.ExitCodeBlockTreeErr)
|
||||
return
|
||||
}
|
||||
data, err := io.ReadAll(fh)
|
||||
if nil != err {
|
||||
util.LogErrorf("read block tree failed: %s", err)
|
||||
os.Exit(util.ExitCodeBlockTreeErr)
|
||||
return
|
||||
}
|
||||
blockTreesLock.Lock()
|
||||
if err = msgpack.Unmarshal(data, &blockTrees); nil != err {
|
||||
util.LogErrorf("unmarshal block tree failed: %s", err)
|
||||
if err = os.RemoveAll(util.BlockTreePath); nil != err {
|
||||
util.LogErrorf("removed corrupted block tree failed: %s", err)
|
||||
}
|
||||
os.Exit(util.ExitCodeBlockTreeErr)
|
||||
return
|
||||
}
|
||||
blockTreesLock.Unlock()
|
||||
debug.FreeOSMemory()
|
||||
if elapsed := time.Since(start).Seconds(); 2 < elapsed {
|
||||
util.LogWarnf("read block tree [%s] to [%s], elapsed [%.2fs]", humanize.Bytes(uint64(len(data))), util.BlockTreePath, elapsed)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SaveBlockTree() {
|
||||
start := time.Now()
|
||||
|
||||
if nil == blocktreeFileLock {
|
||||
blocktreeFileLock = flock.New(util.BlockTreePath)
|
||||
}
|
||||
|
||||
if err := blocktreeFileLock.Lock(); nil != err {
|
||||
util.LogErrorf("read block tree failed: %s", err)
|
||||
os.Exit(util.ExitCodeBlockTreeErr)
|
||||
return
|
||||
}
|
||||
|
||||
blockTreesLock.Lock()
|
||||
data, err := msgpack.Marshal(blockTrees)
|
||||
if nil != err {
|
||||
util.LogErrorf("marshal block tree failed: %s", err)
|
||||
os.Exit(util.ExitCodeBlockTreeErr)
|
||||
return
|
||||
}
|
||||
blockTreesLock.Unlock()
|
||||
|
||||
fh := blocktreeFileLock.Fh()
|
||||
if err = gulu.File.WriteFileSaferByHandle(fh, data); nil != err {
|
||||
util.LogErrorf("write block tree failed: %s", err)
|
||||
os.Exit(util.ExitCodeBlockTreeErr)
|
||||
return
|
||||
}
|
||||
debug.FreeOSMemory()
|
||||
if elapsed := time.Since(start).Seconds(); 2 < elapsed {
|
||||
util.LogWarnf("save block tree [size=%s] to [%s], elapsed [%.2fs]", humanize.Bytes(uint64(len(data))), util.BlockTreePath, elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func CloseBlockTree() {
|
||||
SaveBlockTree()
|
||||
if err := blocktreeFileLock.Unlock(); nil != err {
|
||||
util.LogErrorf("close block tree failed: %s", err)
|
||||
}
|
||||
}
|
||||
165
kernel/treenode/heading.go
Normal file
165
kernel/treenode/heading.go
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
// SiYuan - Build Your Eternal Digital Garden
|
||||
// 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 treenode
|
||||
|
||||
import (
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/88250/lute/parse"
|
||||
)
|
||||
|
||||
func MoveFoldHeading(updateNode, oldNode *ast.Node) {
|
||||
foldHeadings := map[string][]*ast.Node{}
|
||||
// 找到原有节点中所有折叠标题节点的下方节点
|
||||
ast.Walk(oldNode, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if ast.NodeHeading == n.Type && "1" == n.IALAttr("fold") {
|
||||
children := FoldedHeadingChildren(n)
|
||||
foldHeadings[n.ID] = children
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
|
||||
// 将原来所有折叠标题对应的下方节点移动到新节点下
|
||||
var updateFoldHeadings []*ast.Node
|
||||
ast.Walk(updateNode, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if ast.NodeHeading == n.Type && "1" == n.IALAttr("fold") {
|
||||
updateFoldHeadings = append(updateFoldHeadings, n)
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
for _, h := range updateFoldHeadings {
|
||||
children := foldHeadings[h.ID]
|
||||
for _, c := range children {
|
||||
h.Next.InsertAfter(c) // Next 是 Block IAL
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func FoldedHeadingChildren(heading *ast.Node) (ret []*ast.Node) {
|
||||
children := HeadingChildren(heading)
|
||||
if 1 > len(children) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, c := range children {
|
||||
if "1" == c.IALAttr("heading-fold") {
|
||||
ret = append(ret, c)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func HeadingChildren(heading *ast.Node) (ret []*ast.Node) {
|
||||
start := heading.Next
|
||||
if nil == start {
|
||||
return
|
||||
}
|
||||
if ast.NodeKramdownBlockIAL == start.Type {
|
||||
start = start.Next // 跳过 heading 的 IAL
|
||||
}
|
||||
|
||||
currentLevel := heading.HeadingLevel
|
||||
for n := start; nil != n; n = n.Next {
|
||||
if ast.NodeHeading == n.Type {
|
||||
if currentLevel >= n.HeadingLevel {
|
||||
break
|
||||
}
|
||||
} else if ast.NodeSuperBlock == n.Type {
|
||||
if h := SuperBlockHeading(n); nil != h {
|
||||
if currentLevel >= h.HeadingLevel {
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if ast.NodeSuperBlockCloseMarker == n.Type {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, n)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SuperBlockHeading(sb *ast.Node) *ast.Node {
|
||||
c := sb.FirstChild.Next.Next
|
||||
if nil == c {
|
||||
return nil
|
||||
}
|
||||
|
||||
if ast.NodeHeading == c.Type {
|
||||
return c
|
||||
}
|
||||
|
||||
if ast.NodeSuperBlock == c.Type {
|
||||
return SuperBlockHeading(c)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func HeadingParent(node *ast.Node) *ast.Node {
|
||||
if nil == node {
|
||||
return nil
|
||||
}
|
||||
|
||||
currentLevel := 16
|
||||
if ast.NodeHeading == node.Type {
|
||||
currentLevel = node.HeadingLevel
|
||||
}
|
||||
|
||||
for n := node.Previous; nil != n; n = n.Previous {
|
||||
if ast.NodeHeading == n.Type && n.HeadingLevel < currentLevel {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return node.Parent
|
||||
}
|
||||
|
||||
func HeadingLevel(node *ast.Node) int {
|
||||
if nil == node {
|
||||
return 0
|
||||
}
|
||||
|
||||
for n := node; nil != n; n = n.Previous {
|
||||
if ast.NodeHeading == n.Type {
|
||||
return n.HeadingLevel
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func TopHeadingLevel(tree *parse.Tree) (ret int) {
|
||||
ret = 7
|
||||
for n := tree.Root.FirstChild; nil != n; n = n.Next {
|
||||
if ast.NodeHeading == n.Type {
|
||||
if ret > n.HeadingLevel {
|
||||
ret = n.HeadingLevel
|
||||
}
|
||||
}
|
||||
}
|
||||
if 7 == ret { // 没有出现过标题时
|
||||
ret = 0
|
||||
}
|
||||
return
|
||||
}
|
||||
47
kernel/treenode/marker.go
Normal file
47
kernel/treenode/marker.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// SiYuan - Build Your Eternal Digital Garden
|
||||
// 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 treenode
|
||||
|
||||
import (
|
||||
"github.com/88250/lute/lex"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
func ContainsMarker(str string) bool {
|
||||
if !util.IsASCII(str) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, token := range str {
|
||||
if IsMarker(byte(token)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsMarker(token byte) bool {
|
||||
switch token {
|
||||
case lex.ItemAsterisk, lex.ItemUnderscore, lex.ItemOpenBracket, lex.ItemBang, lex.ItemNewline, lex.ItemBackslash, lex.ItemBacktick, lex.ItemLess,
|
||||
lex.ItemCloseBracket, lex.ItemAmpersand, lex.ItemTilde, lex.ItemDollar, lex.ItemOpenBrace, lex.ItemOpenParen, lex.ItemEqual, lex.ItemCrosshatch:
|
||||
return true
|
||||
case lex.ItemCaret:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
318
kernel/treenode/node.go
Normal file
318
kernel/treenode/node.go
Normal file
|
|
@ -0,0 +1,318 @@
|
|||
// SiYuan - Build Your Eternal Digital Garden
|
||||
// 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 treenode
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/88250/lute"
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/88250/lute/html"
|
||||
"github.com/88250/lute/lex"
|
||||
"github.com/88250/lute/parse"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
func NodeStaticMdContent(node *ast.Node, luteEngine *lute.Lute) (md, content string) {
|
||||
md = FormatNode(node, luteEngine)
|
||||
content = NodeStaticContent(node)
|
||||
return
|
||||
}
|
||||
|
||||
func FormatNode(node *ast.Node, luteEngine *lute.Lute) string {
|
||||
markdown, err := lute.FormatNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
|
||||
if nil != err {
|
||||
root := TreeRoot(node)
|
||||
util.LogFatalf("format node [%s] in tree [%s] failed: %s", node.ID, root.ID, err)
|
||||
}
|
||||
return markdown
|
||||
}
|
||||
|
||||
func NodeStaticContent(node *ast.Node) string {
|
||||
if nil == node {
|
||||
return ""
|
||||
}
|
||||
|
||||
if ast.NodeDocument == node.Type {
|
||||
return node.IALAttr("title")
|
||||
}
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
buf.Grow(4096)
|
||||
lastSpace := false
|
||||
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if n.IsContainerBlock() {
|
||||
if !lastSpace {
|
||||
buf.WriteString(" ")
|
||||
lastSpace = true
|
||||
}
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
switch n.Type {
|
||||
case ast.NodeTagOpenMarker, ast.NodeTagCloseMarker:
|
||||
buf.WriteByte('#')
|
||||
case ast.NodeBlockRef:
|
||||
buf.WriteString(GetDynamicBlockRefText(n))
|
||||
lastSpace = false
|
||||
return ast.WalkSkipChildren
|
||||
case ast.NodeText, ast.NodeLinkText, ast.NodeLinkTitle, ast.NodeFileAnnotationRefText, ast.NodeFootnotesRef,
|
||||
ast.NodeCodeSpanContent, ast.NodeInlineMathContent, ast.NodeCodeBlockCode, ast.NodeMathBlockContent, ast.NodeHTMLBlock:
|
||||
buf.Write(n.Tokens)
|
||||
case ast.NodeBackslash:
|
||||
buf.WriteByte(lex.ItemBackslash)
|
||||
case ast.NodeBackslashContent:
|
||||
buf.Write(n.Tokens)
|
||||
}
|
||||
lastSpace = false
|
||||
return ast.WalkContinue
|
||||
})
|
||||
return strings.TrimSpace(buf.String())
|
||||
}
|
||||
|
||||
func FirstLeafBlock(node *ast.Node) (ret *ast.Node) {
|
||||
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering || n.IsMarker() {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if !n.IsContainerBlock() {
|
||||
ret = n
|
||||
return ast.WalkStop
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func CountBlockNodes(node *ast.Node) (ret int) {
|
||||
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering || !n.IsBlock() || ast.NodeList == n.Type || ast.NodeBlockquote == n.Type || ast.NodeSuperBlock == n.Type {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if "1" == n.IALAttr("fold") {
|
||||
ret++
|
||||
return ast.WalkSkipChildren
|
||||
}
|
||||
|
||||
ret++
|
||||
return ast.WalkContinue
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func ParentNodes(node *ast.Node) (parents []*ast.Node) {
|
||||
for n := node.Parent; nil != n; n = n.Parent {
|
||||
parents = append(parents, n)
|
||||
if ast.NodeDocument == n.Type {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ParentBlock(node *ast.Node) *ast.Node {
|
||||
for p := node.Parent; nil != p; p = p.Parent {
|
||||
if "" != p.ID && p.IsBlock() {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetNodeInTree(tree *parse.Tree, id string) (ret *ast.Node) {
|
||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if id == n.ID {
|
||||
ret = n
|
||||
ret.Box = tree.Box
|
||||
ret.Path = tree.Path
|
||||
return ast.WalkStop
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func GetDocTitleImgPath(root *ast.Node) (ret string) {
|
||||
if nil == root {
|
||||
return
|
||||
}
|
||||
|
||||
const background = "background-image: url("
|
||||
titleImg := root.IALAttr("title-img")
|
||||
titleImg = strings.TrimSpace(titleImg)
|
||||
titleImg = html.UnescapeString(titleImg)
|
||||
titleImg = strings.ReplaceAll(titleImg, "background-image:url(", background)
|
||||
if !strings.Contains(titleImg, background) {
|
||||
return
|
||||
}
|
||||
|
||||
start := strings.Index(titleImg, background) + len(background)
|
||||
end := strings.LastIndex(titleImg, ")")
|
||||
ret = titleImg[start:end]
|
||||
ret = strings.TrimPrefix(ret, "\"")
|
||||
ret = strings.TrimPrefix(ret, "'")
|
||||
ret = strings.TrimSuffix(ret, "\"")
|
||||
ret = strings.TrimSuffix(ret, "'")
|
||||
return ret
|
||||
}
|
||||
|
||||
var typeAbbrMap = map[string]string{
|
||||
// 块级元素
|
||||
"NodeDocument": "d",
|
||||
"NodeHeading": "h",
|
||||
"NodeList": "l",
|
||||
"NodeListItem": "i",
|
||||
"NodeCodeBlock": "c",
|
||||
"NodeMathBlock": "m",
|
||||
"NodeTable": "t",
|
||||
"NodeBlockquote": "b",
|
||||
"NodeSuperBlock": "s",
|
||||
"NodeParagraph": "p",
|
||||
"NodeHTMLBlock": "html",
|
||||
"NodeBlockQueryEmbed": "query_embed",
|
||||
"NodeKramdownBlockIAL": "ial",
|
||||
"NodeIFrame": "iframe",
|
||||
"NodeWidget": "widget",
|
||||
"NodeThematicBreak": "tb",
|
||||
"NodeVideo": "video",
|
||||
"NodeAudio": "audio",
|
||||
// 行级元素
|
||||
"NodeText": "text",
|
||||
"NodeLinkText": "link_text",
|
||||
"NodeTag": "tag",
|
||||
"NodeCodeSpan": "code_span",
|
||||
"NodeInlineMath": "inline_math",
|
||||
"NodeBlockRefID": "ref_id",
|
||||
"NodeEmphasis": "em",
|
||||
"NodeStrong": "strong",
|
||||
"NodeStrikethrough": "strikethrough",
|
||||
"NodeMark": "mark",
|
||||
"NodeSup": "sup",
|
||||
"NodeSub": "sub",
|
||||
"NodeKbd": "kbd",
|
||||
"NodeUnderline": "underline",
|
||||
}
|
||||
|
||||
var abbrTypeMap = map[string]string{}
|
||||
|
||||
func init() {
|
||||
for typ, abbr := range typeAbbrMap {
|
||||
abbrTypeMap[abbr] = typ
|
||||
}
|
||||
}
|
||||
|
||||
func TypeAbbr(nodeType string) string {
|
||||
return typeAbbrMap[nodeType]
|
||||
}
|
||||
|
||||
func FromAbbrType(abbrType string) string {
|
||||
return abbrTypeMap[abbrType]
|
||||
}
|
||||
|
||||
func SubTypeAbbr(n *ast.Node) string {
|
||||
switch n.Type {
|
||||
case ast.NodeList, ast.NodeListItem:
|
||||
if 0 == n.ListData.Typ {
|
||||
return "u"
|
||||
}
|
||||
if 1 == n.ListData.Typ {
|
||||
return "o"
|
||||
}
|
||||
if 3 == n.ListData.Typ {
|
||||
return "t"
|
||||
}
|
||||
case ast.NodeHeading:
|
||||
if 1 == n.HeadingLevel {
|
||||
return "h1"
|
||||
}
|
||||
if 2 == n.HeadingLevel {
|
||||
return "h2"
|
||||
}
|
||||
if 3 == n.HeadingLevel {
|
||||
return "h3"
|
||||
}
|
||||
if 4 == n.HeadingLevel {
|
||||
return "h4"
|
||||
}
|
||||
if 5 == n.HeadingLevel {
|
||||
return "h5"
|
||||
}
|
||||
if 6 == n.HeadingLevel {
|
||||
return "h6"
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetLegacyDynamicBlockRefDefIDs(node *ast.Node) (ret []string) {
|
||||
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
if ast.NodeBlockRefID == n.Type && ast.NodeCloseParen == n.Next.Type {
|
||||
ret = append(ret, n.TokensStr())
|
||||
return ast.WalkSkipChildren
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func SetDynamicBlockRefText(blockRef *ast.Node, refText string) {
|
||||
if nil == blockRef {
|
||||
return
|
||||
}
|
||||
|
||||
idNode := blockRef.ChildByType(ast.NodeBlockRefID)
|
||||
if nil == idNode {
|
||||
return
|
||||
}
|
||||
|
||||
var spacesRefTexts []*ast.Node // 可能会有多个空格,或者遗留错误插入的锚文本节点,这里做一次订正
|
||||
for n := idNode.Next; ast.NodeCloseParen != n.Type; n = n.Next {
|
||||
spacesRefTexts = append(spacesRefTexts, n)
|
||||
}
|
||||
for _, toRemove := range spacesRefTexts {
|
||||
toRemove.Unlink()
|
||||
}
|
||||
refText = strings.TrimSpace(refText)
|
||||
idNode.InsertAfter(&ast.Node{Type: ast.NodeBlockRefDynamicText, Tokens: []byte(refText)})
|
||||
idNode.InsertAfter(&ast.Node{Type: ast.NodeBlockRefSpace})
|
||||
}
|
||||
|
||||
func GetDynamicBlockRefText(blockRef *ast.Node) string {
|
||||
refText := blockRef.ChildByType(ast.NodeBlockRefText)
|
||||
if nil != refText {
|
||||
return refText.Text()
|
||||
}
|
||||
refText = blockRef.ChildByType(ast.NodeBlockRefDynamicText)
|
||||
if nil != refText {
|
||||
return refText.Text()
|
||||
}
|
||||
return "ref resolve failed"
|
||||
}
|
||||
98
kernel/treenode/tree.go
Normal file
98
kernel/treenode/tree.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
// SiYuan - Build Your Eternal Digital Garden
|
||||
// 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 treenode
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/88250/gulu"
|
||||
"github.com/88250/lute"
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/88250/lute/parse"
|
||||
util2 "github.com/88250/lute/util"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
func NodeHash(node *ast.Node, tree *parse.Tree, luteEngine *lute.Lute) string {
|
||||
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.Path + hpath + string(ial) + md
|
||||
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 := strings.TrimSuffix(path.Base(p), ".sy")
|
||||
root := &ast.Node{Type: ast.NodeDocument, ID: id}
|
||||
root.SetIALAttr("title", title)
|
||||
root.SetIALAttr("id", id)
|
||||
root.SetIALAttr("updated", util.TimeFromID(id))
|
||||
ret := &parse.Tree{Root: root}
|
||||
ret.Box = boxID
|
||||
ret.Path = p
|
||||
ret.HPath = hp
|
||||
newPara := &ast.Node{Type: ast.NodeParagraph, ID: ast.NewNodeID()}
|
||||
newPara.SetIALAttr("id", newPara.ID)
|
||||
newPara.SetIALAttr("updated", util.TimeFromID(newPara.ID))
|
||||
ret.Root.AppendChild(newPara)
|
||||
return ret
|
||||
}
|
||||
|
||||
func IsEmptyBlockIAL(n *ast.Node) bool {
|
||||
if ast.NodeKramdownBlockIAL != n.Type {
|
||||
return false
|
||||
}
|
||||
|
||||
if util2.IsDocIAL(n.Tokens) {
|
||||
return false
|
||||
}
|
||||
|
||||
if nil != n.Previous {
|
||||
if ast.NodeKramdownBlockIAL == n.Previous.Type {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func IALStr(n *ast.Node) string {
|
||||
if 1 > len(n.KramdownIAL) {
|
||||
return ""
|
||||
}
|
||||
return string(parse.IAL2Tokens(n.KramdownIAL))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue