1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-01 04:14:21 +00:00
cchat-discord/internal/segments/md.go

55 lines
1.9 KiB
Go
Raw Normal View History

2020-06-19 01:00:24 +00:00
package segments
import (
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/state/store"
"github.com/diamondburned/cchat-discord/internal/segments/embed"
"github.com/diamondburned/cchat-discord/internal/segments/renderer"
2020-06-19 01:00:24 +00:00
"github.com/diamondburned/cchat/text"
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/ningen/v2/md"
2020-11-06 06:14:52 +00:00
_ "github.com/diamondburned/cchat-discord/internal/segments/blockquote"
_ "github.com/diamondburned/cchat-discord/internal/segments/codeblock"
_ "github.com/diamondburned/cchat-discord/internal/segments/colored"
_ "github.com/diamondburned/cchat-discord/internal/segments/emoji"
_ "github.com/diamondburned/cchat-discord/internal/segments/inline"
_ "github.com/diamondburned/cchat-discord/internal/segments/link"
_ "github.com/diamondburned/cchat-discord/internal/segments/mention"
2020-06-19 01:00:24 +00:00
)
2020-12-20 05:44:26 +00:00
func ParseMessage(m *discord.Message, s store.Cabinet) text.Rich {
2020-12-31 02:58:36 +00:00
var rich text.Rich
ParseMessageRich(&rich, m, s)
return rich
}
func ParseMessageRich(rich *text.Rich, m *discord.Message, s store.Cabinet) {
var content = []byte(m.Content)
var node = md.ParseWithMessage(content, s, m, true)
r := renderer.New(content, node)
2020-12-31 02:58:36 +00:00
r.Buffer.Grow(len(rich.Content))
r.Buffer.WriteString(rich.Content)
// Register the needed states for some renderers.
r.WithState(m, s)
// Render the main message body.
r.Walk(node)
// Render the extra bits.
embed.RenderAttachments(r, m.Attachments)
embed.RenderEmbeds(r, m.Embeds, m, s)
2020-12-31 02:58:36 +00:00
rich.Content = r.String()
rich.Segments = append(rich.Segments, r.Segments...)
2020-06-19 01:00:24 +00:00
}
2020-12-20 05:44:26 +00:00
func ParseWithMessage(b []byte, m *discord.Message, s store.Cabinet, msg bool) text.Rich {
2020-06-19 01:00:24 +00:00
node := md.ParseWithMessage(b, s, m, msg)
return renderer.RenderNode(b, node)
}
2020-12-31 02:58:36 +00:00
func ParseWithMessageRich(b []byte, m *discord.Message, s store.Cabinet, msg bool) text.Rich {
node := md.ParseWithMessage(b, s, m, msg)
return renderer.RenderNode(b, node)
}