2018-01-25 21:01:19 +00:00
|
|
|
package markdown
|
2016-04-04 07:41:05 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-16 05:26:04 +00:00
|
|
|
"bytes"
|
2019-11-07 08:56:45 +00:00
|
|
|
"encoding/json"
|
2016-04-04 07:41:05 +00:00
|
|
|
"regexp"
|
2019-11-07 08:56:45 +00:00
|
|
|
"strings"
|
2016-04-04 07:41:05 +00:00
|
|
|
"testing"
|
2018-01-28 02:22:11 +00:00
|
|
|
|
2020-02-10 16:46:14 +00:00
|
|
|
"github.com/status-im/markdown/parser"
|
2016-04-04 07:41:05 +00:00
|
|
|
)
|
|
|
|
|
2016-04-04 08:18:50 +00:00
|
|
|
type TestParams struct {
|
2018-01-28 02:50:27 +00:00
|
|
|
extensions parser.Extensions
|
|
|
|
referenceOverride parser.ReferenceOverrideFunc
|
2016-04-04 08:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 08:56:45 +00:00
|
|
|
func runMarkdown(input string, params TestParams) (string, error) {
|
2018-01-28 03:45:55 +00:00
|
|
|
parser := parser.NewWithExtensions(params.extensions)
|
2018-01-26 08:11:58 +00:00
|
|
|
parser.ReferenceOverride = params.referenceOverride
|
2019-11-07 08:56:45 +00:00
|
|
|
result := parser.Parse([]byte(input))
|
|
|
|
if result == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
output, err := json.Marshal(result)
|
|
|
|
return string(output), err
|
2016-04-04 07:41:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 21:03:02 +00:00
|
|
|
// doTests runs full document tests using MarkdownCommon configuration.
|
|
|
|
func doTests(t *testing.T, tests []string) {
|
|
|
|
doTestsParam(t, tests, TestParams{
|
2018-01-28 02:50:27 +00:00
|
|
|
extensions: parser.CommonExtensions,
|
2016-07-15 21:03:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-28 02:50:27 +00:00
|
|
|
func doTestsBlock(t *testing.T, tests []string, extensions parser.Extensions) {
|
2016-04-04 11:00:26 +00:00
|
|
|
doTestsParam(t, tests, TestParams{
|
2017-02-02 07:35:10 +00:00
|
|
|
extensions: extensions,
|
2016-04-04 11:00:26 +00:00
|
|
|
})
|
2016-04-04 09:18:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 11:00:26 +00:00
|
|
|
func doTestsParam(t *testing.T, tests []string, params TestParams) {
|
2019-02-02 08:17:14 +00:00
|
|
|
for i := 0; i+1 < len(tests); i += 2 {
|
|
|
|
input := tests[i]
|
2019-11-07 08:56:45 +00:00
|
|
|
expected := strings.TrimRight(tests[i+1], "\n")
|
|
|
|
got, err := runMarkdown(input, params)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to marshal json: %+v\n", err)
|
|
|
|
}
|
2019-02-02 08:17:14 +00:00
|
|
|
if got != expected {
|
2019-02-03 06:33:23 +00:00
|
|
|
t.Errorf("\nInput [%#v]\nExpected[%#v]\nGot [%#v]\nInput:\n%s\nExpected:\n%s\nGot:\n%s\n",
|
2019-02-02 08:17:14 +00:00
|
|
|
input, expected, got, input, expected, got)
|
2016-04-04 07:41:05 +00:00
|
|
|
}
|
2019-02-02 08:17:14 +00:00
|
|
|
}
|
2016-04-04 07:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func doTestsInline(t *testing.T, tests []string) {
|
2016-04-04 08:18:50 +00:00
|
|
|
doTestsInlineParam(t, tests, TestParams{})
|
2016-04-04 07:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func doLinkTestsInline(t *testing.T, tests []string) {
|
|
|
|
doTestsInline(t, tests)
|
|
|
|
|
|
|
|
prefix := "http://localhost"
|
|
|
|
transformTests := transformLinks(tests, prefix)
|
2019-11-07 08:56:45 +00:00
|
|
|
doTestsInlineParam(t, transformTests, TestParams{})
|
|
|
|
doTestsInlineParam(t, transformTests, TestParams{})
|
2016-04-04 07:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func doSafeTestsInline(t *testing.T, tests []string) {
|
2019-11-07 08:56:45 +00:00
|
|
|
doTestsInlineParam(t, tests, TestParams{})
|
2016-04-04 07:41:05 +00:00
|
|
|
|
|
|
|
// All the links in this test should not have the prefix appended, so
|
|
|
|
// just rerun it with different parameters and the same expectations.
|
|
|
|
prefix := "http://localhost"
|
|
|
|
transformTests := transformLinks(tests, prefix)
|
2019-11-07 08:56:45 +00:00
|
|
|
doTestsInlineParam(t, transformTests, TestParams{})
|
2016-04-04 07:41:05 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 08:18:50 +00:00
|
|
|
func doTestsInlineParam(t *testing.T, tests []string, params TestParams) {
|
2018-01-28 02:50:27 +00:00
|
|
|
params.extensions |= parser.Autolink | parser.Strikethrough
|
2016-04-04 11:00:26 +00:00
|
|
|
doTestsParam(t, tests, params)
|
2016-04-04 07:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func transformLinks(tests []string, prefix string) []string {
|
|
|
|
newTests := make([]string, len(tests))
|
|
|
|
anchorRe := regexp.MustCompile(`<a href="/(.*?)"`)
|
|
|
|
imgRe := regexp.MustCompile(`<img src="/(.*?)"`)
|
|
|
|
for i, test := range tests {
|
|
|
|
if i%2 == 1 {
|
|
|
|
test = anchorRe.ReplaceAllString(test, `<a href="`+prefix+`/$1"`)
|
|
|
|
test = imgRe.ReplaceAllString(test, `<img src="`+prefix+`/$1"`)
|
|
|
|
}
|
|
|
|
newTests[i] = test
|
|
|
|
}
|
|
|
|
return newTests
|
|
|
|
}
|
|
|
|
|
2018-04-16 05:26:04 +00:00
|
|
|
func normalizeNewlines(d []byte) []byte {
|
|
|
|
// replace CR LF (windows) with LF (unix)
|
|
|
|
d = bytes.Replace(d, []byte{13, 10}, []byte{10}, -1)
|
|
|
|
// replace CF (mac) with LF (unix)
|
|
|
|
d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
|
|
|
|
return d
|
|
|
|
}
|