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

Commit

Permalink
✨ feat(docRender): 添加 main 标签作为 doc 的最外层元素,添加 updated 实用属性
Browse files Browse the repository at this point in the history
  • Loading branch information
2234839 committed May 21, 2021
1 parent 260ca8d commit 004136e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
39 changes: 34 additions & 5 deletions src/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

sqlite "github.com/2234839/md2website/src/sqlite"
Expand Down Expand Up @@ -61,16 +62,44 @@ type DirToStructRes struct {
FindFileEntityFromID FindFileEntityFromID
}

func addAll(node *ast.Node) {
func addKramdownIAL(node *ast.Node) {
ctx := addKramdownIALContext{}
addAll(node, &ctx)
// 文档最后更新时间
node.KramdownIAL = append(node.KramdownIAL, []string{"updated", strconv.Itoa(ctx.docUpdated)})
//TODO: 所有块 id 都应该改成 data-block-id 这里应该要看下 lute 内是如何实现的,不应该这里还要写死
for _, v := range node.KramdownIAL {
if v[0] == "id" {
node.KramdownIAL = append(node.KramdownIAL, []string{"data-block-id", v[1]})
}
}
}

type addKramdownIALContext struct {
docUpdated int
}

// addAll 遍历整颗树,附加一些数据到 KramdownIAL
func addAll(node *ast.Node, ctx *addKramdownIALContext) {
for _, v := range node.KramdownIAL {
// 获取文档最后更新时间
if v[0] == "updated" {
updated, _ := strconv.Atoi(v[1])
if updated > ctx.docUpdated {
ctx.docUpdated = updated
}
}
}
node.KramdownIAL = append(node.KramdownIAL, []string{"data-type", node.Type.String()})

if node.Next != nil {
addAll(node.Next)
addAll(node.Next, ctx)
}
if node.FirstChild != nil {
addAll(node.FirstChild)
addAll(node.FirstChild, ctx)
}
for _, n := range node.Children {
addAll(n)
addAll(n, ctx)
}
}

Expand All @@ -91,7 +120,7 @@ func DirToStruct(dir string, dbPath string, structToHTML func(interface{}) strin
} else if suffix == ".md" {
tree = parse.Parse("", []byte(notesCode), mdStructuredLuteEngine.ParseOptions)
}
addAll(tree.Root)
addKramdownIAL(tree.Root)
if err != nil {
panic(err)
}
Expand Down
28 changes: 8 additions & 20 deletions src/store/toHTML.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,27 +305,13 @@ func (r *OceanpressRenderer) 模板复制粘贴用(node *ast.Node, entering bool
})(node, entering)
}

func (r *OceanpressRenderer) titleRenderer(n *ast.Node, entering bool, src string, fileEntity FileEntity, mdInfo StructInfo, html string) template.HTML {
var title = template.HTML(n.Text())
t := string(title)

// 锚文本模板变量处理 使用定义块内容文本填充。
if strings.Contains(t, "{{.text}}") {
var title2 template.HTML
// 如定义块是文档块,则使用文档名填充。
if mdInfo.blockType == "NodeDocument" {
title2 = template.HTML(fileEntity.Name)
} else {
title2 = template.HTML(
r.context.luteEngine.HTML2Text(
r.context.luteEngine.MarkdownStr("", renderNodeMarkdown(mdInfo.node, false)),
),
)
}
title = template.HTML(strings.ReplaceAll(t, "{{.text}}", string(title2)))
func (r *OceanpressRenderer) NodeDocumentRender(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.context.rawRenderer.Tag("main", node.KramdownIAL, false)
} else {
r.context.rawRenderer.Tag("/main", nil, false)
}
title = template.HTML(strings.ReplaceAll(string(title), "\n", ""))
return title
return ast.WalkContinue
}

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

return rawRenderer, ret2
}

0 comments on commit 004136e

Please sign in to comment.