Skip to content

Commit

Permalink
🎨 更新脚注定义块节点结构 #104
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 7, 2020
1 parent 1634e83 commit 2f0705e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
47 changes: 23 additions & 24 deletions render/vditor_wysiwyg_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
// VditorRenderer 描述了 Vditor WYSIWYG DOM 渲染器。
type VditorRenderer struct {
*BaseRenderer
needRenderFootnotesDef bool
}

// NewVditorRenderer 创建一个 Vditor WYSIWYG DOM 渲染器。
Expand Down Expand Up @@ -101,6 +100,7 @@ func NewVditorRenderer(tree *parse.Tree) *VditorRenderer {
ret.RendererFuncs[ast.NodeEmojiUnicode] = ret.renderEmojiUnicode
ret.RendererFuncs[ast.NodeEmojiImg] = ret.renderEmojiImg
ret.RendererFuncs[ast.NodeEmojiAlias] = ret.renderEmojiAlias
ret.RendererFuncs[ast.NodeFootnotesDefBlock] = ret.renderFootnotesDefBlock
ret.RendererFuncs[ast.NodeFootnotesDef] = ret.renderFootnotesDef
ret.RendererFuncs[ast.NodeFootnotesRef] = ret.renderFootnotesRef
ret.RendererFuncs[ast.NodeToC] = ret.renderToC
Expand All @@ -122,7 +122,7 @@ func NewVditorRenderer(tree *parse.Tree) *VditorRenderer {

func (r *VditorRenderer) Render() (output []byte) {
output = r.BaseRenderer.Render()
if 1 > len(r.Tree.Context.LinkRefDefs) || r.needRenderFootnotesDef {
if 1 > len(r.Tree.Context.LinkRefDefs) || r.RenderingFootnotes {
return
}

Expand Down Expand Up @@ -212,25 +212,6 @@ func (r *VditorRenderer) renderYamlFrontMatter(node *ast.Node, entering bool) as
return ast.WalkContinue
}

func (r *VditorRenderer) RenderFootnotesDefs(context *parse.Context) []byte {
r.WriteString("<div data-block=\"0\" data-type=\"footnotes-block\">")
r.WriteString("<ol data-type=\"footnotes-defs-ol\">")
for _, def := range r.FootnotesDefs {
r.WriteString("<li data-type=\"footnotes-li\" data-marker=\"" + string(def.Tokens) + "\">")
tree := &parse.Tree{Name: "", Context: context}
tree.Context.Tree = tree
tree.Root = &ast.Node{Type: ast.NodeDocument}
tree.Root.AppendChild(def)
defRenderer := NewVditorRenderer(tree)
defRenderer.needRenderFootnotesDef = true
defContent := defRenderer.Render()
r.Write(defContent)
r.WriteString("</li>")
}
r.WriteString("</ol></div>")
return r.Writer.Bytes()
}

func (r *VditorRenderer) renderHtmlEntity(node *ast.Node, entering bool) ast.WalkStatus {
previousNodeText := node.PreviousNodeText()
previousNodeText = strings.ReplaceAll(previousNodeText, util.Caret, "")
Expand Down Expand Up @@ -295,13 +276,31 @@ func (r *VditorRenderer) renderToC(node *ast.Node, entering bool) ast.WalkStatus
return ast.WalkStop
}

func (r *VditorRenderer) renderFootnotesDef(node *ast.Node, entering bool) ast.WalkStatus {
if !r.needRenderFootnotesDef {
return ast.WalkStop
func (r *VditorRenderer) renderFootnotesDefBlock(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.WriteString("<div data-block=\"0\" data-type=\"footnotes-block\">")
r.WriteString("<ol data-type=\"footnotes-defs-ol\">")
} else {
r.WriteString("</ol></div>")
}
return ast.WalkContinue
}

func (r *VditorRenderer) renderFootnotesDef(node *ast.Node, entering bool) ast.WalkStatus {
if r.RenderingFootnotes {
return ast.WalkContinue
}

r.WriteString("<li data-type=\"footnotes-li\" data-marker=\"" + string(node.Tokens) + "\">")
for c := node.FirstChild; nil != c; c = c.Next {
ast.Walk(c, func(n *ast.Node, entering bool) ast.WalkStatus {
return r.RendererFuncs[n.Type](n, entering)
})
}
r.WriteString("</li>")
return ast.WalkStop
}

func (r *VditorRenderer) renderFootnotesRef(node *ast.Node, entering bool) ast.WalkStatus {
previousNodeText := node.PreviousNodeText()
previousNodeText = strings.ReplaceAll(previousNodeText, util.Caret, "")
Expand Down
2 changes: 1 addition & 1 deletion test/m2v_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var md2VditorDOMTests = []parseTest{
{"18", "![][foo]\n\n[foo]: bar", "<p data-block=\"0\">\u200b<img src=\"bar\" alt=\"\" data-type=\"link-ref\" data-link-label=\"foo\" /></p><div data-block=\"0\" data-type=\"link-ref-defs-block\">[foo]: bar\n</div>"},
{"17", "![text][foo]\n\n[foo]: bar", "<p data-block=\"0\">\u200b<img src=\"bar\" alt=\"text\" data-type=\"link-ref\" data-link-label=\"foo\" /></p><div data-block=\"0\" data-type=\"link-ref-defs-block\">[foo]: bar\n</div>"},
{"16", "# heading {#custom-id}\n", "<h1 data-block=\"0\" data-id=\"#custom-id\" id=\"wysiwyg-#custom-id\" data-marker=\"#\">heading</h1>"},
{"15", "foo\n\n[^1]: 111\n\n[2]: 222\n", "<p data-block=\"0\">foo</p><div data-block=\"0\" data-type=\"link-ref-defs-block\">[2]: 222\n</div><div data-block=\"0\" data-type=\"footnotes-block\"><ol data-type=\"footnotes-defs-ol\"><li data-type=\"footnotes-li\" data-marker=\"^1\"><p data-block=\"0\">111</p></li></ol></div>"},
{"15", "foo\n\n[^1]: 111\n\n[2]: 222\n", "<p data-block=\"0\">foo</p><div data-block=\"0\" data-type=\"footnotes-block\"><ol data-type=\"footnotes-defs-ol\"><li data-type=\"footnotes-li\" data-marker=\"^1\"><p data-block=\"0\">111</p></li></ol></div><div data-block=\"0\" data-type=\"link-ref-defs-block\">[2]: 222\n</div>"},
{"14", "[^1]\n\n[^1]:\n", "<p data-block=\"0\">\u200b<sup data-type=\"footnotes-ref\" data-footnotes-label=\"^1\" class=\"b3-tooltips b3-tooltips__s\" aria-label=\"\">1</sup>\u200b</p><div data-block=\"0\" data-type=\"footnotes-block\"><ol data-type=\"footnotes-defs-ol\"><li data-type=\"footnotes-li\" data-marker=\"^1\"></li></ol></div>"},
{"13", "[toc]\n\n# foo", "<div class=\"vditor-toc\" data-block=\"0\" data-type=\"toc-block\" contenteditable=\"false\"><span data-type=\"toc-h\">foo</span><br></div><p data-block=\"0\"></p><h1 data-block=\"0\" id=\"wysiwyg-foo\" data-marker=\"#\">foo</h1>"},
{"12", "foo[^1]\n[^1]:bar\n * baz", "<p data-block=\"0\">foo<sup data-type=\"footnotes-ref\" data-footnotes-label=\"^1\" class=\"b3-tooltips b3-tooltips__s\" aria-label=\"barbaz\">1</sup>\u200b</p><div data-block=\"0\" data-type=\"footnotes-block\"><ol data-type=\"footnotes-defs-ol\"><li data-type=\"footnotes-li\" data-marker=\"^1\"><p data-block=\"0\">bar</p><ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\">baz</li></ul></li></ol></div>"},
Expand Down

0 comments on commit 2f0705e

Please sign in to comment.