Skip to content

Commit

Permalink
markup: trailing spaces in code block fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Mar 26, 2021
1 parent ff8b5ce commit 2717d62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 13 additions & 7 deletions tdmarkup/markup_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (s *MarkupScanner) scanInline(marker rune, typ tdproto.MarkupType, allowWhi
start := s.Position()

var b strings.Builder
b.Grow(s.Length()-start)
b.Grow(s.Length() - start)
b.WriteRune(s.TakeNext())

if !(start == 0 || isWhitespace(s.Prev()) || isEOL(s.Prev()) || allowWhitespaceAround) {
Expand Down Expand Up @@ -260,7 +260,7 @@ func (s *MarkupScanner) scanBlock(op, cl []rune, typ tdproto.MarkupType) (string
}

var b strings.Builder
b.Grow(s.Length()-start)
b.Grow(s.Length() - start)
b.WriteString(t)

e := &tdproto.MarkupEntity{
Expand All @@ -279,22 +279,28 @@ func (s *MarkupScanner) scanBlock(op, cl []rune, typ tdproto.MarkupType) (string
b.WriteRune(s.TakeNext())
}

var tail []rune
for s.Rest() > 0 {
t := s.ScanUntil(cl)
if t == "" {
b.WriteRune(s.TakeNext())
ch := s.TakeNext()
b.WriteRune(ch)
tail = append(tail, ch)
continue
}
b.WriteString(t)
e.Close = s.Position() - len(cl)
e.CloseLength = len(cl)

res := b.String()
if strings.HasSuffix(res, "\n"+string(cl)) { // XXX:
for i := len(tail) - 1; i >= 0; i-- {
ch := tail[i]
if !(isWhitespace(ch) || isEOL(ch)) {
break
}
e.Close--
e.CloseLength++
}
return res, e
return b.String(), e
}

s.Rewind(start)
Expand All @@ -308,7 +314,7 @@ func (s *MarkupScanner) scanQuote() (string, *tdproto.MarkupEntity) {
}

var b strings.Builder
b.Grow(s.Length()-s.Position())
b.Grow(s.Length() - s.Position())
b.WriteString(t)

e := &tdproto.MarkupEntity{
Expand Down
6 changes: 6 additions & 0 deletions tdmarkup/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,10 @@ var MarkupTestCases = []struct {
Html: "<u>hey</u> <b>hop😂</b> <u>лалалей</u>",
Plain: "hey hop😂 лалалей",
},
{
Title: "multi pre",
Raw: "``` 1 ```2``` ```3",
Html: "<pre>1</pre>2<pre></pre>3",
//Plain: "``` 1 ```2``` ```3```",
},
}

0 comments on commit 2717d62

Please sign in to comment.