Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Jul 13, 2022
1 parent 4e87838 commit b60c044
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
21 changes: 16 additions & 5 deletions modules/highlight/highlight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ c=2

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lines, err := File(tt.name, "", []byte(tt.code))
out, err := File(tt.name, "", []byte(tt.code))
assert.NoError(t, err)
assert.EqualValues(t, strings.Join(tt.want, "\n"), strings.Join(lines, "\n"))
expected := strings.Join(tt.want, "\n")
actual := strings.Join(out, "\n")
assert.Equal(t, strings.Count(actual, "<span"), strings.Count(actual, "</span>"))
assert.EqualValues(t, expected, actual)
})
}
}
Expand Down Expand Up @@ -134,14 +137,22 @@ b=''
{space}
c=2
`), "{space}", " "),
want: strings.Split("def:&#10;\n a=1&#10;\n&#10;\nb=&#39;&#39;&#10;\n &#10;\nc=2", "\n"),
want: lines(`
def:&#10;
a=1&#10;
&#10;
b=&#39;&#39;&#10;
&#10;
c=2`),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lines := PlainText([]byte(tt.code))
assert.EqualValues(t, strings.Join(tt.want, "\n"), strings.Join(lines, "\n"))
out := PlainText([]byte(tt.code))
expected := strings.Join(tt.want, "\n")
actual := strings.Join(out, "\n")
assert.EqualValues(t, expected, actual)
})
}
}
33 changes: 33 additions & 0 deletions modules/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/rand"
"errors"
"math/big"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -192,6 +193,38 @@ func ToTitleCase(s string) string {
return titleCaser.String(s)
}

var (
whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$")
leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])")
)

// Dedent removes common indentation of a multi-line string along with whitespace around it
// Based on https://github.com/lithammer/dedent
func Dedent(s string) string {
var margin string

s = whitespaceOnly.ReplaceAllString(s, "")
indents := leadingWhitespace.FindAllStringSubmatch(s, -1)

for i, indent := range indents {
if i == 0 {
margin = indent[1]
} else if strings.HasPrefix(indent[1], margin) {
continue
} else if strings.HasPrefix(margin, indent[1]) {
margin = indent[1]
} else {
margin = ""
break
}
}

if margin != "" {
s = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(s, "")
}
return strings.TrimSpace(s)
}

// NumberIntoInt64 transform a given int into int64.
func NumberIntoInt64(number interface{}) int64 {
var value int64
Expand Down
7 changes: 7 additions & 0 deletions modules/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,10 @@ func TestToTitleCase(t *testing.T) {
assert.Equal(t, ToTitleCase(`foo bar baz`), `Foo Bar Baz`)
assert.Equal(t, ToTitleCase(`FOO BAR BAZ`), `Foo Bar Baz`)
}

func TestDedent(t *testing.T) {
assert.Equal(t, Dedent(`
foo
bar
`), "foo\n\tbar")
}

0 comments on commit b60c044

Please sign in to comment.