model

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2023 License: AGPL-3.0 Imports: 10 Imported by: 2

Documentation

Overview

Package model implements ProseMirror's document model, along with the mechanisms needed to support schemas.

Index

Constants

This section is empty.

Variables

View Source
var EmptyContentMatch = NewContentMatch(true)

EmptyContentMatch is an empty ContentMatch.

View Source
var EmptyFragment = &Fragment{Content: nil, Size: 0}

EmptyFragment is an empty fragment.

View Source
var EmptySlice = NewSlice(EmptyFragment, 0, 0)

EmptySlice is an empty slice.

View Source
var NoMarks = []*Mark{}

NoMarks is the empty set of marks (none in JS)

Functions

func SameMarkSet

func SameMarkSet(a, b []*Mark) bool

SameMarkSet tests whether two sets of marks are identical.

Types

type Attribute

type Attribute struct {
	HasDefault bool
	Default    interface{}
}

Attribute descriptors

func NewAttribute

func NewAttribute(options *AttributeSpec) *Attribute

NewAttribute is the constructor for Attribute.

type AttributeSpec

type AttributeSpec struct {
	// The default value for this attribute, to use when no explicit value is
	// provided. Attributes that have no default must be provided whenever a
	// node or mark of a type that has them is created.
	Default interface{} `json:"default,omitempty"`
}

AttributeSpec is used to define attributes on nodes or marks.

type ContentMatch

type ContentMatch struct {
	// True when this match state represents a valid end of the node.
	ValidEnd bool
	// contains filtered or unexported fields
}

ContentMatch represents a match state of a node type's content expression, and can be used to find out whether further content matches here, and whether a given position is a valid end of the node.

func NewContentMatch

func NewContentMatch(validEnd bool) *ContentMatch

NewContentMatch is the constructor for ContentMatch.

func ParseContentMatch

func ParseContentMatch(str string, nodeTypes []*NodeType) (*ContentMatch, error)

ParseContentMatch builds a ContentMatch from a string expression.

func (*ContentMatch) FillBefore

func (cm *ContentMatch) FillBefore(after *Fragment, toEnd ...bool) *Fragment

FillBefore tries to match the given fragment, and if that fails, see if it can be made to match by inserting nodes in front of it. When successful, return a fragment of inserted nodes (which may be empty if nothing had to be inserted). When toEnd is true, only return a fragment if the resulting match goes to the end of the content expression.

func (*ContentMatch) MatchFragment

func (cm *ContentMatch) MatchFragment(frag *Fragment, args ...int) *ContentMatch

MatchFragment tries to match a fragment. Returns the resulting match when successful.

:: (Fragment, ?number, ?number) → ?ContentMatch

func (*ContentMatch) MatchType

func (cm *ContentMatch) MatchType(typ *NodeType) *ContentMatch

MatchType matches a node type, returning a match after that node if successful.

type DiffEnd

type DiffEnd struct {
	A int
	B int
}

DiffEnd is the result of findDiffEnd with the positions in both a and b fragments.

type Fragment

type Fragment struct {
	Content []*Node
	// Size is the total of the size of its content nodes.
	Size int
}

Fragment represents a node's collection of child nodes.

Like nodes, fragments are persistent data structures, and you should not mutate them or their content. Rather, you create new instances whenever needed. The API tries to make this easy.

func FragmentFrom

func FragmentFrom(nodes interface{}) (*Fragment, error)

FragmentFrom creates a fragment from something that can be interpreted as a set of nodes. For null, it returns the empty fragment. For a fragment, the fragment itself. For a node or array of nodes, a fragment containing those nodes.

func FragmentFromArray

func FragmentFromArray(array []*Node) *Fragment

FragmentFromArray builds a fragment from an array of nodes. Ensures that adjacent text nodes with the same marks are joined together.

func FragmentFromJSON

func FragmentFromJSON(schema *Schema, value interface{}) (*Fragment, error)

FragmentFromJSON deserializes a fragment from its JSON representation.

func NewFragment

func NewFragment(content []*Node, size ...int) *Fragment

NewFragment is the constructor for Fragment.

func (*Fragment) Append

func (f *Fragment) Append(other *Fragment) *Fragment

Append creates a new fragment containing the combined content of this fragment and the other.

func (*Fragment) Child

func (f *Fragment) Child(index int) (*Node, error)

Child gets the child node at the given index. Raise an error when the index is out of range.

func (*Fragment) ChildCount

func (f *Fragment) ChildCount() int

ChildCount returns the number of child nodes in this fragment.

func (*Fragment) Cut

func (f *Fragment) Cut(from int, to ...int) *Fragment

Cut out the sub-fragment between the two given positions.

func (*Fragment) Eq

func (f *Fragment) Eq(other *Fragment) bool

Eq compares this fragment to another one.

func (*Fragment) FindDiffEnd

func (f *Fragment) FindDiffEnd(other *Fragment, pos ...int) *DiffEnd

FindDiffEnd finds the first position, searching from the end, at which this fragment and the given fragment differ, or `null` if they are the same. Since this position will not be the same in both nodes, an object with two separate positions is returned.

func (*Fragment) FindDiffStart

func (f *Fragment) FindDiffStart(other *Fragment, pos ...int) *int

FindDiffStart finds the first position at which this fragment and another fragment differ, or null if they are the same.

func (*Fragment) FirstChild

func (f *Fragment) FirstChild() *Node

FirstChild returns the first child of the fragment, or null if it is empty.

func (*Fragment) ForEach added in v0.4.0

func (f *Fragment) ForEach(fn func(node *Node, offset, index int))

ForEach calls fn for every child node, passing the node, its offset into this parent node, and its index.

func (*Fragment) LastChild

func (f *Fragment) LastChild() *Node

LastChild returns the last child of the fragment, or null if it is empty.

func (*Fragment) MaybeChild

func (f *Fragment) MaybeChild(index int) *Node

MaybeChild gets the child node at the given index, if it exists.

func (*Fragment) NodesBetween

func (f *Fragment) NodesBetween(from, to int, fn NBCallback, nodeStart int, parent *Node) *int

NodesBetween invokes a callback for all descendant nodes between the given two positions (relative to start of this fragment). Doesn't descend into a node when the callback returns false.

func (*Fragment) ReplaceChild

func (f *Fragment) ReplaceChild(index int, node *Node) *Fragment

ReplaceChild creates a new fragment in which the node at the given index is replaced by the given node.

func (*Fragment) String

func (f *Fragment) String() string

String returns a debugging string that describes this fragment.

func (*Fragment) ToJSON

func (f *Fragment) ToJSON() interface{}

ToJSON creates a JSON-serializeable representation of this fragment.

type Mark

type Mark struct {
	Type  *MarkType
	Attrs map[string]interface{}
}

Mark is a piece of information that can be attached to a node, such as it being emphasized, in code font, or a link. It has a type and optionally a set of attributes that provide further information (such as the target of the link). Marks are created through a Schema, which controls which types exist and which attributes they have.

func MarkFromJSON

func MarkFromJSON(schema *Schema, raw map[string]interface{}) (*Mark, error)

MarkFromJSON deserializes a mark from its JSON representation.

func MarkSetFrom

func MarkSetFrom(marks ...interface{}) []*Mark

MarkSetFrom creates a properly sorted mark set from null, a single mark, or an unsorted array of marks.

func NewMark

func NewMark(typ *MarkType, attrs map[string]interface{}) *Mark

NewMark is the constructor for Mark.

func (*Mark) AddToSet

func (m *Mark) AddToSet(set []*Mark) []*Mark

AddToSet , when given a set of marks, creates a new set which contains this one as well, in the right position. If this mark is already in the set, the set itself is returned. If any marks that are set to be exclusive with this mark are present, those are replaced by this one.

func (*Mark) Eq

func (m *Mark) Eq(other *Mark) bool

Eq tests whether this mark has the same type and attributes as another mark.

func (*Mark) IsInSet

func (m *Mark) IsInSet(set []*Mark) bool

IsInSet tests whether this mark is in the given set of marks.

func (*Mark) RemoveFromSet

func (m *Mark) RemoveFromSet(set []*Mark) []*Mark

RemoveFromSet removes this mark from the given set, returning a new set. If this mark is not in the set, the set itself is returned.

func (*Mark) ToJSON

func (m *Mark) ToJSON() map[string]interface{}

ToJSON converts this mark to a JSON-serializeable representation.

type MarkSpec

type MarkSpec struct {
	// In JavaScript, the MarkSpec are kept in an OrderedMap. In Go, the map
	// doesn't preserve the order of the keys. Instead, an array is used, and
	// the key is kept here.
	Key string `json:"-"`

	// The attributes that marks of this type get.
	Attrs map[string]*AttributeSpec `json:"attrs,omitempty"`

	// Whether this mark should be active when the cursor is positioned
	// at its end (or at its start when that is also the start of the
	// parent node). Defaults to true.
	Inclusive *bool `json:"inclusive,omitempty"`

	// Determines which other marks this mark can coexist with. Should be a
	// space-separated strings naming other marks or groups of marks. When a
	// mark is added to a set, all marks that it excludes are removed in the
	// process. If the set contains any mark that excludes the new mark but is
	// not, itself, excluded by the new mark, the mark can not be added an the
	// set. You can use the value `"_"` to indicate that the mark excludes all
	// marks in the schema.
	//
	// Defaults to only being exclusive with marks of the same type. You can
	// set it to an empty string (or any string not containing the mark's own
	// name) to allow multiple marks of a given type to coexist (as long as
	// they have different attributes).
	Excludes *string `json:"excludes,omitempty"`

	// The group or space-separated groups to which this mark belongs.
	Group string `json:"group,omitempty"`
}

MarkSpec is an object describing a mark type.

type MarkType

type MarkType struct {
	// The name of the mark type.
	Name string
	Rank int
	// The schema that this mark type instance is part of.
	Schema *Schema
	// The spec on which the type is based.
	Spec     *MarkSpec
	Excluded []*MarkType
	Attrs    map[string]*Attribute
	Instance *Mark
}

MarkType is the type object for marks. Like nodes, marks (which are associated with nodes to signify things like emphasis or being part of a link) are tagged with type objects, which are instantiated once per Schema.

func NewMarkType

func NewMarkType(name string, rank int, schema *Schema, spec *MarkSpec) *MarkType

NewMarkType is the constructor for MarkType.

func (*MarkType) Create

func (mt *MarkType) Create(attrs map[string]interface{}) *Mark

Create a mark of this type. attrs may be null or an object containing only some of the mark's attributes. The others, if they have defaults, will be added.

func (*MarkType) Excludes

func (mt *MarkType) Excludes(other *MarkType) bool

Excludes queries whether a given mark type is excluded by this one.

func (*MarkType) IsInSet

func (mt *MarkType) IsInSet(set []*Mark) *Mark

IsInSet tests whether there is a mark of this type in the given set.

type NBCallback

type NBCallback func(*Node, int, *Node, int) bool

NBCallback is a type of the function used for NodesBetween. The arguments are: - the current node - the current position - the parent node - the index of the current node in the list of its parent children. If the callback returns false, it will prevent NodesBetween to descend into this node.

type Node

type Node struct {
	// The type of node that this is.
	Type *NodeType
	// An object mapping attribute names to values. The kind of attributes
	// allowed and required are determined by the node type.
	Attrs map[string]interface{}
	// A container holding the node's children.
	Content *Fragment
	// For text nodes, this contains the node's text content.
	Text *string
	// The marks (things like whether it is emphasized or part of a link)
	// applied to this node.
	Marks []*Mark
}

Node class represents a node in the tree that makes up a ProseMirror document. So a document is an instance of Node, with children that are also instances of Node.

Nodes are persistent data structures. Instead of changing them, you create new ones with the content you want. Old ones keep pointing at the old document shape. This is made cheaper by sharing structure between the old and new data as much as possible, which a tree shape like this (without back pointers) makes easy.

Do not directly mutate the properties of a Node object.

func NewNode

func NewNode(typ *NodeType, attrs map[string]interface{}, content *Fragment, marks []*Mark) *Node

NewNode is the constructor of Node.

func NewTextNode

func NewTextNode(typ *NodeType, attrs map[string]interface{}, text string, marks []*Mark) *Node

NewTextNode is a constructor for text Node.

func NodeFromJSON

func NodeFromJSON(schema *Schema, raw map[string]interface{}) (*Node, error)

NodeFromJSON deserializes a node from its JSON representation.

func (*Node) CanReplace

func (n *Node) CanReplace(from, to int, args ...interface{}) bool

CanReplace tests whether replacing the range between from and to (by child index) with the given replacement fragment (which defaults to the empty fragment) would leave the node's content valid. You can optionally pass start and end indices into the replacement fragment.

:: (number, number, ?Fragment, ?number, ?number) → bool

func (*Node) Child

func (n *Node) Child(index int) (*Node, error)

Child gets the child node at the given index. Raises an error when the index is out of range.

func (*Node) ChildCount

func (n *Node) ChildCount() int

ChildCount returns the number of children that the node has.

func (*Node) ContentMatchAt

func (n *Node) ContentMatchAt(index int) (*ContentMatch, error)

ContentMatchAt gets the content match in this node at the given index.

func (*Node) Copy

func (n *Node) Copy(content ...*Fragment) *Node

Copy creates a new node with the same markup as this node, containing the given content (or empty, if no content is given).

func (*Node) Cut

func (n *Node) Cut(from int, to ...int) *Node

Cut creates a copy of this node with only the content between the given positions. If to is not given, it defaults to the end of the node.

func (*Node) Eq

func (n *Node) Eq(other *Node) bool

Eq tests whether two nodes represent the same piece of document.

func (*Node) FirstChild

func (n *Node) FirstChild() *Node

FirstChild returns this node's first child, or null if there are no children.

func (*Node) ForEach added in v0.4.0

func (n *Node) ForEach(fn func(node *Node, offset, index int))

ForEach calls fn for every child node, passing the node, its offset into this parent node, and its index.

func (*Node) HasMarkup

func (n *Node) HasMarkup(typ *NodeType, args ...interface{}) bool

HasMarkup checks whether this node's markup correspond to the given type, attributes, and marks.

:: (NodeType, ?Object, ?Mark) → bool

func (*Node) IsAtom added in v0.4.12

func (n *Node) IsAtom() bool

IsAtom returns true when this is an atom, i.e. when it does not have directly editable content. This is usually the same as `isLeaf`, but can be configured with the [`atom` property](#model.NodeSpec.atom) on a node's spec (typically used when the node is displayed as an uneditable [node view](#view.NodeView)).

func (*Node) IsBlock

func (n *Node) IsBlock() bool

IsBlock returns true when this is a block (non-inline node)

func (*Node) IsInline

func (n *Node) IsInline() bool

IsInline returns true when this is an inline node (a text node or a node that can appear among text).

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

IsLeaf returns true when this is a leaf node.

func (*Node) IsText

func (n *Node) IsText() bool

IsText returns true when this is a text node.

func (*Node) LastChild

func (n *Node) LastChild() *Node

LastChild returns this node's last child, or null if there are no children.

func (*Node) Mark

func (n *Node) Mark(marks []*Mark) *Node

Mark creates a copy of this node, with the given set of marks instead of the node's own marks.

func (*Node) MaybeChild

func (n *Node) MaybeChild(index int) *Node

MaybeChild gets the child node at the given index, if it exists.

func (*Node) NodeAt

func (n *Node) NodeAt(pos int) *Node

NodeAt finds the node directly after the given position.

func (*Node) NodeSize

func (n *Node) NodeSize() int

NodeSize returns the size of this node, as defined by the integer-based indexing scheme. For text nodes, this is the amount of characters. For other leaf nodes, it is one. For non-leaf nodes, it is the size of the content plus two (the start and end token).

func (*Node) NodesBetween

func (n *Node) NodesBetween(from, to int, fn NBCallback, startPos ...int)

NodesBetween invokes a callback for all descendant nodes recursively between the given two positions that are relative to start of this node's content. The callback is invoked with the node, its parent-relative position, its parent node, and its child index. When the callback returns false for a given node, that node's children will not be recursed over. The last parameter can be used to specify a starting position to count from.

func (*Node) Replace

func (n *Node) Replace(from, to int, slice *Slice) (*Node, error)

Replace the part of the document between the given positions with the given slice. The slice must 'fit', meaning its open sides must be able to connect to the surrounding content, and its content nodes must be valid children for the node they are placed into. If any of this is violated, an error of type ReplaceError is thrown.

func (*Node) Resolve

func (n *Node) Resolve(pos int) (*ResolvedPos, error)

Resolve the given position in the document, returning an object with information about its context.

func (*Node) SameMarkup

func (n *Node) SameMarkup(other *Node) bool

SameMarkup compares the markup (type, attributes, and marks) of this node to those of another. Returns true if both have the same markup.

func (*Node) Slice

func (n *Node) Slice(from int, args ...interface{}) (*Slice, error)

Slice cuts out the part of the document between the given positions, and return it as a Slice object.

func (*Node) String

func (n *Node) String() string

String returns a string representation of this node for debugging purposes.

func (*Node) TextBetween

func (n *Node) TextBetween(from, to int, args ...string) string

TextBetween gets all text between positions from and to. When blockSeparator is given, it will be inserted whenever a new block node is started. When leafText is given, it'll be inserted for every non-text leaf node encountered.

func (*Node) TextContent

func (n *Node) TextContent() string

TextContent concatenates all the text nodes found in this fragment and its children.

func (*Node) ToJSON

func (n *Node) ToJSON() map[string]interface{}

ToJSON converts this node to a JSON-serializeable representation.

func (*Node) UnitCodeAt added in v0.4.5

func (n *Node) UnitCodeAt(pos int) uint16

UnitCodeAt returns the UTF-16 unit code at the given position. It is a function that does not exist in the original prosemirror in JS, as it is only useful in Go to emulate the behavior of strings in JavaScript. In Go, the strings are encoded as UTF-8 by default, but in JavaScript, they can be seen as an array of UTF-16 unit codes. In particular, it means that a single unicode character with a value > 0xffff are a surrogate pair of two UTF-16 unit codes in JavaScript.

func (*Node) WithText added in v0.4.0

func (n *Node) WithText(text string) *Node

WithText returns a new text node with the given string.

type NodeSpec

type NodeSpec struct {
	// In JavaScript, the NodeSpec are kept in an OrderedMap. In Go, the map
	// doesn't preserve the order of the keys. Instead, an array is used, and
	// the key is kept here.
	Key string `json:"-"`

	// The content expression for this node, as described in the schema guide.
	// When not given, the node does not allow any content.
	Content string `json:"content,omitempty"`

	// The marks that are allowed inside of this node. May be a space-separated
	// string referring to mark names or groups, "_" to explicitly allow all
	// marks, or "" to disallow marks. When not given, nodes with inline
	// content default to allowing all marks, other nodes default to not
	// allowing marks.
	Marks *string `json:"marks,omitempty"`

	// The group or space-separated groups to which this node belongs, which
	// can be referred to in the content expressions for the schema.
	Group string `json:"group,omitempty"`

	// Should be set to true for inline nodes. (Implied for text nodes.)
	Inline bool `json:"inline,omitempty"`

	// Can be set to true to indicate that, though this isn't a [leaf
	// node](#model.NodeType.isLeaf), it doesn't have directly editable
	// content and should be treated as a single unit in the view.
	Atom bool `json:"atom,omitempty"`

	// The attributes that nodes of this type get.
	Attrs map[string]*AttributeSpec `json:"attrs,omitempty"`

	// Defines the default way a node of this type should be serialized to a
	// string representation for debugging (e.g. in error messages).
	ToDebugString func(*Node) string `json:"-"`
}

NodeSpec is an object describing a node type.

type NodeType

type NodeType struct {
	// The name the node type has in this schema.
	Name string
	// A link back to the `Schema` the node type belongs to.
	Schema *Schema
	// The spec that this type is based on
	Spec         *NodeSpec
	Groups       []string
	Attrs        map[string]*Attribute
	DefaultAttrs map[string]interface{}
	// The starting match of the node type's content expression.
	ContentMatch *ContentMatch
	// The set of marks allowed in this node. `null` means all marks are
	// allowed.
	MarkSet *[]*MarkType
	// True if this node type has inline content.
	InlineContent bool
}

NodeType are objects allocated once per Schema and used to tag Node instances. They contain information about the node type, such as its name and what kind of node it represents.

func NewNodeType

func NewNodeType(name string, schema *Schema, spec *NodeSpec) *NodeType

NewNodeType is the constructor for NodeType.

func (*NodeType) AllowsMarkType

func (nt *NodeType) AllowsMarkType(markType *MarkType) bool

AllowsMarkType checks whether the given mark type is allowed in this node.

func (*NodeType) AllowsMarks

func (nt *NodeType) AllowsMarks(marks []*Mark) bool

AllowsMarks tests whether the given set of marks are allowed in this node.

func (*NodeType) Create

func (nt *NodeType) Create(attrs map[string]interface{}, content interface{}, marks []*Mark) (*Node, error)

Create a Node of this type. The given attributes are checked and defaulted (you can pass null to use the type's defaults entirely, if no required attributes exist). content may be a Fragment, a node, an array of nodes, or null. Similarly marks may be null to default to the empty set of marks.

func (*NodeType) CreateAndFill

func (nt *NodeType) CreateAndFill(args ...interface{}) (*Node, error)

CreateAndFill is like create, but see if it is necessary to add nodes to the start or end of the given fragment to make it fit the node. If no fitting wrapping can be found, return null. Note that, due to the fact that required nodes can always be created, this will always succeed if you pass null or Fragment.empty as content.

:: (?Object, ?union<Fragment, Node, [Node]>, ?Mark) → ?Node

func (*NodeType) CreateChecked

func (nt *NodeType) CreateChecked(args ...interface{}) (*Node, error)

CreateChecked is like create, but check the given content against the node type's content restrictions, and throw an error if it doesn't match.

:: (?Object, ?union<Fragment, Node, [Node]>, ?Mark) → Node

func (*NodeType) HasRequiredAttrs added in v0.4.6

func (nt *NodeType) HasRequiredAttrs() bool

HasRequiredAttrs tells you whether this node type has any required attributes.

func (*NodeType) IsAtom added in v0.4.12

func (nt *NodeType) IsAtom() bool

IsAtom returns true when this node is an atom, i.e. when it does not have directly editable content.

func (*NodeType) IsBlock

func (nt *NodeType) IsBlock() bool

IsBlock returns true if this is a block type.

func (*NodeType) IsInline

func (nt *NodeType) IsInline() bool

IsInline returns true if this is an inline type.

func (*NodeType) IsLeaf

func (nt *NodeType) IsLeaf() bool

IsLeaf returns true for node types that allow no content.

func (*NodeType) IsText

func (nt *NodeType) IsText() bool

IsText returns true if this is the text node type.

func (*NodeType) ValidContent

func (nt *NodeType) ValidContent(content *Fragment) bool

ValidContent returns true if the given fragment is valid content for this node type with the given attributes.

type ReplaceError

type ReplaceError struct {
	Message string
}

ReplaceError is the error type raised by Node.replace when given an invalid replacement.

func NewReplaceError

func NewReplaceError(message string, args ...interface{}) *ReplaceError

NewReplaceError is the constructor for ReplaceError.

func (*ReplaceError) Error

func (e *ReplaceError) Error() string

Error returns the error message.

type ResolvedPos

type ResolvedPos struct {
	// The position that was resolved.
	Pos  int
	Path []interface{}
	// The number of levels the parent node is from the root. If this
	// position points directly into the root node, it is 0. If it
	// points into a top-level paragraph, 1, and so on.
	Depth int
	// The offset this position has into its parent node.
	ParentOffset int
}

ResolvedPos means resolved position. You can resolve a position to get more information about it. Objects of this class represent such a resolved position, providing various pieces of context information, and some helper methods.

Throughout this interface, methods that take an optional depth parameter will interpret undefined as this.depth and negative numbers as this.depth + value.

func NewResolvedPos

func NewResolvedPos(pos int, path []interface{}, parentOffset int) *ResolvedPos

NewResolvedPos is the constructor of ResolvedPos.

func (*ResolvedPos) After

func (r *ResolvedPos) After(depth ...int) (int, error)

After is the (absolute) position directly after the wrapping node at the given level, or the original position when depth is this.depth + 1.

func (*ResolvedPos) Before

func (r *ResolvedPos) Before(depth ...int) (int, error)

Before is the (absolute) position directly before the wrapping node at the given level, or, when depth is this.depth + 1, the original position.

func (*ResolvedPos) Doc

func (r *ResolvedPos) Doc() *Node

Doc is the root node in which the position was resolved.

func (*ResolvedPos) End

func (r *ResolvedPos) End(depth ...int) int

End is the (absolute) position at the end of the node at the given level.

func (*ResolvedPos) Index

func (r *ResolvedPos) Index(depth ...int) int

Index returns the index into the ancestor at the given level. If this points at the 3rd node in the 2nd paragraph on the top level, for example, p.index(0) is 1 and p.index(1) is 2.

func (*ResolvedPos) IndexAfter

func (r *ResolvedPos) IndexAfter(depth ...int) int

IndexAfter returns the index pointing after this position into the ancestor at the given level.

func (*ResolvedPos) Marks

func (r *ResolvedPos) Marks() []*Mark

Marks gets the marks at this position, factoring in the surrounding marks' inclusive property. If the position is at the start of a non-empty node, the marks of the node after it (if any) are returned.

func (*ResolvedPos) Node

func (r *ResolvedPos) Node(depth ...int) *Node

Node returns the ancestor node at the given level. p.node(p.depth) is the same as p.parent.

func (*ResolvedPos) NodeAfter

func (r *ResolvedPos) NodeAfter() (*Node, error)

NodeAfter gets the node directly after the position, if any. If the position points into a text node, only the part of that node after the position is returned.

func (*ResolvedPos) NodeBefore

func (r *ResolvedPos) NodeBefore() (*Node, error)

NodeBefore gets the node directly before the position, if any. If the position points into a text node, only the part of that node before the position is returned.

func (*ResolvedPos) Parent

func (r *ResolvedPos) Parent() *Node

Parent returns the parent node that the position points into. Note that even if a position points into a text node, that node is not considered the parent—text nodes are ‘flat’ in this model, and have no content.

func (*ResolvedPos) SharedDepth

func (r *ResolvedPos) SharedDepth(pos int) int

SharedDepth is the depth up to which this position and the given (non-resolved) position share the same parent nodes.

func (*ResolvedPos) Start

func (r *ResolvedPos) Start(depth ...int) int

Start is the (absolute) position at the start of the node at the given level.

func (*ResolvedPos) TextOffset

func (r *ResolvedPos) TextOffset() int

TextOffset returns, when this position points into a text node, the distance between the position and the start of the text node. Will be zero for positions that point between nodes.

type Schema

type Schema struct {
	// The spec on which the schema is based.
	Spec *SchemaSpec

	// An object mapping the schema's node names to node type objects.
	Nodes []*NodeType

	// A map from mark names to mark type objects.
	Marks []*MarkType
}

Schema is a a document schema: it holds node and mark type objects for the nodes and marks that may occur in conforming documents, and provides functionality for creating and deserializing such documents.

func NewSchema

func NewSchema(spec *SchemaSpec) (*Schema, error)

NewSchema constructs a schema from a schema specification.

func (*Schema) Mark

func (s *Schema) Mark(typ interface{}, args ...map[string]interface{}) *Mark

Mark creates a mark with the given type and attributes.

func (*Schema) MarkFromJSON

func (s *Schema) MarkFromJSON(raw []byte) (*Mark, error)

MarkFromJSON deserializes a mark from its JSON representation.

func (*Schema) MarkType added in v0.2.0

func (s *Schema) MarkType(name string) (*MarkType, error)

MarkType returns the MarkType with the given name in this schema.

func (*Schema) Node

func (s *Schema) Node(typ interface{}, args ...interface{}) (*Node, error)

Node creates a node in this schema. The type may be a string or a NodeType instance. Attributes will be extended with defaults, content may be a Fragment, null, a Node, or an array of nodes.

:: (union<string, NodeType>, ?Object, ?union<Fragment, Node, [Node]>, ?Mark) → Node

func (*Schema) NodeFromJSON

func (s *Schema) NodeFromJSON(raw []byte) (*Node, error)

NodeFromJSON deserializes a node from its JSON representation.

func (*Schema) NodeType added in v0.2.0

func (s *Schema) NodeType(name string) (*NodeType, error)

NodeType returns the NodeType with the given name in this schema.

func (*Schema) Text

func (s *Schema) Text(text string, marks ...[]*Mark) *Node

Text creates a text node in the schema. Empty text nodes are not allowed.

type SchemaSpec

type SchemaSpec struct {
	// The node types in this schema. Maps names to NodeSpec objects that
	// describe the node type associated with that name. Their order is
	// significant—it determines which parse rules take precedence by default,
	// and which nodes come first in a given group.
	Nodes []*NodeSpec

	// The mark types that exist in this schema. The order in which they are
	// provided determines the order in which mark sets are sorted and in which
	// parse rules are tried.
	Marks []*MarkSpec

	// The name of the default top-level node for the schema. Defaults to "doc".
	TopNode string
}

SchemaSpec is an object describing a schema, as passed to the Schema constructor.

func SchemaSpecFromJSON added in v0.3.0

func SchemaSpecFromJSON(raw map[string]interface{}) SchemaSpec

SchemaSpecFromJSON returns a SchemaSpec from a JSON representation.

func (SchemaSpec) MarshalJSON

func (s SchemaSpec) MarshalJSON() ([]byte, error)

MarshalJSON creates a JSON representation of the SchemaSpec.

func (*SchemaSpec) UnmarshalJSON

func (s *SchemaSpec) UnmarshalJSON(buf []byte) error

UnmarshalJSON parses a JSON representation of a SchemaSpec.

type Slice

type Slice struct {
	// Fragment The slice's content.
	Content *Fragment
	// The open depth at the start.
	OpenStart int
	// number The open depth at the end.
	OpenEnd int
}

Slice represents a piece cut out of a larger document. It stores not only a fragment, but also the depth up to which nodes on both side are ‘open’ (cut through).

func NewSlice

func NewSlice(content *Fragment, openStart, openEnd int) *Slice

NewSlice creates a slice. When specifying a non-zero open depth, you must make sure that there are nodes of at least that depth at the appropriate side of the fragment—i.e. if the fragment is an empty paragraph node, openStart and openEnd can't be greater than 1.

It is not necessary for the content of open nodes to conform to the schema's content constraints, though it should be a valid start/end/middle for such a node, depending on which sides are open.

func SliceFromJSON added in v0.2.0

func SliceFromJSON(schema *Schema, obj interface{}) (*Slice, error)

SliceFromJSON deserializes a slice from its JSON representation.

func (*Slice) Eq

func (s *Slice) Eq(other *Slice) bool

Eq tests whether this slice is equal to another slice.

func (*Slice) InsertAt

func (s *Slice) InsertAt(pos int, fragment *Fragment) *Slice

InsertAt inserts a fragment at the given position.

func (*Slice) RemoveBetween

func (s *Slice) RemoveBetween(from, to int) (*Slice, error)

RemoveBetween removes content between the two given positions.

func (*Slice) Size

func (s *Slice) Size() int

Size returns the size this slice would add when inserted into a document.

func (*Slice) String

func (s *Slice) String() string

String returns a string representation of this slice.

func (*Slice) ToJSON added in v0.2.0

func (s *Slice) ToJSON() interface{}

ToJSON converts a slice to a JSON-serializable representation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL