1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-12-26 22:16:54 +00:00
cchat-discord/internal/segments/codeblock/codeblock.go

67 lines
1.3 KiB
Go
Raw Normal View History

package codeblock
import (
"github.com/diamondburned/cchat-discord/internal/segments/renderer"
"github.com/diamondburned/cchat/text"
"github.com/diamondburned/cchat/utils/empty"
"github.com/yuin/goldmark/ast"
)
func init() {
2020-11-06 06:14:52 +00:00
renderer.Register(ast.KindFencedCodeBlock, codeblock)
}
func codeblock(r *renderer.Text, node ast.Node, enter bool) ast.WalkStatus {
n := node.(*ast.FencedCodeBlock)
if enter {
// Open the block by adding formatting and all.
r.StartBlock()
r.Buffer.WriteString("---\n")
// Create a segment.
seg := CodeblockSegment{
Start: r.Buffer.Len(),
Language: string(n.Language(r.Source)),
}
// Join all lines together.
var lines = n.Lines()
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
r.Buffer.Write(line.Value(r.Source))
}
// Close the segment.
seg.End = r.Buffer.Len()
r.Append(seg)
// Close the block.
r.Buffer.WriteString("\n---")
r.EndBlock()
}
return ast.WalkContinue
}
type CodeblockSegment struct {
empty.TextSegment
Start, End int
Language string
}
var _ text.Codeblocker = (*CodeblockSegment)(nil)
func (c CodeblockSegment) Bounds() (start, end int) {
return c.Start, c.End
}
func (c CodeblockSegment) AsCodeblocker() text.Codeblocker {
return c
}
func (c CodeblockSegment) CodeblockLanguage() string {
return c.Language
}