1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-12-23 12:56:42 +00:00
cchat-discord/segments/codeblock.go

49 lines
873 B
Go
Raw Normal View History

2020-06-19 01:00:24 +00:00
package segments
import (
"github.com/diamondburned/cchat/text"
"github.com/yuin/goldmark/ast"
)
type CodeblockSegment struct {
start, end int
language string
}
var _ text.Codeblocker = (*CodeblockSegment)(nil)
func (r *TextRenderer) codeblock(n *ast.FencedCodeBlock, enter bool) ast.WalkStatus {
if enter {
r.startBlock()
defer r.endBlock()
2020-06-19 01:00:24 +00:00
// Create a segment.
seg := CodeblockSegment{
start: r.i(),
language: string(n.Language(r.src)),
}
// Join all lines together.
var lines = n.Lines()
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
r.buf.Write(line.Value(r.src))
}
// Close the segment.
seg.end = r.i()
r.append(seg)
}
return ast.WalkContinue
}
func (c CodeblockSegment) Bounds() (start, end int) {
return c.start, c.end
}
func (c CodeblockSegment) CodeblockLanguage() string {
return c.language
}