Skip to content
This repository was archived by the owner on Sep 17, 2023. It is now read-only.

Commit 004136e

Browse files
committed
✨ feat(docRender): 添加 main 标签作为 doc 的最外层元素,添加 updated 实用属性
1 parent 260ca8d commit 004136e

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

src/store/store.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"strconv"
89
"strings"
910

1011
sqlite "github.com/2234839/md2website/src/sqlite"
@@ -61,16 +62,44 @@ type DirToStructRes struct {
6162
FindFileEntityFromID FindFileEntityFromID
6263
}
6364

64-
func addAll(node *ast.Node) {
65+
func addKramdownIAL(node *ast.Node) {
66+
ctx := addKramdownIALContext{}
67+
addAll(node, &ctx)
68+
// 文档最后更新时间
69+
node.KramdownIAL = append(node.KramdownIAL, []string{"updated", strconv.Itoa(ctx.docUpdated)})
70+
//TODO: 所有块 id 都应该改成 data-block-id 这里应该要看下 lute 内是如何实现的,不应该这里还要写死
71+
for _, v := range node.KramdownIAL {
72+
if v[0] == "id" {
73+
node.KramdownIAL = append(node.KramdownIAL, []string{"data-block-id", v[1]})
74+
}
75+
}
76+
}
77+
78+
type addKramdownIALContext struct {
79+
docUpdated int
80+
}
81+
82+
// addAll 遍历整颗树,附加一些数据到 KramdownIAL
83+
func addAll(node *ast.Node, ctx *addKramdownIALContext) {
84+
for _, v := range node.KramdownIAL {
85+
// 获取文档最后更新时间
86+
if v[0] == "updated" {
87+
updated, _ := strconv.Atoi(v[1])
88+
if updated > ctx.docUpdated {
89+
ctx.docUpdated = updated
90+
}
91+
}
92+
}
6593
node.KramdownIAL = append(node.KramdownIAL, []string{"data-type", node.Type.String()})
94+
6695
if node.Next != nil {
67-
addAll(node.Next)
96+
addAll(node.Next, ctx)
6897
}
6998
if node.FirstChild != nil {
70-
addAll(node.FirstChild)
99+
addAll(node.FirstChild, ctx)
71100
}
72101
for _, n := range node.Children {
73-
addAll(n)
102+
addAll(n, ctx)
74103
}
75104
}
76105

@@ -91,7 +120,7 @@ func DirToStruct(dir string, dbPath string, structToHTML func(interface{}) strin
91120
} else if suffix == ".md" {
92121
tree = parse.Parse("", []byte(notesCode), mdStructuredLuteEngine.ParseOptions)
93122
}
94-
addAll(tree.Root)
123+
addKramdownIAL(tree.Root)
95124
if err != nil {
96125
panic(err)
97126
}

src/store/toHTML.go

+8-20
Original file line numberDiff line numberDiff line change
@@ -305,27 +305,13 @@ func (r *OceanpressRenderer) 模板复制粘贴用(node *ast.Node, entering bool
305305
})(node, entering)
306306
}
307307

308-
func (r *OceanpressRenderer) titleRenderer(n *ast.Node, entering bool, src string, fileEntity FileEntity, mdInfo StructInfo, html string) template.HTML {
309-
var title = template.HTML(n.Text())
310-
t := string(title)
311-
312-
// 锚文本模板变量处理 使用定义块内容文本填充。
313-
if strings.Contains(t, "{{.text}}") {
314-
var title2 template.HTML
315-
// 如定义块是文档块,则使用文档名填充。
316-
if mdInfo.blockType == "NodeDocument" {
317-
title2 = template.HTML(fileEntity.Name)
318-
} else {
319-
title2 = template.HTML(
320-
r.context.luteEngine.HTML2Text(
321-
r.context.luteEngine.MarkdownStr("", renderNodeMarkdown(mdInfo.node, false)),
322-
),
323-
)
324-
}
325-
title = template.HTML(strings.ReplaceAll(t, "{{.text}}", string(title2)))
308+
func (r *OceanpressRenderer) NodeDocumentRender(node *ast.Node, entering bool) ast.WalkStatus {
309+
if entering {
310+
r.context.rawRenderer.Tag("main", node.KramdownIAL, false)
311+
} else {
312+
r.context.rawRenderer.Tag("/main", nil, false)
326313
}
327-
title = template.HTML(strings.ReplaceAll(string(title), "\n", ""))
328-
return title
314+
return ast.WalkContinue
329315
}
330316

331317
// HOC, 内部处理了循环引用的问题, 生成一个渲染函数,
@@ -554,5 +540,7 @@ func NewOceanpressRenderer(tree *parse.Tree, options *render.Options,
554540
rawRenderer.RendererFuncs[ast.NodeBlockQueryEmbed] = ret2.NodeBlockQueryEmbed
555541
rawRenderer.RendererFuncs[ast.NodeSuperBlock] = ret2.NodeSuperBlock
556542
rawRenderer.RendererFuncs[ast.NodeCodeBlock] = ret2.NodeCodeBlock
543+
rawRenderer.RendererFuncs[ast.NodeDocument] = ret2.NodeDocumentRender
544+
557545
return rawRenderer, ret2
558546
}

0 commit comments

Comments
 (0)