2020-06-19 01:00:24 +00:00
|
|
|
package segments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/arikawa/discord"
|
|
|
|
"github.com/diamondburned/arikawa/state"
|
2020-10-05 03:45:34 +00:00
|
|
|
"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"
|
|
|
|
"github.com/diamondburned/ningen/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
|
|
|
)
|
|
|
|
|
|
|
|
func ParseMessage(m *discord.Message, s state.Store) text.Rich {
|
2020-07-06 00:18:40 +00:00
|
|
|
var content = []byte(m.Content)
|
|
|
|
var node = md.ParseWithMessage(content, s, m, true)
|
|
|
|
|
2020-10-05 03:45:34 +00:00
|
|
|
r := renderer.New(content, node)
|
2020-07-08 08:35:30 +00:00
|
|
|
// Register the needed states for some renderers.
|
|
|
|
r.WithState(m, s)
|
|
|
|
// Render the main message body.
|
2020-10-05 03:45:34 +00:00
|
|
|
r.Walk(node)
|
2020-07-08 08:35:30 +00:00
|
|
|
// Render the extra bits.
|
2020-10-05 03:45:34 +00:00
|
|
|
embed.RenderAttachments(r, m.Attachments)
|
|
|
|
embed.RenderEmbeds(r, m.Embeds, m, s)
|
2020-07-06 00:18:40 +00:00
|
|
|
|
|
|
|
return text.Rich{
|
|
|
|
Content: r.String(),
|
2020-10-05 03:45:34 +00:00
|
|
|
Segments: r.Segments,
|
2020-07-06 00:18:40 +00:00
|
|
|
}
|
2020-06-19 01:00:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 00:18:40 +00:00
|
|
|
func ParseWithMessage(b []byte, m *discord.Message, s state.Store, msg bool) text.Rich {
|
2020-06-19 01:00:24 +00:00
|
|
|
node := md.ParseWithMessage(b, s, m, msg)
|
2020-10-05 03:45:34 +00:00
|
|
|
return renderer.RenderNode(b, node)
|
2020-07-08 08:35:30 +00:00
|
|
|
}
|