-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.go
59 lines (47 loc) · 1.19 KB
/
atom.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
49
50
51
52
53
54
55
56
57
58
59
package mobiledoc
import (
"encoding/json"
"errors"
"fmt"
)
// Atom renders an Atom
type Atom func(value string, payload interface{}) string
type atom struct {
name string
value string
payload interface{}
}
// UnmarshalJSON decodes the Atom JSON
func (a *atom) UnmarshalJSON(b []byte) error {
var tmp []json.RawMessage
err := json.Unmarshal(b, &tmp)
if err != nil {
return fmt.Errorf("unable to unmarshal atom: %w", err)
}
if len(tmp) != 3 {
return errors.New("atom too short")
}
err = json.Unmarshal(tmp[0], &a.name)
if err != nil {
return fmt.Errorf("unable to unmarshal atom: %w", err)
}
err = json.Unmarshal(tmp[1], &a.value)
if err != nil {
return fmt.Errorf("unable to unmarshal atom: %w", err)
}
err = json.Unmarshal(tmp[2], &a.payload)
if err != nil {
return fmt.Errorf("unable to unmarshal atom: %w", err)
}
return nil
}
func (md *Mobiledoc) renderAtom(a *atom) (*node, error) {
if md.atoms == nil {
return nil, fmt.Errorf("unable to locate renderer for atom %q", a.name)
}
renderer, ok := md.atoms[a.name]
if !ok {
return nil, fmt.Errorf("unable to locate renderer for atom %q", a.name)
}
return newNode("", renderer(a.value, a.payload)), nil
}