Skip to content

Commit

Permalink
🎨 列表项合并 Fix Vanessa219/vditor#796
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 24, 2020
1 parent fad14ac commit 4d01c46
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion 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.

3 changes: 3 additions & 0 deletions test/v2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package test

import (
"github.com/88250/lute/render"
"testing"

"github.com/88250/lute"
Expand Down Expand Up @@ -175,6 +176,7 @@ func TestVditorIRDOM2Md(t *testing.T) {

var vditorIRBlockDOM2MdTests = []parseTest{

{"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=\"20201024153456-ktna8mm\"}\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"},
{"1", "<ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\" data-node-id=\"id\" data-type=\"ul\"><li data-marker=\"*\" data-node-id=\"fooid\">foo</li></ul>", "* {: id=\"fooid\"}foo\n{: id=\"id\"}\n"},
{"0", "<ul data-tight=\"true\" data-marker=\"-\" data-block=\"0\" data-node-id=\"20200910154204-c4bobg8\"><li data-marker=\"-\" class=\"vditor-task\" data-node-id=\"\"><input checked=\"\" type=\"checkbox\"></li></ul>", "- [X]\n{: id=\"20200910154204-c4bobg8\"}\n"},
Expand All @@ -184,6 +186,7 @@ func TestVditorIRBlockDOM2Md(t *testing.T) {
luteEngine := lute.New()
luteEngine.KramdownIAL = true

render.Testing = true
for _, test := range vditorIRBlockDOM2MdTests {
md := luteEngine.VditorIRBlockDOM2Md(test.from)
if test.to != md {
Expand Down
41 changes: 21 additions & 20 deletions vditor_wysiwyg.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ func (lute *Lute) adjustVditorDOMListItemInP(n *html.Node) {
}

// 在 li 下的每个非块容器节点用 p 包裹
var nodes []*html.Node
var lastc *html.Node
for c := n.FirstChild; nil != c; c = c.NextSibling {
if lute.listItemEnter(n) {
p := &html.Node{Type: html.ElementNode, Data: "p", DataAtom: atom.P}
Expand All @@ -322,34 +320,37 @@ func (lute *Lute) adjustVditorDOMListItemInP(n *html.Node) {
}

if atom.P != c.DataAtom && atom.Blockquote != c.DataAtom && atom.Ul != c.DataAtom && atom.Ol != c.DataAtom && atom.Div != c.DataAtom {
nodes = append(nodes, c)
} else if 0 < len(nodes) {
spans, nextBlock := lute.forwardNextBlock(c)
p := &html.Node{Type: html.ElementNode, Data: "p", DataAtom: atom.P}
for _, pChild := range nodes {
pChild.Unlink()
p.AppendChild(pChild)
}
nodes = nil
c.InsertBefore(p)
c = p
}
if nil == c.NextSibling {
lastc = c
for _, span := range spans {
span.Unlink()
p.AppendChild(span)
}
if c = nextBlock;nil == c {
break
}
}
}
if 0 < len(nodes) {
p := &html.Node{Type: html.ElementNode, Data: "p", DataAtom: atom.P}
lastc.InsertBefore(p)
lastc.Unlink()
p.AppendChild(lastc)
}
}

for c := n.FirstChild; c != nil; c = c.NextSibling {
lute.adjustVditorDOMListItemInP(c)
}
}

// forwardNextBlock 向前移动至下一个块级节点,即跳过行级节点。
func (lute *Lute) forwardNextBlock(spanNode *html.Node) (spans []*html.Node, nextBlock *html.Node) {
for next := spanNode; nil != next; next = next.NextSibling {
switch next.DataAtom {
case atom.Ol,atom.Ul,atom.Div,atom.Blockquote:
return
}
spans = append(spans, next)
}
return
}

func (lute *Lute) listItemEnter(li *html.Node) bool {
if nil == li.FirstChild {
return false
Expand Down Expand Up @@ -379,7 +380,7 @@ func (lute *Lute) isTightList(list *html.Node) string {
return "false"
}

if 1 < subParagraphs + subDivs || 1 < subParagraphs + subBlockquotes || 1 < subParagraphs + subLists {
if 1 < subParagraphs+subDivs || 1 < subParagraphs+subBlockquotes || 1 < subParagraphs+subLists {
return "false"
}

Expand Down

0 comments on commit 4d01c46

Please sign in to comment.