memos/plugin/gomark/ast/inline.go

180 lines
2.5 KiB
Go
Raw Normal View History

2023-12-12 23:24:02 +08:00
package ast
2023-12-28 22:35:39 +08:00
import "fmt"
2023-12-13 21:00:13 +08:00
type BaseInline struct {
2023-12-13 23:50:05 +08:00
BaseNode
2023-12-13 21:00:13 +08:00
}
2023-12-12 23:24:02 +08:00
type Text struct {
BaseInline
Content string
}
func (*Text) Type() NodeType {
2023-12-14 22:21:23 +08:00
return TextNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Text) Restore() string {
return n.Content
}
2023-12-12 23:24:02 +08:00
type Bold struct {
BaseInline
2023-12-13 23:50:05 +08:00
// Symbol is "*" or "_".
2023-12-16 10:09:20 +08:00
Symbol string
Children []Node
2023-12-12 23:24:02 +08:00
}
func (*Bold) Type() NodeType {
2023-12-14 22:21:23 +08:00
return BoldNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Bold) Restore() string {
symbol := n.Symbol + n.Symbol
children := ""
for _, child := range n.Children {
children += child.Restore()
}
return fmt.Sprintf("%s%s%s", symbol, children, symbol)
}
2023-12-13 21:00:13 +08:00
type Italic struct {
BaseInline
2023-12-13 23:50:05 +08:00
// Symbol is "*" or "_".
2023-12-13 21:00:13 +08:00
Symbol string
Content string
}
func (*Italic) Type() NodeType {
2023-12-14 22:21:23 +08:00
return ItalicNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Italic) Restore() string {
return fmt.Sprintf("%s%s%s", n.Symbol, n.Content, n.Symbol)
}
2023-12-13 21:00:13 +08:00
type BoldItalic struct {
BaseInline
2023-12-13 23:50:05 +08:00
// Symbol is "*" or "_".
2023-12-13 21:00:13 +08:00
Symbol string
Content string
}
func (*BoldItalic) Type() NodeType {
2023-12-14 22:21:23 +08:00
return BoldItalicNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *BoldItalic) Restore() string {
symbol := n.Symbol + n.Symbol + n.Symbol
return fmt.Sprintf("%s%s%s", symbol, n.Content, symbol)
}
2023-12-13 09:06:47 +08:00
type Code struct {
BaseInline
Content string
}
func (*Code) Type() NodeType {
2023-12-14 22:21:23 +08:00
return CodeNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Code) Restore() string {
return fmt.Sprintf("`%s`", n.Content)
}
2023-12-13 09:06:47 +08:00
type Image struct {
BaseInline
AltText string
URL string
}
func (*Image) Type() NodeType {
2023-12-14 22:21:23 +08:00
return ImageNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Image) Restore() string {
return fmt.Sprintf("![%s](%s)", n.AltText, n.URL)
}
2023-12-13 09:06:47 +08:00
type Link struct {
BaseInline
Text string
URL string
}
func (*Link) Type() NodeType {
2023-12-14 22:21:23 +08:00
return LinkNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Link) Restore() string {
return fmt.Sprintf("[%s](%s)", n.Text, n.URL)
}
2023-12-27 08:44:51 +08:00
type AutoLink struct {
BaseInline
2023-12-28 22:35:39 +08:00
URL string
IsRawText bool
2023-12-27 08:44:51 +08:00
}
func (*AutoLink) Type() NodeType {
return AutoLinkNode
}
2023-12-28 22:35:39 +08:00
func (n *AutoLink) Restore() string {
if n.IsRawText {
return n.URL
}
return fmt.Sprintf("<%s>", n.URL)
}
2023-12-13 21:00:13 +08:00
type Tag struct {
2023-12-13 09:06:47 +08:00
BaseInline
Content string
}
2023-12-13 21:00:13 +08:00
func (*Tag) Type() NodeType {
2023-12-14 22:21:23 +08:00
return TagNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Tag) Restore() string {
return fmt.Sprintf("<%s>", n.Content)
}
2023-12-13 21:00:13 +08:00
type Strikethrough struct {
2023-12-13 09:06:47 +08:00
BaseInline
Content string
}
2023-12-13 21:00:13 +08:00
func (*Strikethrough) Type() NodeType {
2023-12-14 22:21:23 +08:00
return StrikethroughNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Strikethrough) Restore() string {
return fmt.Sprintf("~~%s~~", n.Content)
}
type EscapingCharacter struct {
BaseInline
Symbol string
}
func (*EscapingCharacter) Type() NodeType {
return EscapingCharacterNode
}
2023-12-28 22:35:39 +08:00
func (n *EscapingCharacter) Restore() string {
return fmt.Sprintf("\\%s", n.Symbol)
}