Skip to content

Commit

Permalink
🎨 改进格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 5, 2020
1 parent 434b213 commit 812ac6f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
21 changes: 21 additions & 0 deletions format_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package lute
import (
"bytes"
"strconv"
"unicode"
"unicode/utf8"
)

// FormatRenderer 描述了格式化渲染器。
Expand Down Expand Up @@ -359,6 +361,25 @@ func (r *FormatRenderer) renderText(node *Node, entering bool) (WalkStatus, erro
}

func (r *FormatRenderer) renderCodeSpan(node *Node, entering bool) (WalkStatus, error) {
if entering {
if r.option.AutoSpace {
if text := node.PreviousNodeText(); "" != text {
lastc, _ := utf8.DecodeLastRuneInString(text)
if unicode.IsLetter(lastc) || unicode.IsDigit(lastc) {
r.writeByte(itemSpace)
}
}
}
} else {
if r.option.AutoSpace {
if text := node.NextNodeText(); "" != text {
firstc, _ := utf8.DecodeRuneInString(text)
if unicode.IsLetter(firstc) || unicode.IsDigit(firstc) {
r.writeByte(itemSpace)
}
}
}
}
return WalkContinue, nil
}

Expand Down
21 changes: 21 additions & 0 deletions html_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package lute
import (
"strconv"
"strings"
"unicode"
"unicode/utf8"
)

// HTMLRenderer 描述了 HTML 渲染器。
Expand Down Expand Up @@ -484,6 +486,25 @@ func (r *HTMLRenderer) renderText(node *Node, entering bool) (WalkStatus, error)
}

func (r *HTMLRenderer) renderCodeSpan(node *Node, entering bool) (WalkStatus, error) {
if entering {
if r.option.AutoSpace {
if text := node.PreviousNodeText(); "" != text {
lastc, _ := utf8.DecodeLastRuneInString(text)
if unicode.IsLetter(lastc) || unicode.IsDigit(lastc) {
r.writeByte(itemSpace)
}
}
}
} else {
if r.option.AutoSpace {
if text := node.NextNodeText(); "" != text {
firstc, _ := utf8.DecodeRuneInString(text)
if unicode.IsLetter(firstc) || unicode.IsDigit(firstc) {
r.writeByte(itemSpace)
}
}
}
}
return WalkContinue, nil
}

Expand Down
4 changes: 2 additions & 2 deletions 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.

1 change: 1 addition & 0 deletions test/format-case3-formatted.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
> 段落二
>
>> 嵌套
>>
>
> 还原嵌套
8 changes: 7 additions & 1 deletion test/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type formatTest struct {

var formatTests = []formatTest{

{"36", "`foo`、`bar`\n", "`foo`、`bar`\n"},
{"35", "foo`bar`\n", "foo `bar`\n"},
{"34", "`bar`\n", "`bar`\n"},
{"33", "foo`bar`baz\n", "foo `bar` baz\n"},

// 格式化后丢失反斜杠 https://github.com/88250/lute/issues/16
{"32", "|foo|\n|-|\n|`\\|bar`|\n", "| foo |\n| ------- |\n| `\\|bar` |\n"},
{"31", "|foo|\n|-|\n|\\|bar|\n", "| foo |\n| ----- |\n| \\|bar |\n"},
Expand Down Expand Up @@ -94,7 +99,8 @@ func TestFormatCases(t *testing.T) {
t.Fatalf("read test dir failed: %s", err)
}

skips := "format-case0.md,format-case1.md,format-case2.md,format-case3.md,format-case4.md,format-case5.md" // 用于跳过测试文件,例如 format-case0.md
//skips := "format-case0.md,format-case1.md,format-case2.md,format-case3.md,format-case4.md,format-case5.md" // 用于跳过测试文件,例如 format-case0.md
skips := ""

for _, file := range files {
if !strings.HasPrefix(file.Name(), "format-case") || strings.Contains(file.Name(), "formatted") {
Expand Down
2 changes: 1 addition & 1 deletion test/term_typo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var termTypoTests = []parseTest{
{"3", "github.com\n", "<p>github.com</p>\n"},
{"2", "https://github.com\n", "<p><a href=\"https://github.com\">https://github.com</a></p>\n"},
{"1", "特别是简历中千万不要出现这样的情况:熟练使用JAVA、Javascript、GIT,对android、ios开发有一定了解,熟练使用Mysql、postgresql数据库。\n", "<p>特别是简历中千万不要出现这样的情况:熟练使用 Java、JavaScript、Git,对 Android、iOS 开发有一定了解,熟练使用 MySQL、PostgreSQL 数据库。</p>\n"},
{"0", "在github上做开源项目是一件很开心的事情,请不要把Github拼写成`github`哦!\n", "<p>在 GitHub 上做开源项目是一件很开心的事情,请不要把 GitHub 拼写成<code>github</code>哦!</p>\n"},
{"0", "在github上做开源项目是一件很开心的事情,请不要把Github拼写成`github`哦!\n", "<p>在 GitHub 上做开源项目是一件很开心的事情,请不要把 GitHub 拼写成 <code>github</code> 哦!</p>\n"},
}

func TestTermTypo(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions test/v2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ var vditorDOM2MdTests = []parseTest{
{"57", "<h3>隐藏细节</h3><div class=\"vditor-wysiwyg__block\" data-type=\"html-block\"><pre><code>&lt;details&gt;\n&lt;summary&gt;\n这里是摘要部分。\n&lt;/summary&gt;\n这里是细节部分。&lt;/details&gt;<br></code></pre><div class=\"vditor-wysiwyg__preview\" contenteditable=\"false\" data-render=\"false\"></div></div><p>1<wbr></p>", "### 隐藏细节\n\n<details>\n<summary>\n这里是摘要部分。\n</summary>\n这里是细节部分。</details>\n\n1\n"},
{"56", "<p>~删除线~</p>", "~删除线~\n"},
{"55", "<ul data-tight=\"true\"><li data-marker=\"*\">foo</li><li data-marker=\"*\"><br></li><li data-marker=\"*\"><wbr>bar</li></ul>", "* foo\n*\n* bar\n"},
{"54", "<p>f<code>o</code><wbr>o\n</p>", "f`o`o\n"},
{"54", "<p>f<code>o</code><wbr>o\n</p>", "f `o` o\n"},
{"53", "<blockquote><p><br></p><p><wbr>foo\n</p></blockquote>", "> foo\n"}, // 在块引用第一个字符前换行
{"52", "<blockquote><p>foo\n</p><blockquote><p>bar<wbr>\n</p></blockquote></blockquote>", "> foo\n>\n> > bar\n> >\n"},
{"52", "<blockquote><p>foo\n</p><blockquote><p>bar<wbr>\n</p></blockquote></blockquote>", "> foo\n>\n>> bar\n>>\n"},
{"51", "<blockquote><blockquote><p><wbr>\n</p></blockquote></blockquote>", "\n"},
{"50", "<blockquote><p><wbr>\n</p></blockquote>", "\n"},
{"49", "<blockquote><p>f<wbr>\n</p></blockquote>", "> f\n"},
Expand Down
7 changes: 7 additions & 0 deletions vditor_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"bytes"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)

// VditorRenderer 描述了 Vditor DOM 渲染器。
Expand Down Expand Up @@ -455,6 +457,11 @@ func (r *VditorRenderer) renderCodeSpan(node *Node, entering bool) (WalkStatus,
previousNodeText = strings.ReplaceAll(previousNodeText, caret, "")
if "" == previousNodeText {
r.writeString(zwsp)
} else {
lastc, _ := utf8.DecodeLastRuneInString(previousNodeText)
if unicode.IsLetter(lastc) || unicode.IsDigit(lastc) {
r.writeByte(itemSpace)
}
}
}
return WalkContinue, nil
Expand Down

0 comments on commit 812ac6f

Please sign in to comment.