prosemirror

package module
v0.0.0-...-32d4dcf Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 10 Imported by: 0

README

Prosemirror

This is a go implementation of the prosemirror document model.

It tries to map closely to the original source code.

The examples directory is a set of examples providing ways to get easy test data out of the original prosemirror.

We want to be 100% compliant at least on document and transformation aspects.

Usage

go get -u github.com/karitham/prosemirror

Then create a Schema and use it to build your document.

Due to how the original prosemirror implementation handles transforms and how go deals with unmarshalling, you need to globally register your marshallers for schemas. This means this library cannot handle using schemas with the same objects but different unmarshalling policies.

This might be possible since we use github.com/go-json-experiment/json to handle the marshalling.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must[T any](v T, err error) T

func RegisterSchema

func RegisterSchema(s Schema)

RegisterSchema registers a schema in the global store. It is used for unmarshalling Nodes and Marks.

Types

type Attribute

type Attribute struct {
	Default any
}

type Attrs

type Attrs map[string]Attribute

type ContentMatch

type ContentMatch struct {
	Next     []contentMatchNode
	ValidEnd bool
}

func EmptyContentMatch

func EmptyContentMatch() *ContentMatch

func (ContentMatch) Empty

func (c ContentMatch) Empty() bool

func (ContentMatch) Format

func (c ContentMatch) Format(f fmt.State, r rune)

func (*ContentMatch) InlineContent

func (c *ContentMatch) InlineContent() bool

func (ContentMatch) String

func (c ContentMatch) String() string

type Edge

type Edge struct {
	Term *NodeType
	To   *int
}

type Expr

type Expr struct {
	Type string
	// for `choice` and `seq`
	Exprs []Expr

	Value NodeType

	// for `choice`, `seq`, `+`, `*`, `range` and `?`
	Expr *Expr

	// for `+`, `range` as well as `*` and `?`
	Min int
	Max int
}

TODO: handle `seq` properly

type Fragment

type Fragment struct {
	Size    int
	Content []Node
}

Fragment represents a node's collection of child nodes its represented as an array of nodes in JSON, but we cache the size of the fragment for operational reasons.

func NewFragment

func NewFragment(nodes ...Node) Fragment

func (Fragment) Child

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

func (Fragment) ChildCount

func (f Fragment) ChildCount() int

func (Fragment) IsZero

func (f Fragment) IsZero() bool

func (Fragment) MarshalJSON

func (f Fragment) MarshalJSON() ([]byte, error)

func (Fragment) String

func (f Fragment) String() string

func (*Fragment) UnmarshalJSON

func (f *Fragment) UnmarshalJSON(data []byte) error

type Mark

type Mark struct {
	Type  MarkType       `json:"type"`
	Attrs map[string]any `json:"attrs,omitempty"`
}

func (Mark) Eq

func (m Mark) Eq(other Mark) bool

type MarkSpec

type MarkSpec struct {
	// The attributes this mark can have.
	Attrs map[string]Attribute

	// Whether this mark should be active at its end.
	Inclusive bool

	// Determines which other marks this can coexist with.
	Excludes string

	// The group or groups this mark belongs to.
	Group string

	// Whether this mark can span multiple nodes.
	Spanning bool

	// Additional spec properties.
	Extra map[string]any
}

type MarkType

type MarkType struct {
	// The Name of the mark type.
	Name MarkTypeName

	// The schema this mark type is part of.
	Schema Schema
	// The spec for this mark type.
	Spec MarkSpec

	// The defined attributes for this mark type.
	Attrs map[string]Attribute

	// Marks excluded by this mark type.
	Excluded []MarkType

	// A mark instance with default attributes.
	Instance *Mark
}

MarkType represents a mark on a node. Marks are used to represent things like whether a node is emphasized or part of a link.

func NewMarkType

func NewMarkType(s Schema, name MarkTypeName, spec MarkSpec) MarkType

func (MarkType) Create

func (mt MarkType) Create(attrs map[string]any) Mark

Create a new mark of this type.

func (MarkType) Eq

func (m MarkType) Eq(other MarkType) bool

func (MarkType) Excludes

func (mt MarkType) Excludes(other MarkType) bool

Check if this mark type excludes another type.

func (MarkType) IsInSet

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

Check if this mark type is in a mark set.

func (MarkType) MarshalJSON

func (mt MarkType) MarshalJSON() ([]byte, error)

func (MarkType) RemoveFromSet

func (mt MarkType) RemoveFromSet(set []MarkType) []MarkType

Remove this mark type from a mark set, if present.

func (MarkType) String

func (mt MarkType) String() string

func (*MarkType) UnmarshalJSON

func (mt *MarkType) UnmarshalJSON(b []byte) error

type MarkTypeName

type MarkTypeName string

type Node

type Node struct {
	// The type of this node.
	Type NodeType `json:"type,omitempty"`

	// A map of attribute names to values.
	// The kind of attributes depends on the node type.
	Attrs map[string]any `json:"attrs,omitempty"`

	// The marks (things like whether it is emphasized or part of a link) applied to this node.
	Marks []Mark `json:"marks,omitempty"`

	// For text nodes, this contains the node's text content.
	Text string `json:"text,omitempty"`

	// The child node at the given index.
	Content Fragment `json:"content,omitempty,omitzero"`
}

A node is a nested object which represents a fragment of a document. It roughly corresponds to an HTML element. Each node type has an associated string, known as its name. It may have any number of attributes, represented as a map from strings to arbitrary values.

  • If the node type is text, it may have a string content property.
  • If the node type is a non-leaf node, it may have a content property containing an array of child nodes.
  • If the node type has inline content, it may have a marks property containing an array of marks.
  • If the node type is a block node, it may have a attrs property containing a map of additional HTML attributes for the element.

The size of a node is either n.textLen(), 1, or the sum of the sizes of its children.

func (Node) Child

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

func (Node) ChildCount

func (n Node) ChildCount() int

func (Node) Clone

func (n Node) Clone() Node

func (Node) Descendants

func (n Node) Descendants(f func(n Node, pos int, parent *Node, index int) bool)

Call the given callback for every descendant node. Doesn't descend into a node when the callback returns `false`.

func (Node) FirstChild

func (n Node) FirstChild() *Node

func (Node) IsAtom

func (n Node) IsAtom() bool

func (Node) IsInline

func (n Node) IsInline() bool

func (Node) IsLeaf

func (n Node) IsLeaf() bool

func (Node) IsText

func (n Node) IsText() bool

func (Node) MarshalJSON

func (n Node) MarshalJSON() ([]byte, error)

func (Node) MaybeChild

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

func (Node) NodeSize

func (n Node) NodeSize() int

func (Node) Replace

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

func (Node) Resolve

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

func (Node) Slice

func (n Node) Slice(from, to int, includeParents bool) (Slice, error)

Cut 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

func (Node) WithMarks

func (n Node) WithMarks(m []Mark) Node

type NodeSpec

type NodeSpec struct {
	// The content expression for this node.
	Content string

	// The marks that are allowed inside this node.
	Marks *string

	// The group or groups this node belongs to.
	Group string

	// Should be true for inline nodes.
	Inline bool

	// Can be set to true for non-leaf nodes.
	Atom bool

	// The attributes this node can have.
	Attrs map[string]Attribute

	// Arbitrary additional properties.
	Extra map[string]any
}

type NodeType

type NodeType struct {
	Name          NodeTypeName
	Spec          NodeSpec
	Schema        Schema
	Block         bool
	Text          bool
	Groups        []string
	Marks         []MarkType
	Attrs         Attrs
	DefaultAttrs  map[string]any
	ContentMatch  ContentMatch
	InlineContent bool
}

NodeType is the type of a slice content element

func NewNodeType

func NewNodeType(name NodeTypeName) (NodeType, error)

func (NodeType) AllowsMarkType

func (n NodeType) AllowsMarkType(markType MarkType) bool

func (NodeType) CheckContent

func (n NodeType) CheckContent(f Fragment) error

func (NodeType) Create

func (n NodeType) Create(attrs map[string]any, marks []Mark, content ...Node) (Node, error)

CreateNodeType creates a node of the given type with the given attributes and content.

func (NodeType) Eq

func (n NodeType) Eq(other NodeType) bool

func (NodeType) Format

func (n NodeType) Format(s fmt.State, verb rune)

func (NodeType) MarshalJSON

func (n NodeType) MarshalJSON() ([]byte, error)

func (*NodeType) UnmarshalJSON

func (n *NodeType) UnmarshalJSON(b []byte) error

type NodeTypeName

type NodeTypeName string

type ResolvedPos

type ResolvedPos struct {
	Depth        int
	Pos          int
	ParentOffset int

	// in the original JS implementation, paths is a any[] type
	// they index it to retrieve the right one.
	// nodepath is *3
	NodePath []Node
	// indexpath is *3+1
	IndexPath []int
	// offsetpath is *3+2 or *3-1
	OffsetPath []int
}

func (ResolvedPos) End

func (r ResolvedPos) End(depth int) int

func (ResolvedPos) Index

func (r ResolvedPos) Index(depth int) int

func (ResolvedPos) IndexAfter

func (r ResolvedPos) IndexAfter(depth int) int

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

func (ResolvedPos) Node

func (r ResolvedPos) Node(depth int) Node

func (ResolvedPos) NodeAfter

func (r ResolvedPos) NodeAfter() *Node

Get 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

Get 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

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

func (ResolvedPos) Start

func (r ResolvedPos) Start(depth int) int

func (ResolvedPos) String

func (r ResolvedPos) String() string

func (ResolvedPos) TextOffset

func (r ResolvedPos) TextOffset() bool

When this position points into a text node, this returns 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 {
	Spec SchemaSpec

	TopNodeType *NodeType
	Nodes       map[NodeTypeName]NodeType
	Marks       map[MarkTypeName]MarkType
}

func NewSchema

func NewSchema(spec SchemaSpec) (Schema, error)

func (Schema) Mark

func (s Schema) Mark(typ MarkTypeName, attrs map[string]any) Mark

func (Schema) Node

func (s Schema) Node(typ NodeTypeName, attrs map[string]any, content Fragment, marks ...Mark) Node

func (Schema) Text

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

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.
	// The order in which they occur in the list is significant.
	Nodes map[NodeTypeName]NodeSpec

	// The mark types that exist in this schema.
	Marks map[MarkTypeName]MarkSpec

	// The name of the default top-level node for the schema.
	TopNode NodeTypeName

	// Don't register the schema in the global schema store.
	DontRegister bool
}

type Slice

type Slice struct {
	Content   Fragment `json:"content,omitempty"`
	OpenStart int      `json:"openStart,omitempty"`
	OpenEnd   int      `json:"openEnd,omitempty"`
}

func (Slice) InsertAt

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

func (Slice) String

func (s Slice) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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