From c488315c621d209f8f46ac6fe6cc4a0aa590c062 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 12 Jun 2020 17:32:27 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E8=B0=83=E7=94=A8HTML2Markdown=20?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2table=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98=20F?= =?UTF-8?q?ix=20#70?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- h2m.go | 8 ++++ test/h2m_test.go | 110 +++++++++++++++++++++++++++-------------------- 2 files changed, 72 insertions(+), 46 deletions(-) diff --git a/h2m.go b/h2m.go index af4a142da9..6be12d40b7 100644 --- a/h2m.go +++ b/h2m.go @@ -333,7 +333,15 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) { defer tree.Context.ParentTip() case atom.Tbody: case atom.Tr: + table := n.Parent.Parent node.Type = ast.NodeTableRow + if atom.Thead != table.FirstChild.DataAtom && n == n.Parent.FirstChild { + // 补全 thread 节点 + thead := &ast.Node{Type: ast.NodeTableHead} + tree.Context.Tip.AppendChild(thead) + tree.Context.Tip = thead + defer tree.Context.ParentTip() + } tree.Context.Tip.AppendChild(node) tree.Context.Tip = node defer tree.Context.ParentTip() diff --git a/test/h2m_test.go b/test/h2m_test.go index 68f11e0b4a..57de237813 100644 --- a/test/h2m_test.go +++ b/test/h2m_test.go @@ -18,55 +18,73 @@ import ( var html2MdTests = []parseTest{ - {"27", ` + {"28", ` - - - - - - - - - - - - - - - - - -
MonthSavings
January$100
February$80
+ + + + + + + + + + + + + +
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"}, - {"23", "

Ⓜ️ Markdown User Guide

", "## Ⓜ️ Markdown User Guide\n"}, - {"22", "
npm install vditor --save
", "```shell\nnpm install vditor --save\n```\n"}, - {"21", "

id

", "#### id\n"}, - {"20", "

What’s Markdown?

", "## What’s Markdown?\n"}, - {"19", "
`foo`
", "`foo`\n"}, - {"18", "foo", "~foo~\n"}, - {"17", "\"foo\"", "![foo](bar.png)\n"}, - {"16", "foo
bar", "foo\nbar\n"}, - {"15", "foo", "*foo*\n"}, - {"14", "
", "---\n"}, - {"13", "
foo
", "> foo\n"}, - {"12", "

foo

", "# foo\n"}, - {"11", "
  • foo
  • bar
  • ", "* foo\n* bar\n"}, - {"10", `

    foo'%'bar

    `, "foo'%'bar\n"}, - {"9", `>`, "`>`\n"}, - {"8", `
    foo
    `, "[foo](/bar)\n"}, - {"7", ``, "* Java\n * Spring\n"}, - {"6", `

    这是一篇讲解如何正确使用 Markdown 的排版示例,学会这个很有必要,能让你的文章有更佳清晰的排版。

    `, "这是一篇讲解如何正确使用 **Markdown** 的排版示例,学会这个很有必要,能让你的文章有更佳清晰的排版。\n"}, - {"5", ``, "* [X] 发布 Solo\n"}, - {"4", " 发布 Solo", "发布 Solo\n"}, - {"3", "
    ", "
    \n"}, - {"2", "
    // Lute - A structured markdown engine.
    // Copyright (c) 2019-present, b3log.org
    ", "// Lute - A structured Markdown engine.\n// Copyright (c) 2019-present, b3log.org\n"}, - {"1", "foo", "foo\n"}, - {"0", "

    foo

    ", "foo\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"}, +// {"23", "

    Ⓜ️ Markdown User Guide

    ", "## Ⓜ️ Markdown User Guide\n"}, +// {"22", "
    npm install vditor --save
    ", "```shell\nnpm install vditor --save\n```\n"}, +// {"21", "

    id

    ", "#### id\n"}, +// {"20", "

    What’s Markdown?

    ", "## What’s Markdown?\n"}, +// {"19", "
    `foo`
    ", "`foo`\n"}, +// {"18", "foo", "~foo~\n"}, +// {"17", "\"foo\"", "![foo](bar.png)\n"}, +// {"16", "foo
    bar", "foo\nbar\n"}, +// {"15", "foo", "*foo*\n"}, +// {"14", "
    ", "---\n"}, +// {"13", "
    foo
    ", "> foo\n"}, +// {"12", "

    foo

    ", "# foo\n"}, +// {"11", "
  • foo
  • bar
  • ", "* foo\n* bar\n"}, +// {"10", `

    foo'%'bar

    `, "foo'%'bar\n"}, +// {"9", `>`, "`>`\n"}, +// {"8", `
    foo
    `, "[foo](/bar)\n"}, +// {"7", ``, "* Java\n * Spring\n"}, +// {"6", `

    这是一篇讲解如何正确使用 Markdown 的排版示例,学会这个很有必要,能让你的文章有更佳清晰的排版。

    `, "这是一篇讲解如何正确使用 **Markdown** 的排版示例,学会这个很有必要,能让你的文章有更佳清晰的排版。\n"}, +// {"5", ``, "* [X] 发布 Solo\n"}, +// {"4", " 发布 Solo", "发布 Solo\n"}, +// {"3", "
    ", "
    \n"}, +// {"2", "
    // Lute - A structured markdown engine.
    // Copyright (c) 2019-present, b3log.org
    ", "// Lute - A structured Markdown engine.\n// Copyright (c) 2019-present, b3log.org\n"}, +// {"1", "foo", "foo\n"}, +// {"0", "

    foo

    ", "foo\n"}, } func TestHTML2Md(t *testing.T) {