Markdown parser
This commit is contained in:
parent
a8ecb13367
commit
fc0b9a03eb
5 changed files with 45 additions and 10 deletions
20
discordmd/discordmd.go
Normal file
20
discordmd/discordmd.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package discordmd
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
boldRegex = regexp.MustCompile(`(?ms)\*\*(.*?)\*\*`)
|
||||
italicRegex = regexp.MustCompile(`(?ms)\*(.*?)\*`)
|
||||
underlineRegex = regexp.MustCompile(`(?ms)__(.*?)__`)
|
||||
strikeThroughRegex = regexp.MustCompile(`(?ms)~~(.*?)~~`)
|
||||
)
|
||||
|
||||
func Parse(input string) string {
|
||||
input = boldRegex.ReplaceAllString(input, "[::b]$1[::-]")
|
||||
input = italicRegex.ReplaceAllString(input, "[::i]$1[::-]")
|
||||
input = underlineRegex.ReplaceAllString(input, "[::u]$1[::-]")
|
||||
input = strikeThroughRegex.ReplaceAllString(input, "[::s]$1[::-]")
|
||||
return input
|
||||
}
|
||||
14
discordmd/discordmd_test.go
Normal file
14
discordmd/discordmd_test.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package discordmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
const input = `**Lorem** ipsum dolor sit amet, consectetur adipiscing __elit.__ Nullam ante magna, luctus in ~~molestie non, elementum sit~~ amet tortor. Nunc euismod urna ac massa dictum ultrices. Donec tempor __dignissim__ ullamcorper. Mauris ultricies, risus non malesuada consectetur, *purus leo interdum purus*, nec vestibulum lacus neque non nulla.`
|
||||
const want = `[::b]Lorem[::-] ipsum dolor sit amet, consectetur adipiscing [::u]elit.[::-] Nullam ante magna, luctus in [::s]molestie non, elementum sit[::-] amet tortor. Nunc euismod urna ac massa dictum ultrices. Donec tempor [::u]dignissim[::-] ullamcorper. Mauris ultricies, risus non malesuada consectetur, [::i]purus leo interdum purus[::-], nec vestibulum lacus neque non nulla.`
|
||||
|
||||
if got := Parse(input); got != want {
|
||||
t.Errorf("got %s; want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ func newGuildsTree() *GuildsTree {
|
|||
return gt
|
||||
}
|
||||
|
||||
func (gt *GuildsTree) newGuildFromID(n *tview.TreeNode, gid discord.GuildID) error {
|
||||
func (gt *GuildsTree) createGuildNodeFromID(n *tview.TreeNode, gid discord.GuildID) error {
|
||||
g, err := discordState.Cabinet.Guild(gid)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -54,7 +54,7 @@ func (gt *GuildsTree) newGuildFromID(n *tview.TreeNode, gid discord.GuildID) err
|
|||
return nil
|
||||
}
|
||||
|
||||
func (gt *GuildsTree) newChannel(n *tview.TreeNode, c discord.Channel) {
|
||||
func (gt *GuildsTree) createChannelNode(n *tview.TreeNode, c discord.Channel) {
|
||||
cn := tview.NewTreeNode(gt.channelToString(c))
|
||||
cn.SetReference(c.ID)
|
||||
n.AddChild(cn)
|
||||
|
|
@ -121,7 +121,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
// Orphan (top-level) channels
|
||||
for _, c := range cs {
|
||||
if c.Type != discord.GuildCategory && !c.ParentID.IsValid() {
|
||||
gt.newChannel(n, c)
|
||||
gt.createChannelNode(n, c)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -131,12 +131,12 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
if c.Type == discord.GuildCategory {
|
||||
for _, nestedChannel := range cs {
|
||||
if nestedChannel.ParentID == c.ID {
|
||||
gt.newChannel(n, c)
|
||||
gt.createChannelNode(n, c)
|
||||
continue CATEGORY
|
||||
}
|
||||
}
|
||||
|
||||
gt.newChannel(n, c)
|
||||
gt.createChannelNode(n, c)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -154,7 +154,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
})
|
||||
|
||||
if parent != nil {
|
||||
gt.newChannel(parent, c)
|
||||
gt.createChannelNode(parent, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -196,7 +196,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
|
|||
})
|
||||
|
||||
for _, c := range cs {
|
||||
gt.newChannel(n, c)
|
||||
gt.createChannelNode(n, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/ayn2op/discordo/discordmd"
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
|
|
@ -92,7 +93,7 @@ func (mt *MessagesText) newTimestamp(m *discord.Message) {
|
|||
}
|
||||
|
||||
func (mt *MessagesText) newContent(m *discord.Message) {
|
||||
fmt.Fprint(mt, tview.Escape(m.Content))
|
||||
fmt.Fprint(mt, discordmd.Parse(tview.Escape(m.Content)))
|
||||
}
|
||||
|
||||
func (mt *MessagesText) newAttachments(m *discord.Message) {
|
||||
|
|
|
|||
4
state.go
4
state.go
|
|
@ -42,7 +42,7 @@ func (s *State) onReady(r *gateway.ReadyEvent) {
|
|||
for _, gf := range r.UserSettings.GuildFolders {
|
||||
/// If the ID of the guild folder is zero, the guild folder only contains single guild.
|
||||
if gf.ID == 0 {
|
||||
if err := guildsTree.newGuildFromID(guildsTree.root, gf.GuildIDs[0]); err != nil {
|
||||
if err := guildsTree.createGuildNodeFromID(guildsTree.root, gf.GuildIDs[0]); err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ func (s *State) onReady(r *gateway.ReadyEvent) {
|
|||
guildsTree.root.AddChild(gfNode)
|
||||
|
||||
for _, gid := range gf.GuildIDs {
|
||||
if err := guildsTree.newGuildFromID(gfNode, gid); err != nil {
|
||||
if err := guildsTree.createGuildNodeFromID(gfNode, gid); err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue