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", ` + + + + + + + + + + + + + + + + + + +
MonthSavings
January$100
February$80
+ +`, "| Month | Savings |\n| - | - |\n| January | $100 |\n| February | $80 |\n"}, {"26", "
TypeOr… to Get
*Italic*_Italic_Italic
**Bold**__Bold__Bold
# Heading 1Heading 1
=========

Heading 1

## Heading 2Heading 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
\"Markdown\"
> Blockquote 
Blockquote

* List
* List
* List

- List
- List
- List

  • List
  • List
  • List
", "| Type | Or | … to Get |\n| - | - | - |\n| *Italic* | _Italic_ | *Italic* |\n| **Bold** | __Bold__ | **Bold** |\n| # Heading 1 | Heading 1
========= | # Heading 1 |\n| ## Heading 2 | Heading 2
--------- | ## Heading 2 |\n| [Link](http://a.com) | [Link][1]

[1]: http://b.org | [Link](https://commonmark.org/) |\n| ![Image](http://url/a.png) | ![Image][1]

[1]: http://url/b.jpg | ![Markdown](https://commonmark.org/help/images/favicon.png) |\n| > Blockquote | | > Blockquote |\n| * List
* List
* List | - List
- List
- List
| * List* List* List |\n"}, {"25", "
ElementMarkdown Syntax
Table| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |
Fenced Code Block```
{
  \"firstName\": \"John\",
  \"lastName\": \"Smith\",
  \"age\": 25
}
```
", "| Element | Markdown Syntax |\n| - | - |\n| [Table](https://www.markdownguide.org/extended-syntax/#tables) | \\| 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", "
ElementMarkdown Syntax
Table| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |
", "| Element | Markdown Syntax |\n| - | - |\n| Table | \\| 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) } }