From f4982d2cd6a6aaf709f56f062503308462cd7d33 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 17 Jun 2023 15:49:16 +0800 Subject: [PATCH] :art: Preview mode supports jumping through the outline panel https://github.com/siyuan-note/siyuan/issues/3059 --- kernel/api/export.go | 5 +++-- kernel/go.mod | 2 +- kernel/go.sum | 4 ++-- kernel/model/backlink.go | 6 +++--- kernel/model/export.go | 10 ++++++++-- kernel/model/outline.go | 10 ++++++++-- kernel/model/path.go | 5 +++-- 7 files changed, 28 insertions(+), 14 deletions(-) diff --git a/kernel/api/export.go b/kernel/api/export.go index 503c86518..db3665156 100644 --- a/kernel/api/export.go +++ b/kernel/api/export.go @@ -498,9 +498,10 @@ func exportPreview(c *gin.Context) { } id := arg["id"].(string) - stdHTML := model.Preview(id) + stdHTML, outline := model.Preview(id) ret.Data = map[string]interface{}{ - "html": stdHTML, + "html": stdHTML, + "outline": outline, } } diff --git a/kernel/go.mod b/kernel/go.mod index e136b8101..90dcc1b1f 100644 --- a/kernel/go.mod +++ b/kernel/go.mod @@ -6,7 +6,7 @@ require ( github.com/88250/clipboard v0.1.5 github.com/88250/css v0.1.2 github.com/88250/gulu v1.2.3-0.20230615033005-b519d6875346 - github.com/88250/lute v1.7.6-0.20230608014232-e10343eb445d + github.com/88250/lute v1.7.6-0.20230617074457-ffab89d50887 github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 github.com/ClarkThan/ahocorasick v0.0.0-20230220142845-f237b6348b3e diff --git a/kernel/go.sum b/kernel/go.sum index e9dd435cb..cd003c4fc 100644 --- a/kernel/go.sum +++ b/kernel/go.sum @@ -8,8 +8,8 @@ github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5 h1:8HdZozCsXS github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/88250/gulu v1.2.3-0.20230615033005-b519d6875346 h1:U0wZN6zuf+vUhzdHC6DbZE/bY5FUt3FuCwn/KgObV44= github.com/88250/gulu v1.2.3-0.20230615033005-b519d6875346/go.mod h1:pTWnjt+6qUqNnP9xltswsJxgCBVu3C7eW09u48LWX0k= -github.com/88250/lute v1.7.6-0.20230608014232-e10343eb445d h1:D3HPp3lcibUYqq6hu/dPP6rx+To23VyE96eMBhwFoNg= -github.com/88250/lute v1.7.6-0.20230608014232-e10343eb445d/go.mod h1:+wUqx/1kdFDbWtxn9LYJlaCOAeol2pjSO6w+WJTVQsg= +github.com/88250/lute v1.7.6-0.20230617074457-ffab89d50887 h1:jIOA18lMo4DOdF3K66lu4DCOhgrfeHTNY710cXuPkL0= +github.com/88250/lute v1.7.6-0.20230617074457-ffab89d50887/go.mod h1:+wUqx/1kdFDbWtxn9LYJlaCOAeol2pjSO6w+WJTVQsg= github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c h1:Dl/8S9iLyPMTElnWIBxmjaLiWrkI5P4a21ivwAn5pU0= github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4= github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 h1:48T899JQDwyyRu9yXHePYlPdHtpJfrJEUGBMH3SMBWY= diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index b61a099a3..a16ea0603 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -252,7 +252,7 @@ func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode refs = removeDuplicatedRefs(refs) // 同一个块中引用多个相同块时反链去重 https://github.com/siyuan-note/siyuan/issues/3317 linkRefs, linkRefsCount, excludeBacklinkIDs := buildLinkRefs(rootID, refs, keyword) - tmpBacklinks := toFlatTree(linkRefs, 0, "backlink") + tmpBacklinks := toFlatTree(linkRefs, 0, "backlink", nil) for _, l := range tmpBacklinks { l.Blocks = nil @@ -282,7 +282,7 @@ func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode }) mentionRefs, _ := buildTreeBackmention(sqlBlock, linkRefs, mentionKeyword, excludeBacklinkIDs, 12) - tmpBackmentions := toFlatTree(mentionRefs, 0, "backlink") + tmpBackmentions := toFlatTree(mentionRefs, 0, "backlink", nil) for _, l := range tmpBackmentions { l.Blocks = nil backmentions = append(backmentions, l) @@ -442,7 +442,7 @@ func GetBacklink(id, keyword, mentionKeyword string, beforeLen int) (boxID strin mentions, _ := buildTreeBackmention(sqlBlock, linkRefs, mentionKeyword, excludeBacklinkIDs, beforeLen) mentionsCount = len(mentions) - mentionPaths = toFlatTree(mentions, 0, "backlink") + mentionPaths = toFlatTree(mentions, 0, "backlink", nil) return } diff --git a/kernel/model/export.go b/kernel/model/export.go index b3952a4c5..5996c64f6 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -318,7 +318,7 @@ func exportData(exportFolder string) (zipPath string, err error) { return } -func Preview(id string) string { +func Preview(id string) (retStdHTML string, retOutline []*Path) { tree, _ := loadTreeByBlockID(id) tree = exportTree(tree, false, false, false, Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode, @@ -329,7 +329,13 @@ func Preview(id string) string { luteEngine.SetFootnotes(true) md := treenode.FormatNode(tree.Root, luteEngine) tree = parse.Parse("", []byte(md), luteEngine.ParseOptions) - return luteEngine.ProtylePreview(tree, luteEngine.RenderOptions) + retStdHTML = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions) + + if footnotesDefBlock := tree.Root.ChildByType(ast.NodeFootnotesDefBlock); nil != footnotesDefBlock { + footnotesDefBlock.Unlink() + } + retOutline = outline(tree) + return } func ExportDocx(id, savePath string, removeAssets, merge bool) (err error) { diff --git a/kernel/model/outline.go b/kernel/model/outline.go index f2aa341d8..a5f292e49 100644 --- a/kernel/model/outline.go +++ b/kernel/model/outline.go @@ -20,6 +20,7 @@ import ( "time" "github.com/88250/lute/ast" + "github.com/88250/lute/parse" "github.com/emirpasic/gods/stacks/linkedliststack" "github.com/siyuan-note/siyuan/kernel/treenode" "github.com/siyuan-note/siyuan/kernel/util" @@ -35,13 +36,18 @@ func Outline(rootID string) (ret []*Path, err error) { return } + ret = outline(tree) + return +} + +func outline(tree *parse.Tree) (ret []*Path) { luteEngine := NewLute() var headings []*Block ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) { n.Box, n.Path = tree.Box, tree.Path block := &Block{ - RootID: rootID, + RootID: tree.Root.ID, Depth: n.HeadingLevel, Box: n.Box, Path: n.Path, @@ -82,7 +88,7 @@ func Outline(rootID string) (ret []*Path, err error) { } } - ret = toFlatTree(blocks, 0, "outline") + ret = toFlatTree(blocks, 0, "outline", tree) if 0 < len(ret) { children := ret[0].Blocks ret = nil diff --git a/kernel/model/path.go b/kernel/model/path.go index 91731ff3b..72f3d37f8 100644 --- a/kernel/model/path.go +++ b/kernel/model/path.go @@ -25,6 +25,7 @@ import ( "strings" "github.com/88250/lute/ast" + "github.com/88250/lute/parse" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/search" "github.com/siyuan-note/siyuan/kernel/sql" @@ -99,12 +100,12 @@ func createDocsByHPath(boxID, hPath, content, parentID string) (id string, exist return } -func toFlatTree(blocks []*Block, baseDepth int, typ string) (ret []*Path) { +func toFlatTree(blocks []*Block, baseDepth int, typ string, tree *parse.Tree) (ret []*Path) { var blockRoots []*Block for _, block := range blocks { root := getBlockIn(blockRoots, block.RootID) if nil == root { - root, _ = getBlock(block.RootID, nil) + root, _ = getBlock(block.RootID, tree) blockRoots = append(blockRoots, root) } if nil == root {