Created
March 15, 2016 23:05
-
-
Save nathanborror/653175f60f1d4a80199c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Markup (based on Draft.js: http://facebook.github.io/draft-js) | |
package draft | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
type Markup struct { | |
Blocks []Block `json:"blocks"` | |
EntityMap map[string]Entity `json:"entityMap"` | |
} | |
type Block struct { | |
Key string `json:"key"` | |
Text string `json:"text"` | |
Type string `json:"type"` | |
Depth int `json:"depth"` | |
StyleRanges []StyleRange `json:"inlineStyleRanges"` | |
EntityRanges []EntityRange `json:"entityRanges"` | |
} | |
type Entity struct { | |
Type string `json:"type"` | |
Mutability string `json:"mutability"` | |
Data interface{} `json:"data"` | |
} | |
type EntityRange struct { | |
Key int `json:"key"` | |
Offset int `json:"offset"` | |
Length int `json:"length"` | |
Style string `json:"style"` | |
} | |
type StyleRange struct { | |
Offset int `json:"offset"` | |
Length int `json:"length"` | |
Style string `json:"style"` | |
} | |
func New(raw string) (*Markup, error) { | |
if len(raw) == 0 { | |
return nil, nil | |
} | |
var m Markup | |
if err := json.Unmarshal([]byte(raw), &m); err != nil { | |
return nil, fmt.Errorf("Invalid markup: %s", err) | |
} | |
return &m, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment