diff --git a/test/h2m_test.go b/test/h2m_test.go index b96160f355..68f11e0b4a 100644 --- a/test/h2m_test.go +++ b/test/h2m_test.go @@ -18,7 +18,28 @@ import ( var html2MdTests = []parseTest{ - {"26", "", "\n"}, + {"27", ` +
+Month | +Savings | +
---|---|
January | +$100 | +
February | +$80 | +
Type | Or | … to Get |
---|---|---|
*Italic* | _Italic_ | Italic |
**Bold** | __Bold__ | Bold |
# Heading 1 | Heading 1 ========= | Heading 1 |
## Heading 2 | Heading 2 --------- | Heading 2 |
[Link](http://a.com) | [Link][1] ⋮ [1]: http://b.org | Link |
![Image](http://url/a.png) | ![Image][1] ⋮ [1]: http://url/b.jpg | |
> Blockquote | Blockquote | |
* List | - List |
|
Element | Markdown Syntax |
---|---|
Table | | Syntax | Description | |
Fenced Code Block | ``` |
\\| Syntax \\| Description \\|\\| ----------- \\| ----------- \\|\\| Header \\| Title \\|\\| Paragraph \\| Text \\|
|\n| [Fenced Code Block](https://www.markdownguide.org/extended-syntax/#fenced-code-blocks) | ```{\u00a0\u00a0\"firstName\": \"John\",\u00a0\u00a0\"lastName\": \"Smith\",\u00a0\u00a0\"age\": 25}```
|\n"},
{"24", "Element | Markdown Syntax |
---|---|
Table | | Syntax | Description | |
\\| Syntax \\| Description \\|\\| ----------- \\| ----------- \\|\\| Header \\| Title \\|\\| Paragraph \\| Text \\|
|\n"},
diff --git a/vditor.go b/vditor.go
index dd5731d4aa..575bac4d95 100644
--- a/vditor.go
+++ b/vditor.go
@@ -208,12 +208,30 @@ func (lute *Lute) adjustVditorDOM(nodes []*html.Node) {
}
}
+ var emptyTextNodes []*html.Node
for c := nodes[0]; nil != c; c = c.NextSibling {
- lute.adjustVditorDOM0(c)
+ lute.adjustVditorDOM0(c, &emptyTextNodes)
+ }
+
+ for _, emptyTextNode := range emptyTextNodes {
+ if parent := emptyTextNode.Parent; nil != parent && (atom.Table == parent.DataAtom || atom.Thead == parent.DataAtom || atom.Tbody == parent.DataAtom || atom.Tr == parent.DataAtom) {
+ emptyTextNode.Unlink()
+ continue
+ }
+
+ if nil == emptyTextNode.PrevSibling || nil == emptyTextNode.NextSibling {
+ // 没有前后节点的话保留该空白
+ continue
+ }
+ if atom.Span == emptyTextNode.PrevSibling.DataAtom || atom.Span == emptyTextNode.NextSibling.DataAtom {
+ // 前节点或者后节点是 span 的话保留该空白
+ continue
+ }
+ emptyTextNode.Unlink()
}
}
-func (lute *Lute) adjustVditorDOM0(n *html.Node) {
+func (lute *Lute) adjustVditorDOM0(n *html.Node, emptyTextNodes *[]*html.Node) {
switch n.DataAtom {
case atom.Li:
// 在 li 下的每个非块容器节点用 p 包裹
@@ -272,10 +290,14 @@ func (lute *Lute) adjustVditorDOM0(n *html.Node) {
}
}
}
+ case 0:
+ if "" == strings.TrimSpace(n.Data) {
+ *emptyTextNodes = append(*emptyTextNodes, n)
+ }
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
- lute.adjustVditorDOM0(c)
+ lute.adjustVditorDOM0(c, emptyTextNodes)
}
}