Skip to content

Commit

Permalink
✨ KityMinder JSON 渲染器 #127
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jan 9, 2021
1 parent a9ada4a commit ef6aee3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
77 changes: 37 additions & 40 deletions render/kityminder_json_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,18 @@ func (r *KityMinderJSONRenderer) renderHTML(node *ast.Node, entering bool) ast.W
return ast.WalkSkipChildren
}

func (r *KityMinderJSONRenderer) renderDocument(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.WriteByte(lex.ItemOpenBrace)
r.WriteString("\"root\":")
r.openObj()
r.dataText("文档名")
r.openChildren(node)
} else {
r.closeChildren(node)
r.closeObj()
r.WriteByte(lex.ItemCloseBrace)
}
return ast.WalkContinue
}

func (r *KityMinderJSONRenderer) renderParagraph(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.openObj()
md := r.renderBlockMarkdown(node)
md := r.formatNode(node)
r.dataText(md)
r.openChildren(node)
} else {
r.closeChildren(node)
r.closeObj()
if nil != node.Next {
r.comma()
}
}
return ast.WalkSkipChildren
}
Expand All @@ -149,9 +137,18 @@ func (r *KityMinderJSONRenderer) renderBlockquote(node *ast.Node, entering bool)
func (r *KityMinderJSONRenderer) renderHeading(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.openObj()
md := r.renderBlockMarkdown(node)
md := r.formatNode(node)
r.dataText(md)
r.openChildren(node)

for c := node.FirstChild; nil != c; c = c.Next {
c.Unlink()
}

children := headingChildren(node)
for _, c := range children {
node.AppendChild(c)
}
} else {
r.closeChildren(node)
r.closeObj()
Expand All @@ -162,7 +159,7 @@ func (r *KityMinderJSONRenderer) renderHeading(node *ast.Node, entering bool) as
func (r *KityMinderJSONRenderer) renderList(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.openObj()
md := r.renderBlockMarkdown(node)
md := r.formatNode(node)
r.dataText(md)
r.openChildren(node)
} else {
Expand All @@ -175,7 +172,7 @@ func (r *KityMinderJSONRenderer) renderList(node *ast.Node, entering bool) ast.W
func (r *KityMinderJSONRenderer) renderListItem(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.openObj()
md := r.renderBlockMarkdown(node)
md := r.formatNode(node)
r.dataText(md)
r.openChildren(node)
} else {
Expand Down Expand Up @@ -213,6 +210,21 @@ func (r *KityMinderJSONRenderer) renderCodeBlock(node *ast.Node, entering bool)
return ast.WalkSkipChildren
}

func (r *KityMinderJSONRenderer) renderDocument(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.WriteByte(lex.ItemOpenBrace)
r.WriteString("\"root\":")
r.openObj()
r.dataText("文档名 TODO")
r.openChildren(node)
} else {
r.closeChildren(node)
r.closeObj()
r.WriteByte(lex.ItemCloseBrace)
}
return ast.WalkContinue
}

func (r *KityMinderJSONRenderer) leaf(val string, node *ast.Node) {
r.openObj()
r.val(val, node)
Expand Down Expand Up @@ -258,29 +270,14 @@ func (r *KityMinderJSONRenderer) comma() {
r.WriteString(",")
}

func (r *KityMinderJSONRenderer) renderBlockMarkdown(node *ast.Node) string {
var nodes []*ast.Node
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering {
nodes = append(nodes, n)
if ast.NodeHeading == node.Type {
// 支持“标题块”引用
children := headingChildren(n)
nodes = append(nodes, children...)
}
}
return ast.WalkSkipChildren
})

func (r *KityMinderJSONRenderer) formatNode(node *ast.Node) string {
renderer := NewFormatRenderer(r.Tree, r.Options)
renderer.Writer = &bytes.Buffer{}
renderer.NodeWriterStack = append(renderer.NodeWriterStack, renderer.Writer)
for _, node := range nodes {
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
rendererFunc := renderer.RendererFuncs[n.Type]
return rendererFunc(n, entering)
})
}
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
rendererFunc := renderer.RendererFuncs[n.Type]
return rendererFunc(n, entering)
})
return strings.TrimSpace(renderer.Writer.String())
}

Expand All @@ -290,7 +287,7 @@ func headingChildren(heading *ast.Node) (ret []*ast.Node) {
start = heading.Next.Next
}
currentLevel := heading.HeadingLevel
for n := start; nil != n; n = n.Next {
for n := start.Next; nil != n; n = n.Next {
if ast.NodeHeading == n.Type {
if currentLevel >= n.HeadingLevel {
break
Expand Down
1 change: 1 addition & 0 deletions test/kityminder_json_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var kitymindJSONRendererTests = []parseTest{

{"1", "# foo\n\n para1\n\npara2", ""},
{"0", "foo **bar**\n", ""},
}

Expand Down

0 comments on commit ef6aee3

Please sign in to comment.