Skip to content

Commit

Permalink
✨ 引入链接引用定义节点 Fix #107
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 9, 2020
1 parent f56f8fd commit 3aa3f21
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 35 deletions.
6 changes: 3 additions & 3 deletions javascript/lute.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion javascript/lute.min.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions render/vditor_ir_block_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ func (r *VditorIRBlockRenderer) renderLinkRefDef(node *ast.Node, entering bool)
if entering {
dest := node.FirstChild.ChildByType(ast.NodeLinkDest).Tokens
destStr := util.BytesToStr(dest)
r.WriteString("[" + util.BytesToStr(node.Tokens) + "]:")
r.WriteString("<span><span>[" + util.BytesToStr(node.Tokens) + "]:")
if util.Caret != destStr {
r.WriteString(" ")
r.WriteString(" </span>")
}
r.WriteString(destStr + "\n")
r.WriteString(destStr + "\n</span>")
}
return ast.WalkSkipChildren
}
Expand Down
1 change: 1 addition & 0 deletions test/spinv_ir_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

var spinVditorIRBlockDOMTests = []*parseTest{

{"60", "<p data-block=\"0\" data-node-id=\"20201109231947-ql351ot\" data-type=\"p\"><span data-type=\"link-ref\" class=\"vditor-ir__node\"><span class=\"vditor-ir__marker vditor-ir__marker--bracket\">[</span><span>text</span><span class=\"vditor-ir__marker vditor-ir__marker--bracket\">]</span><span class=\"vditor-ir__marker vditor-ir__marker--link\">[foo]</span></span></p><div data-block=\"0\" data-type=\"link-ref-defs-block\"><span><span>[foo]: </span>bar\n</span></div><div data-block=\"0\" data-type=\"link-ref-defs-block\"><span><wbr><br></span></div>", "<p data-block=\"0\" data-node-id=\"20201109231947-ql351ot\" data-type=\"p\"><span data-type=\"link-ref\" class=\"vditor-ir__node\"><span class=\"vditor-ir__marker vditor-ir__marker--bracket\">[</span><span>text</span><span class=\"vditor-ir__marker vditor-ir__marker--bracket\">]</span><span class=\"vditor-ir__marker vditor-ir__marker--link\">[foo]</span></span></p><div data-block=\"0\" data-type=\"link-ref-defs-block\"><span><span>[foo]: </span>bar\n</span></div><p data-block=\"0\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"p\"><wbr></p>"},
{"59", "<p data-block=\"0\" data-node-id=\"20201108202934-60lz8qy\" data-type=\"p\">[^foo]<wbr></p><div data-block=\"0\" data-type=\"footnotes-block\"><div data-type=\"footnotes-def\"><span>[^foo]: </span><p data-block=\"0\" data-node-id=\"20201108213050-f6lbjws\" data-type=\"p\">foo</p></div><div data-type=\"footnotes-def\"><span>[^bar]: </span><p data-block=\"0\" data-node-id=\"20201108204215-k4ua23v\" data-type=\"p\">bar</p></div></div>", "<p data-block=\"0\" data-node-id=\"20201108202934-60lz8qy\" data-type=\"p\">\u200b<sup data-type=\"footnotes-ref\" class=\"vditor-ir__node b3-tooltips b3-tooltips__s\" aria-label=\"foo\" data-footnotes-label=\"^foo\"><span class=\"vditor-ir__marker vditor-ir__marker--bracket\">[</span><span class=\"vditor-ir__marker vditor-ir__marker--link\">^foo</span><span class=\"vditor-ir__marker--hide\" data-render=\"1\">1</span><span class=\"vditor-ir__marker vditor-ir__marker--bracket\">]</span></sup>\u200b<wbr></p><div data-block=\"0\" data-type=\"footnotes-block\"><div data-type=\"footnotes-def\"><span>[^foo]: </span><p data-block=\"0\" data-node-id=\"20201108213050-f6lbjws\" data-type=\"p\">foo</p></div><div data-type=\"footnotes-def\"><span>[^bar]: </span><p data-block=\"0\" data-node-id=\"20201108204215-k4ua23v\" data-type=\"p\">bar</p></div></div>"},
{"58", "<div data-block=\"0\" data-type=\"footnotes-block\"><div data-type=\"footnotes-def\"><span>[^fn]: </span><p data-block=\"0\" data-node-id=\"20201107230538-uemlzcf\" data-type=\"p\">​f<wbr></p></div></div>", "<div data-block=\"0\" data-type=\"footnotes-block\"><div data-type=\"footnotes-def\"><span>[^fn]: </span><p data-block=\"0\" data-node-id=\"20201107230538-uemlzcf\" data-type=\"p\">f<wbr></p></div></div>"},
{"57", "<ul data-marker=\"*\" data-block=\"0\" data-node-id=\"20201106094752-tw5u1zz\" data-type=\"ul\"><li data-marker=\"*\" class=\"vditor-task\" data-node-id=\"20201106094752-n0uvzjy\"><p data-block=\"0\" data-node-id=\"20201106094826-al3hi61\" data-type=\"p\"><input type=\"checkbox\"> foo</p></li><li data-marker=\"*\" class=\"vditor-task\" data-node-id=\"20201106094756-g8cgvth\"><p data-block=\"0\" data-node-id=\"20201106094826-5ztcccc\" data-type=\"p\"><input type=\"checkbox\"> bar</p><p data-block=\"0\" data-node-id=\"20201106094826-x1uij4r\" data-type=\"p\">ba<wbr></p></li></ul>", "<ul data-marker=\"*\" data-block=\"0\" data-node-id=\"20201106094752-tw5u1zz\" data-type=\"ul\"><li data-marker=\"*\" class=\"vditor-task\" data-node-id=\"20201106094752-n0uvzjy\"><p data-block=\"0\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"p\"><input type=\"checkbox\" /> foo</p></li><li data-marker=\"*\" class=\"vditor-task\" data-node-id=\"20201106094756-g8cgvth\"><p data-block=\"0\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"p\"><input type=\"checkbox\" /> bar</p><p data-block=\"0\" data-node-id=\"20201106094826-x1uij4r\" data-type=\"p\">ba<wbr></p></li></ul>"},
Expand Down
2 changes: 1 addition & 1 deletion test/v2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestVditorIRDOM2Md(t *testing.T) {

var vditorIRBlockDOM2MdTests = []parseTest{

{"6", "<div data-block=\"0\" data-type=\"footnotes-block\"><div data-type=\"footnotes-def\"><p data-block=\"0\" data-node-id=\"20201106195335-zufg3eh\" data-type=\"p\">[^1]: foo</p><p data-block=\"0\" data-node-id=\"20201106195338-pxxn5j3\" data-type=\"p\"></p></div></div>", "[^1]: foo\n {: id=\"20201106195335-zufg3eh\"}\n"},
{"6", "<div data-block=\"0\" data-type=\"footnotes-block\"><div data-type=\"footnotes-def\"><p data-block=\"0\" data-node-id=\"20201106195335-zufg3eh\" data-type=\"p\">[^1]: foo</p><p data-block=\"0\" data-node-id=\"20201106195338-pxxn5j3\" data-type=\"p\"></p></div></div>", "[^1]: foo\n {: id=\"20201106195335-zufg3eh\"}\n\n\n{: id=\"20060102150405-1a2b3c4\"}\n"},
{"4", "<p data-block=\"0\" data-node-id=\"20201024165528-n88b4e2\" data-type=\"p\"><span data-type=\"strong\" class=\"vditor-ir__node\"><span class=\"vditor-ir__marker vditor-ir__marker--strong\">**</span><strong data-newline=\"1\">foo</strong><span class=\"vditor-ir__marker vditor-ir__marker--strong\">**</span></span>\nba<wbr></p>", "**foo**\nba\n{: id=\"20201024165528-n88b4e2\"}\n"},
{"3", "<ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\" data-node-id=\"20201024153454-bqqcbc1\" data-type=\"ul\"><li data-marker=\"*\" data-node-id=\"20201024153456-ktna8mm\">foo<span data-type=\"strong\" class=\"vditor-ir__node\"><span class=\"vditor-ir__marker vditor-ir__marker--strong\">**</span><strong data-newline=\"1\">bar</strong><span class=\"vditor-ir__marker vditor-ir__marker--strong\">**</span></span></li><li data-marker=\"*\" data-node-id=\"20201024153456-ktna8mm\"><span data-type=\"strong\" class=\"vditor-ir__node\"><span class=\"vditor-ir__marker vditor-ir__marker--strong\"><wbr><br></span></span></li></ul>", "* {: id=\"20201024153456-ktna8mm\"}foo**bar**\n* {: id=\"20060102150405-1a2b3c4\"}\n{: id=\"20201024153454-bqqcbc1\"}\n"},
{"2", "<ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\" data-node-id=\"ul1\" data-type=\"ul\"><li data-marker=\"*\" data-node-id=\"fooid\">foo<ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\" data-node-id=\"ul2\" data-type=\"ul\"><li data-marker=\"*\" data-node-id=\"barid\">bar</li></ul></li></ul>", "* {: id=\"fooid\"}foo\n * {: id=\"barid\"}bar\n {: id=\"ul2\"}\n{: id=\"ul1\"}\n"},
Expand Down
65 changes: 38 additions & 27 deletions vditor_ir_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,48 @@ func (lute *Lute) genASTByVditorIRBlockDOM(n *html.Node, tree *parse.Tree) {

dataType := lute.domAttrValue(n, "data-type")

class := lute.domAttrValue(n, "class")
content := strings.ReplaceAll(n.Data, parse.Zwsp, "")
nodeID := lute.domAttrValue(n, "data-node-id")
node := &ast.Node{ID: nodeID, Type: ast.NodeText, Tokens: []byte(content)}
if "" == nodeID {
if "p" == dataType || "ul" == dataType || "ol" == dataType || "blockquote" == dataType ||
"math-block" == dataType || "code-block" == dataType || "table" == dataType || "h" == dataType ||
"link-ref-defs-block" == dataType || "footnotes-block" == dataType {
nodeID = ast.NewNodeID()
}
}
if "" != nodeID {
node.KramdownIAL = [][]string{{"id", nodeID}}
ialTokens := []byte("{: id=\"" + nodeID + "\"")
bookmark := lute.domAttrValue(n, "bookmark")
if "" != bookmark {
node.SetIALAttr("bookmark", bookmark)
ialTokens = append(ialTokens, []byte(" bookmark=\""+bookmark+"\"")...)
}
ialTokens = append(ialTokens, '}')
ial := &ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: ialTokens}
defer tree.Context.TipAppendChild(ial)
}

if atom.Div == n.DataAtom {
if "link-ref-defs-block" == dataType {
linkRefDef := &ast.Node{Type: ast.NodeLinkRefDef, }
tree.Context.Tip.AppendChild(linkRefDef)
text := lute.domText(n)
if !strings.HasPrefix(text, "[") {
subTree := parse.Parse("", []byte(text), lute.Options)
tree.Context.Tip.AppendChild(subTree.Root.FirstChild)
return
}

defBlock := &ast.Node{Type: ast.NodeLinkRefDefBlock}
tree.Context.Tip.AppendChild(defBlock)
for def := n.FirstChild; nil != def; def = def.NextSibling {
text := lute.domText(def)
text = lute.domText(def)
subTree := parse.Parse("", []byte(text), lute.Options)
linkRefDef.AppendChild(subTree.Root.FirstChild.FirstChild)
child := subTree.Root.FirstChild.FirstChild
if ast.NodeLinkRefDef == child.Type {
defBlock.AppendChild(subTree.Root.FirstChild.FirstChild)
}
}
return
} else if "footnotes-def" == dataType {
Expand Down Expand Up @@ -296,29 +330,6 @@ func (lute *Lute) genASTByVditorIRBlockDOM(n *html.Node, tree *parse.Tree) {
}
}

class := lute.domAttrValue(n, "class")
content := strings.ReplaceAll(n.Data, parse.Zwsp, "")
nodeID := lute.domAttrValue(n, "data-node-id")
node := &ast.Node{ID: nodeID, Type: ast.NodeText, Tokens: []byte(content)}
if "" == nodeID {
if "p" == dataType || "ul" == dataType || "ol" == dataType || "blockquote" == dataType ||
"math-block" == dataType || "code-block" == dataType || "table" == dataType || "h" == dataType {
nodeID = ast.NewNodeID()
}
}
if "" != nodeID {
node.KramdownIAL = [][]string{{"id", nodeID}}
ialTokens := []byte("{: id=\"" + nodeID + "\"")
bookmark := lute.domAttrValue(n, "bookmark")
if "" != bookmark {
node.SetIALAttr("bookmark", bookmark)
ialTokens = append(ialTokens, []byte(" bookmark=\""+bookmark+"\"")...)
}
ialTokens = append(ialTokens, '}')
ial := &ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: ialTokens}
defer tree.Context.TipAppendChild(ial)
}

switch n.DataAtom {
case 0:
if "" == content {
Expand Down

0 comments on commit 3aa3f21

Please sign in to comment.