Skip to content

Commit

Permalink
🎨 Vditor 支持
Browse files Browse the repository at this point in the history
Copy Paste multiline inside `table`
Fix Vanessa219/vditor#368
  • Loading branch information
88250 committed May 7, 2020
1 parent f770d29 commit ac602a1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 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.

42 changes: 32 additions & 10 deletions render/format_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ func (r *FormatRenderer) renderDocument(node *ast.Node, entering bool) ast.WalkS

func (r *FormatRenderer) renderParagraph(node *ast.Node, entering bool) ast.WalkStatus {
if !entering {
r.Newline()

if !node.ParentIs(ast.NodeTableCell) {
r.Newline()
}
inTightList := false
lastListItemLastPara := false
if parent := node.Parent; nil != parent {
Expand All @@ -402,7 +403,7 @@ func (r *FormatRenderer) renderParagraph(node *ast.Node, entering bool) ast.Walk
}
}

if !inTightList || (lastListItemLastPara) {
if (!inTightList || (lastListItemLastPara)) && !node.ParentIs(ast.NodeTableCell) {
r.WriteByte(lex.ItemNewline)
}
}
Expand Down Expand Up @@ -685,7 +686,9 @@ func (r *FormatRenderer) renderBlockquote(node *ast.Node, entering bool) ast.Wal
buf = bytes.TrimSpace(r.Writer.Bytes())
r.Writer.Reset()
r.Write(buf)
r.WriteString("\n\n")
if !node.ParentIs(ast.NodeTableCell) { // 在表格中不能换行,否则会破坏表格的排版 https://github.com/Vanessa219/vditor/issues/368
r.WriteString("\n\n")
}
}
return ast.WalkContinue
}
Expand Down Expand Up @@ -722,7 +725,7 @@ func (r *FormatRenderer) renderHeading(node *ast.Node, entering bool) ast.WalkSt
r.WriteString(" {" + util.BytesToStr(node.HeadingID) + "}")
}

if !node.ParentIs(ast.NodeTableCell) { // 在表格中不能换行,否则会破坏表格的排版 https://github.com/Vanessa219/vditor/issues/368
if !node.ParentIs(ast.NodeTableCell) {
r.Newline()
r.WriteByte(lex.ItemNewline)
}
Expand All @@ -746,7 +749,9 @@ func (r *FormatRenderer) renderList(node *ast.Node, entering bool) ast.WalkStatu
buf := bytes.TrimSpace(r.Writer.Bytes())
r.Writer.Reset()
r.Write(buf)
r.WriteString("\n\n")
if !node.ParentIs(ast.NodeTableCell) {
r.WriteString("\n\n")
}
}
return ast.WalkContinue
}
Expand Down Expand Up @@ -788,14 +793,23 @@ func (r *FormatRenderer) renderListItem(node *ast.Node, entering bool) ast.WalkS
}
listItemBuf.WriteByte(lex.ItemSpace)
buf = append(listItemBuf.Bytes(), buf...)
if node.ParentIs(ast.NodeTableCell) {
buf = bytes.ReplaceAll(buf, []byte("\n"), nil)
}
writer.Reset()
writer.Write(buf)
r.nodeWriterStack[len(r.nodeWriterStack)-1].Write(writer.Bytes())
buf = writer.Bytes()
if node.ParentIs(ast.NodeTableCell) {
buf = bytes.ReplaceAll(buf, []byte("\n"), nil)
}
r.nodeWriterStack[len(r.nodeWriterStack)-1].Write(buf)
r.Writer = r.nodeWriterStack[len(r.nodeWriterStack)-1]
buf = bytes.TrimSpace(r.Writer.Bytes())
r.Writer.Reset()
r.Write(buf)
r.WriteString("\n")
if !node.ParentIs(ast.NodeTableCell) {
r.WriteString("\n")
}
}
return ast.WalkContinue
}
Expand All @@ -812,15 +826,23 @@ func (r *FormatRenderer) renderTaskListItemMarker(node *ast.Node, entering bool)
}

func (r *FormatRenderer) renderThematicBreak(node *ast.Node, entering bool) ast.WalkStatus {
r.WriteString("---\n\n")
if node.ParentIs(ast.NodeTableCell) {
r.WriteString("<hr/>")
} else {
r.WriteString("---\n\n")
}
return ast.WalkStop
}

func (r *FormatRenderer) renderHardBreak(node *ast.Node, entering bool) ast.WalkStatus {
if !r.Option.SoftBreak2HardBreak {
r.WriteString("\\\n")
} else {
r.WriteByte(lex.ItemNewline)
if node.ParentIs(ast.NodeTableCell) {
r.WriteString("<br/>")
} else {
r.WriteByte(lex.ItemNewline)
}
}
return ast.WalkStop
}
Expand Down
1 change: 1 addition & 0 deletions test/h2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var html2MdTests = []parseTest{

{"26", "<table class=\"markdown-reference\"><thead><tr><th>Type</th><th class=\"second-example\">Or</th><th>… to Get</th></tr></thead><tbody><tr><td class=\"preformatted\">*Italic*</td><td class=\"preformatted second-example\">_Italic_</td><td><em>Italic</em></td></tr><tr><td class=\"preformatted\">**Bold**</td><td class=\"preformatted second-example\">__Bold__</td><td><strong>Bold</strong></td></tr><tr><td class=\"preformatted\"># Heading 1</td><td class=\"preformatted second-example\">Heading 1<br>=========</td><td><h1 class=\"smaller-h1\">Heading 1</h1></td></tr><tr><td class=\"preformatted\">## Heading 2</td><td class=\"preformatted second-example\">Heading 2<br>---------</td><td><h2 class=\"smaller-h2\">Heading 2</h2></td></tr><tr><td class=\"preformatted\">[Link](http://a.com)</td><td class=\"preformatted second-example\">[Link][1]<br>⋮<br>[1]: http://b.org</td><td><a href=\"https://commonmark.org/\">Link</a></td></tr><tr><td class=\"preformatted\">![Image](http://url/a.png)</td><td class=\"preformatted second-example\">![Image][1]<br>⋮<br>[1]: http://url/b.jpg</td><td><img src=\"https://commonmark.org/help/images/favicon.png\" width=\"36\" height=\"36\" alt=\"Markdown\"></td></tr><tr><td class=\"preformatted\">&gt; Blockquote</td><td class=\"preformatted second-example\">&nbsp;</td><td><blockquote>Blockquote</blockquote></td></tr><tr><td class=\"preformatted\"><p>* List<br>* List<br>* List</p></td><td class=\"preformatted second-example\"><p>- List<br>- List<br>- List<br></p></td><td><ul><li>List</li><li>List</li><li>List</li></ul></td></tr></tbody></table>", "| Type | Or | … to Get |\n| - | - | - |\n| *Italic* | _Italic_ | *Italic* |\n| **Bold** | __Bold__ | **Bold** |\n| # Heading 1 | Heading 1<br/>========= | # Heading 1 |\n| ## Heading 2 | Heading 2<br/>--------- | ## Heading 2 |\n| [Link](http://a.com) | [Link][1]<br/>⋮<br/>[1]: http://b.org | [Link](https://commonmark.org/) |\n| ![Image](http://url/a.png) | ![Image][1]<br/>⋮<br/>[1]: http://url/b.jpg | ![Markdown](https://commonmark.org/help/images/favicon.png) |\n| > Blockquote | | > Blockquote |\n| * List<br/>* List<br/>* List | - List<br/>- List<br/>- List<br/> | * List* List* List |\n"},
{"25", "<table class=\"table table-bordered\"><thead class=\"thead-light\"><tr><th>Element</th><th>Markdown Syntax</th></tr></thead><tbody><tr><td><a href=\"https://www.markdownguide.org/extended-syntax/#tables\">Table</a></td><td><code>| Syntax | Description |<br>| ----------- | ----------- |<br>| Header | Title |<br>| Paragraph | Text |</code></td></tr><tr><td><a href=\"https://www.markdownguide.org/extended-syntax/#fenced-code-blocks\">Fenced Code Block</a></td><td><code>```<br>{<br>&nbsp;&nbsp;\"firstName\": \"John\",<br>&nbsp;&nbsp;\"lastName\": \"Smith\",<br>&nbsp;&nbsp;\"age\": 25<br>}<br>```</code></td></tr></tbody></table>", "| Element | Markdown Syntax |\n| - | - |\n| [Table](https://www.markdownguide.org/extended-syntax/#tables) | <code>\\| Syntax \\| Description \\|\\| ----------- \\| ----------- \\|\\| Header \\| Title \\|\\| Paragraph \\| Text \\|</code> |\n| [Fenced Code Block](https://www.markdownguide.org/extended-syntax/#fenced-code-blocks) | <code>```{\u00a0\u00a0\"firstName\": \"John\",\u00a0\u00a0\"lastName\": \"Smith\",\u00a0\u00a0\"age\": 25}```</code> |\n"},
{"24", "<table><thead><tr><th>Element</th><th>Markdown Syntax</th></tr></thead><tbody><tr><td>Table</td><td><code>| Syntax | Description |<br>| ----------- | ----------- |<br>| Header | Title |<br>| Paragraph | Text |</code></td></tr></tbody></table>", "| Element | Markdown Syntax |\n| - | - |\n| Table | <code>\\| Syntax \\| Description \\|\\| ----------- \\| ----------- \\|\\| Header \\| Title \\|\\| Paragraph \\| Text \\|</code> |\n"},
{"23", "<h2 style=\"box-sizing: border-box; margin-top: 24px; margin-bottom: 16px; font-weight: 600; font-size: 1.5em; line-height: 1.25; padding-bottom: 0.3em; border-bottom: 1px solid rgb(234, 236, 239); color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;\"><g-emoji class=\"g-emoji\" alias=\"m\" fallback-src=\"https://github.githubassets.com/images/icons/emoji/unicode/24c2.png\" style=\"box-sizing: border-box; font-family: &quot;Apple Color Emoji&quot;, &quot;Segoe UI&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 1.2em; font-weight: 400; line-height: 20px; vertical-align: middle; font-style: normal !important;\">Ⓜ️</g-emoji><span> </span>Markdown User Guide</h2>", "## Ⓜ️ Markdown User Guide\n"},
Expand Down
2 changes: 0 additions & 2 deletions test/spinv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

// +build javascript

package test

import (
Expand Down
2 changes: 0 additions & 2 deletions test/v2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

// +build javascript

package test

import (
Expand Down

0 comments on commit ac602a1

Please sign in to comment.