Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Created March 15, 2016 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanborror/653175f60f1d4a80199c to your computer and use it in GitHub Desktop.
Save nathanborror/653175f60f1d4a80199c to your computer and use it in GitHub Desktop.
// 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