add unescape table processing after md parsing #354
This commit is contained in:
@@ -38,6 +38,10 @@ func TestComment_Sanitize(t *testing.T) {
|
||||
inp: Comment{Text: "blah & & 123 — —"},
|
||||
out: Comment{Text: `blah & & 123 — —`},
|
||||
},
|
||||
{
|
||||
inp: Comment{Text: "blah & & 123 — —"},
|
||||
out: Comment{Text: `blah & & 123 — —`},
|
||||
},
|
||||
}
|
||||
|
||||
for n, tt := range tbl {
|
||||
|
||||
@@ -49,6 +49,7 @@ func (f *CommentFormatter) FormatText(txt string) (res string) {
|
||||
})
|
||||
|
||||
res = string(bf.Run([]byte(txt), bf.WithExtensions(mdExt), bf.WithRenderer(rend)))
|
||||
res = f.unEscape(res)
|
||||
|
||||
for _, conv := range f.converters {
|
||||
res = conv.Convert(res)
|
||||
@@ -90,3 +91,16 @@ func (f *CommentFormatter) shortenAutoLinks(commentHTML string, max int) (resHTM
|
||||
}
|
||||
return resHTML
|
||||
}
|
||||
|
||||
func (f *CommentFormatter) unEscape(txt string) (res string) {
|
||||
elems := []struct {
|
||||
from, to string
|
||||
}{
|
||||
{`—`, "—"},
|
||||
}
|
||||
res = txt
|
||||
for _, e := range elems {
|
||||
res = strings.Replace(res, e.from, e.to, -1)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -14,17 +14,23 @@ func (m mockConverter) Convert(text string) string { return text + "!converted"
|
||||
func TestFormatter_FormatText(t *testing.T) {
|
||||
tbl := []struct {
|
||||
in, out string
|
||||
name string
|
||||
}{
|
||||
{"", "!converted"},
|
||||
{"12345 abc", "<p>12345 abc</p>\n!converted"},
|
||||
{"**xyz** _aaa_ - \"sfs\"", "<p><strong>xyz</strong> <em>aaa</em> – «sfs»</p>\n!converted"},
|
||||
{"", "!converted", "empty"},
|
||||
{"12345 abc", "<p>12345 abc</p>\n!converted", "simple"},
|
||||
{"**xyz** _aaa_ - \"sfs\"", "<p><strong>xyz</strong> <em>aaa</em> – «sfs»</p>\n!converted", "format"},
|
||||
{
|
||||
"http://127.0.0.1/some-long-link/12345/678901234567890", "<p><a href=\"http://127.0.0.1/some-long-link/12345/678901234567890\">http://127.0.0.1/some-long-link/12345/6789012...</a></p>\n!converted",
|
||||
"http://127.0.0.1/some-long-link/12345/678901234567890",
|
||||
"<p><a href=\"http://127.0.0.1/some-long-link/12345/678901234567890\">http://127.0.0." +
|
||||
"1/some-long-link/12345/6789012...</a></p>\n!converted", "links",
|
||||
},
|
||||
{"— not translated #354", "<p>— not translated #354</p>\n!converted", "mdash"},
|
||||
}
|
||||
f := NewCommentFormatter(mockConverter{})
|
||||
for n, tt := range tbl {
|
||||
assert.Equal(t, tt.out, f.FormatText(tt.in), "check #%d", n)
|
||||
for _, tt := range tbl {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equal(t, tt.out, f.FormatText(tt.in))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user