mirror of
https://github.com/diamondburned/cchat-discord.git
synced 2024-12-23 12:56:42 +00:00
49 lines
873 B
Go
49 lines
873 B
Go
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()
|
|
|
|
// 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
|
|
}
|