123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- // SiYuan - Refactor your thinking
- // 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 model
- import (
- "os"
- "path/filepath"
- "sort"
- "strings"
- "unicode/utf8"
- "github.com/88250/gulu"
- "github.com/88250/lute/ast"
- "github.com/88250/lute/editor"
- "github.com/88250/lute/parse"
- "github.com/siyuan-note/logging"
- "github.com/siyuan-note/siyuan/kernel/av"
- "github.com/siyuan-note/siyuan/kernel/sql"
- "github.com/siyuan-note/siyuan/kernel/treenode"
- "github.com/siyuan-note/siyuan/kernel/util"
- )
- type BlockInfo struct {
- ID string `json:"id"`
- RootID string `json:"rootID"`
- Name string `json:"name"`
- RefCount int `json:"refCount"`
- SubFileCount int `json:"subFileCount"`
- RefIDs []string `json:"refIDs"`
- IAL map[string]string `json:"ial"`
- Icon string `json:"icon"`
- AttrViews []*AttrView `json:"attrViews"`
- }
- type AttrView struct {
- ID string `json:"id"`
- Name string `json:"name"`
- }
- func GetDocInfo(blockID string) (ret *BlockInfo) {
- WaitForWritingFiles()
- tree, err := LoadTreeByBlockID(blockID)
- if nil != err {
- logging.LogErrorf("load tree by root id [%s] failed: %s", blockID, err)
- return
- }
- title := tree.Root.IALAttr("title")
- ret = &BlockInfo{ID: blockID, RootID: tree.Root.ID, Name: title}
- ret.IAL = parse.IAL2Map(tree.Root.KramdownIAL)
- scrollData := ret.IAL["scroll"]
- if 0 < len(scrollData) {
- scroll := map[string]interface{}{}
- if parseErr := gulu.JSON.UnmarshalJSON([]byte(scrollData), &scroll); nil != parseErr {
- logging.LogWarnf("parse scroll data [%s] failed: %s", scrollData, parseErr)
- delete(ret.IAL, "scroll")
- } else {
- if zoomInId := scroll["zoomInId"]; nil != zoomInId {
- if nil == treenode.GetBlockTree(zoomInId.(string)) {
- delete(ret.IAL, "scroll")
- }
- } else {
- if startId := scroll["startId"]; nil != startId {
- if nil == treenode.GetBlockTree(startId.(string)) {
- delete(ret.IAL, "scroll")
- }
- }
- if endId := scroll["endId"]; nil != endId {
- if nil == treenode.GetBlockTree(endId.(string)) {
- delete(ret.IAL, "scroll")
- }
- }
- }
- }
- }
- ret.RefIDs, _ = sql.QueryRefIDsByDefID(blockID, false)
- ret.RefCount = len(ret.RefIDs) // 填充块引计数
- // 填充属性视图角标 Display the database title on the block superscript https://github.com/siyuan-note/siyuan/issues/10545
- avIDs := strings.Split(ret.IAL[av.NodeAttrNameAvs], ",")
- for _, avID := range avIDs {
- avName, getErr := av.GetAttributeViewName(avID)
- if nil != getErr {
- continue
- }
- if "" == avName {
- avName = Conf.language(105)
- }
- attrView := &AttrView{ID: avID, Name: avName}
- ret.AttrViews = append(ret.AttrViews, attrView)
- }
- var subFileCount int
- boxLocalPath := filepath.Join(util.DataDir, tree.Box)
- subFiles, err := os.ReadDir(filepath.Join(boxLocalPath, strings.TrimSuffix(tree.Path, ".sy")))
- if nil == err {
- for _, subFile := range subFiles {
- if strings.HasSuffix(subFile.Name(), ".sy") {
- subFileCount++
- }
- }
- }
- ret.SubFileCount = subFileCount
- ret.Icon = tree.Root.IALAttr("icon")
- return
- }
- func GetBlockRefText(id string) string {
- bt := treenode.GetBlockTree(id)
- if nil == bt {
- return ErrBlockNotFound.Error()
- }
- tree, err := LoadTreeByBlockID(id)
- if nil != err {
- return ErrTreeNotFound.Error()
- }
- node := treenode.GetNodeInTree(tree, id)
- if nil == node {
- return ErrBlockNotFound.Error()
- }
- ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering {
- return ast.WalkContinue
- }
- if n.IsTextMarkType("inline-memo") {
- // Block ref anchor text no longer contains contents of inline-level memos https://github.com/siyuan-note/siyuan/issues/9363
- n.TextMarkInlineMemoContent = ""
- return ast.WalkContinue
- }
- return ast.WalkContinue
- })
- return getNodeRefText(node)
- }
- func GetDOMText(dom string) (ret string) {
- luteEngine := NewLute()
- tree := luteEngine.BlockDOM2Tree(dom)
- ret = renderBlockText(tree.Root.FirstChild, nil)
- return
- }
- func getBlockRefText(id string, tree *parse.Tree) (ret string) {
- node := treenode.GetNodeInTree(tree, id)
- if nil == node {
- return
- }
- ret = getNodeRefText(node)
- ret = maxContent(ret, Conf.Editor.BlockRefDynamicAnchorTextMaxLen)
- return
- }
- func getNodeRefText(node *ast.Node) string {
- if nil == node {
- return ""
- }
- if ret := node.IALAttr("name"); "" != ret {
- ret = strings.TrimSpace(ret)
- ret = util.EscapeHTML(ret)
- return ret
- }
- return getNodeRefText0(node)
- }
- func getNodeRefText0(node *ast.Node) string {
- switch node.Type {
- case ast.NodeBlockQueryEmbed:
- return "Query Embed Block..."
- case ast.NodeIFrame:
- return "IFrame..."
- case ast.NodeThematicBreak:
- return "Thematic Break..."
- case ast.NodeVideo:
- return "Video..."
- case ast.NodeAudio:
- return "Audio..."
- case ast.NodeAttributeView:
- ret, _ := av.GetAttributeViewName(node.AttributeViewID)
- if "" == ret {
- ret = "Database..."
- }
- return ret
- }
- if ast.NodeDocument != node.Type && node.IsContainerBlock() {
- node = treenode.FirstLeafBlock(node)
- }
- ret := renderBlockText(node, nil)
- if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(ret) {
- ret = gulu.Str.SubStr(ret, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
- }
- return ret
- }
- func GetBlockRefIDs(id string) (refIDs, refTexts, defIDs []string) {
- refIDs = []string{}
- refTexts = []string{}
- defIDs = []string{}
- bt := treenode.GetBlockTree(id)
- if nil == bt {
- return
- }
- isDoc := bt.ID == bt.RootID
- refIDs, refTexts = sql.QueryRefIDsByDefID(id, isDoc)
- if isDoc {
- defIDs = sql.QueryChildDefIDsByRootDefID(id)
- } else {
- defIDs = append(defIDs, id)
- }
- return
- }
- func GetBlockRefIDsByFileAnnotationID(id string) (refIDs, refTexts []string) {
- refIDs, refTexts = sql.QueryRefIDsByAnnotationID(id)
- return
- }
- func GetBlockDefIDsByRefText(refText string, excludeIDs []string) (ret []string) {
- ret = sql.QueryBlockDefIDsByRefText(refText, excludeIDs)
- sort.Sort(sort.Reverse(sort.StringSlice(ret)))
- if 1 > len(ret) {
- ret = []string{}
- }
- return
- }
- func GetBlockIndex(id string) (ret int) {
- tree, _ := LoadTreeByBlockID(id)
- if nil == tree {
- return
- }
- node := treenode.GetNodeInTree(tree, id)
- if nil == node {
- return
- }
- rootChild := node
- for ; nil != rootChild.Parent && ast.NodeDocument != rootChild.Parent.Type; rootChild = rootChild.Parent {
- }
- ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering {
- return ast.WalkContinue
- }
- if !n.IsChildBlockOf(tree.Root, 1) {
- return ast.WalkContinue
- }
- ret++
- if n.ID == rootChild.ID {
- return ast.WalkStop
- }
- return ast.WalkContinue
- })
- return
- }
- func GetBlocksIndexes(ids []string) (ret map[string]int) {
- ret = map[string]int{}
- if 1 > len(ids) {
- return
- }
- tree, _ := LoadTreeByBlockID(ids[0])
- if nil == tree {
- return
- }
- idx := 0
- nodesIndexes := map[string]int{}
- ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering {
- return ast.WalkContinue
- }
- if !n.IsChildBlockOf(tree.Root, 1) {
- if n.IsBlock() {
- nodesIndexes[n.ID] = idx
- }
- return ast.WalkContinue
- }
- idx++
- nodesIndexes[n.ID] = idx
- return ast.WalkContinue
- })
- for _, id := range ids {
- ret[id] = nodesIndexes[id]
- }
- return
- }
- type BlockPath struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Type string `json:"type"`
- SubType string `json:"subType"`
- Children []*BlockPath `json:"children"`
- }
- func BuildBlockBreadcrumb(id string, excludeTypes []string) (ret []*BlockPath, err error) {
- ret = []*BlockPath{}
- tree, err := LoadTreeByBlockID(id)
- if nil == tree {
- err = nil
- return
- }
- node := treenode.GetNodeInTree(tree, id)
- if nil == node {
- return
- }
- ret = buildBlockBreadcrumb(node, excludeTypes)
- return
- }
- func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string) (ret []*BlockPath) {
- ret = []*BlockPath{}
- if nil == node {
- return
- }
- box := Conf.Box(node.Box)
- if nil == box {
- return
- }
- headingLevel := 16
- maxNameLen := 1024
- var hPath string
- baseBlock := treenode.GetBlockTreeRootByPath(node.Box, node.Path)
- if nil != baseBlock {
- hPath = baseBlock.HPath
- }
- for parent := node; nil != parent; parent = parent.Parent {
- if "" == parent.ID {
- continue
- }
- id := parent.ID
- fc := parent.FirstChild
- if nil != fc && ast.NodeTaskListItemMarker == fc.Type {
- fc = fc.Next
- }
- name := parent.IALAttr("name")
- if ast.NodeDocument == parent.Type {
- name = box.Name + hPath
- } else if ast.NodeAttributeView == parent.Type {
- name, _ = av.GetAttributeViewName(parent.AttributeViewID)
- } else {
- if "" == name {
- if ast.NodeListItem == parent.Type {
- name = gulu.Str.SubStr(renderBlockText(fc, excludeTypes), maxNameLen)
- } else {
- name = gulu.Str.SubStr(renderBlockText(parent, excludeTypes), maxNameLen)
- }
- }
- if ast.NodeHeading == parent.Type {
- headingLevel = parent.HeadingLevel
- }
- }
- add := true
- if ast.NodeList == parent.Type || ast.NodeSuperBlock == parent.Type || ast.NodeBlockquote == parent.Type {
- add = false
- }
- if ast.NodeParagraph == parent.Type && nil != parent.Parent && ast.NodeListItem == parent.Parent.Type && nil == parent.Next && (nil == parent.Previous || ast.NodeTaskListItemMarker == parent.Previous.Type) {
- add = false
- }
- if ast.NodeListItem == parent.Type {
- if "" == name {
- name = gulu.Str.SubStr(renderBlockText(fc, excludeTypes), maxNameLen)
- }
- }
- name = strings.ReplaceAll(name, editor.Caret, "")
- name = util.EscapeHTML(name)
- if add {
- ret = append([]*BlockPath{{
- ID: id,
- Name: name,
- Type: parent.Type.String(),
- SubType: treenode.SubTypeAbbr(parent),
- }}, ret...)
- }
- for prev := parent.Previous; nil != prev; prev = prev.Previous {
- b := prev
- if ast.NodeSuperBlock == prev.Type {
- // 超级块中包含标题块时下方块面包屑计算不正确 https://github.com/siyuan-note/siyuan/issues/6675
- b = treenode.SuperBlockLastHeading(prev)
- if nil == b {
- // 超级块下方块被作为嵌入块时设置显示面包屑后不渲染 https://github.com/siyuan-note/siyuan/issues/6690
- b = prev
- }
- }
- if ast.NodeHeading == b.Type && headingLevel > b.HeadingLevel {
- name = gulu.Str.SubStr(renderBlockText(b, excludeTypes), maxNameLen)
- name = util.EscapeHTML(name)
- ret = append([]*BlockPath{{
- ID: b.ID,
- Name: name,
- Type: b.Type.String(),
- SubType: treenode.SubTypeAbbr(b),
- }}, ret...)
- headingLevel = b.HeadingLevel
- }
- }
- }
- return
- }
|