-
Notifications
You must be signed in to change notification settings - Fork 108
/
richtext.go
48 lines (37 loc) · 1.01 KB
/
richtext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: MIT
package doc
import "encoding/xml"
// 富文本可用的类型
const (
RichtextTypeHTML = "html"
RichtextTypeMarkdown = "markdown"
)
// Richtext 富文本内容
type Richtext struct {
Type string `xml:"type,attr,omitempty"` // 文档类型,可以是 html 或是 markdown
Text string `xml:",cdata"`
}
type shadowRichtext Richtext
// MarshalXML xml.Marshaler
func (text Richtext) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if text.Text == "" {
return nil
}
if text.Type == "" {
text.Type = RichtextTypeMarkdown
}
shadow := shadowRichtext(text)
return e.EncodeElement(shadow, start)
}
// UnmarshalXML 实现 xml.Unmarshaler
func (text *Richtext) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
field := "/" + start.Name.Local
shadow := (*shadowRichtext)(text)
if err := d.DecodeElement(shadow, &start); err != nil {
return fixedSyntaxError(err, "", field, 0)
}
if shadow.Type == "" {
shadow.Type = RichtextTypeMarkdown
}
return nil
}