prosekit/pm/model
Re-exported from prosemirror-model.
ContentMatch
Instances of this class represent 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.
-
constructor
-
new ContentMatch(): ContentMatch
-
validEnd: boolean
-
True when this match state represents a valid end of the node.
-
get defaultType(): null | NodeType
-
Get the first matching node type at this match position that can be generated.
-
get edgeCount(): number
-
The number of outgoing edges this node has in the finite automaton that describes the content expression.
-
edge
-
Get the _n_th outgoing edge from this node in the finite automaton that describes the content expression.
const edge: (n: number) => MatchEdge
-
fillBefore
-
Try 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.const fillBefore: (after: ProseMirrorFragment, toEnd?: boolean, startIndex?: number) => null | ProseMirrorFragment
-
findWrapping
-
Find a set of wrapping node types that would allow a node of the given type to appear at this position. The result may be empty (when it fits directly) and will be null when no such wrapping exists.
const findWrapping: (target: NodeType) => null | readonly NodeType[]
-
matchFragment
-
Try to match a fragment. Returns the resulting match when successful.
const matchFragment: (frag: ProseMirrorFragment, start?: number, end?: number) => null | ContentMatch
-
matchType
-
Match a node type, returning a match after that node if successful.
const matchType: (type: NodeType) => null | ContentMatch
DOMParser
A DOM parser represents a strategy for parsing DOM content into a ProseMirror document conforming to a given schema. Its behavior is defined by an array of rules.
-
constructor
-
new DOMParser(schema: Schema, rules: readonly ParseRule[]): DOMParser
-
rules: readonly ParseRule[]
-
The set of parse rules that the parser uses, in order of precedence.
-
schema: Schema
-
The schema into which the parser parses.
-
parse
-
Parse a document from the content of a DOM node.
const parse: (dom: Node, options?: ParseOptions) => ProseMirrorNode
-
parseSlice
-
Parses the content of the given DOM node, like
parse
, and takes the same set of options. But unlike that method, which produces a whole node, this one returns a slice that is open at the sides, meaning that the schema constraints aren’t applied to the start of nodes to the left of the input and the end of nodes at the end.const parseSlice: (dom: Node, options?: ParseOptions) => Slice
-
fromSchema
-
Construct a DOM parser using the parsing rules listed in a schema’s node specs, reordered by priority.
const fromSchema: (schema: Schema) => DOMParser
DOMSerializer
A DOM serializer knows how to convert ProseMirror nodes and marks of various types to DOM nodes.
-
constructor
-
new DOMSerializer(nodes: { [node]: (node: ProseMirrorNode) => DOMOutputSpec }, marks: { [mark]: (mark: Mark, inline: boolean) => DOMOutputSpec }): DOMSerializer
-
marks: { [mark]: (mark: Mark, inline: boolean) => DOMOutputSpec }
-
The mark serialization functions.
-
nodes: { [node]: (node: ProseMirrorNode) => DOMOutputSpec }
-
The node serialization functions.
-
serializeFragment
-
Serialize the content of this fragment to a DOM fragment. When not in the browser, the
document
option, containing a DOM document, should be passed so that the serializer can create nodes.const serializeFragment: (fragment: ProseMirrorFragment, options?: { document?: Document }, target?: HTMLElement | DocumentFragment) => HTMLElement | DocumentFragment
-
serializeNode
-
Serialize this node to a DOM node. This can be useful when you need to serialize a part of a document, as opposed to the whole document. To serialize a whole document, use
serializeFragment
on its content.const serializeNode: (node: ProseMirrorNode, options?: { document?: Document }) => Node
-
fromSchema
-
Build a serializer using the
toDOM
properties in a schema’s node and mark specs.const fromSchema: (schema: Schema) => DOMSerializer
-
marksFromSchema
-
Gather the serializers in a schema’s mark specs into an object.
const marksFromSchema: (schema: Schema) => { [mark]: (mark: Mark, inline: boolean) => DOMOutputSpec }
-
nodesFromSchema
-
Gather the serializers in a schema’s node specs into an object. This can be useful as a base to build a custom serializer from.
const nodesFromSchema: (schema: Schema) => { [node]: (node: ProseMirrorNode) => DOMOutputSpec }
-
renderSpec
-
Render an output spec to a DOM node. If the spec has a hole (zero) in it,
contentDOM
will point at the node with the hole.const renderSpec: (doc: Document, structure: DOMOutputSpec, xmlNS?: null | string) => { contentDOM?: HTMLElement; dom: Node }
Mark
A 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.
-
constructor
-
new Mark(): Mark
-
attrs: Attrs
-
The attributes associated with this mark.
-
type: MarkType
-
The type of this mark.
-
none: readonly Mark[]
-
The empty set of marks.
-
addToSet
-
Given a set of marks, create 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.
const addToSet: (set: readonly Mark[]) => readonly Mark[]
-
eq
-
Test whether this mark has the same type and attributes as another mark.
const eq: (other: Mark) => boolean
-
isInSet
-
Test whether this mark is in the given set of marks.
const isInSet: (set: readonly Mark[]) => boolean
-
removeFromSet
-
Remove this mark from the given set, returning a new set. If this mark is not in the set, the set itself is returned.
const removeFromSet: (set: readonly Mark[]) => readonly Mark[]
-
toJSON
-
Convert this mark to a JSON-serializeable representation.
const toJSON: () => any
-
fromJSON
-
Deserialize a mark from JSON.
const fromJSON: (schema: Schema, json: any) => Mark
-
sameSet
-
Test whether two sets of marks are identical.
const sameSet: (a: readonly Mark[], b: readonly Mark[]) => boolean
-
setFrom
-
Create a properly sorted mark set from null, a single mark, or an unsorted array of marks.
const setFrom: (marks?: null | Mark | readonly Mark[]) => readonly Mark[]
MarkType
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
.
-
constructor
-
new MarkType(): MarkType
-
name: string
-
The name of the mark type.
-
schema: Schema
-
The schema that this mark type instance is part of.
-
spec: MarkSpec
-
The spec on which the type is based.
-
create
-
Create a mark of this type.
attrs
may benull
or an object containing only some of the mark’s attributes. The others, if they have defaults, will be added.const create: (attrs?: null | Attrs) => Mark
-
excludes
-
Queries whether a given mark type is excluded by this one.
const excludes: (other: MarkType) => boolean
-
isInSet
-
Tests whether there is a mark of this type in the given set.
const isInSet: (set: readonly Mark[]) => undefined | Mark
-
removeFromSet
-
When there is a mark of this type in the given set, a new set without it is returned. Otherwise, the input set is returned.
const removeFromSet: (set: readonly Mark[]) => readonly Mark[]
NodeRange
Represents a flat range of content, i.e. one that starts and ends in the same node.
-
constructor
-
new NodeRange($from: ResolvedPos, $to: ResolvedPos, depth: number): NodeRange
-
$from: ResolvedPos
-
A resolved position along the start of the content. May have a
depth
greater than this object’sdepth
property, since these are the positions that were used to compute the range, not re-resolved positions directly at its boundaries. -
$to: ResolvedPos
-
A position along the end of the content. See caveat for
$from
. -
depth: number
-
The depth of the node that this range points into.
-
get end(): number
-
The position at the end of the range.
-
get endIndex(): number
-
The end index of the range in the parent node.
-
get parent(): ProseMirrorNode
-
The parent node that the range points into.
-
get start(): number
-
The position at the start of the range.
-
get startIndex(): number
-
The start index of the range in the parent node.
NodeType
Node types 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.
-
constructor
-
new NodeType(): NodeType
-
contentMatch: ContentMatch
-
The starting match of the node type’s content expression.
-
inlineContent: boolean
-
True if this node type has inline content.
-
isBlock: boolean
-
True if this is a block type
-
isText: boolean
-
True if this is the text node type.
-
markSet: null | readonly MarkType[]
-
The set of marks allowed in this node.
null
means all marks are allowed. -
name: string
-
The name the node type has in this schema.
-
schema: Schema
-
A link back to the
Schema
the node type belongs to. -
spec: NodeSpec
-
The spec that this type is based on
-
get isAtom(): boolean
-
True when this node is an atom, i.e. when it does not have directly editable content.
-
get isInline(): boolean
-
True if this is an inline type.
-
get isLeaf(): boolean
-
True for node types that allow no content.
-
get isTextblock(): boolean
-
True if this is a textblock type, a block that contains inline content.
-
get whitespace(): "pre" | "normal"
-
The node type’s whitespace option.
-
allowedMarks
-
Removes the marks that are not allowed in this node from the given set.
const allowedMarks: (marks: readonly Mark[]) => readonly Mark[]
-
allowsMarks
-
Test whether the given set of marks are allowed in this node.
const allowsMarks: (marks: readonly Mark[]) => boolean
-
allowsMarkType
-
Check whether the given mark type is allowed in this node.
const allowsMarkType: (markType: MarkType) => boolean
-
compatibleContent
-
Indicates whether this node allows some of the same content as the given node type.
const compatibleContent: (other: NodeType) => boolean
-
create
-
Create a
Node
of this type. The given attributes are checked and defaulted (you can passnull
to use the type’s defaults entirely, if no required attributes exist).content
may be aFragment
, a node, an array of nodes, ornull
. Similarlymarks
may benull
to default to the empty set of marks.const create: (attrs?: null | Attrs, content?: null | ProseMirrorNode | ProseMirrorFragment | readonly ProseMirrorNode[], marks?: readonly Mark[]) => ProseMirrorNode
-
createAndFill
-
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 orFragment.empty
as content.const createAndFill: (attrs?: null | Attrs, content?: null | ProseMirrorNode | ProseMirrorFragment | readonly ProseMirrorNode[], marks?: readonly Mark[]) => null | ProseMirrorNode
-
createChecked
-
Like
create
, but check the given content against the node type’s content restrictions, and throw an error if it doesn’t match.const createChecked: (attrs?: null | Attrs, content?: null | ProseMirrorNode | ProseMirrorFragment | readonly ProseMirrorNode[], marks?: readonly Mark[]) => ProseMirrorNode
-
hasRequiredAttrs
-
Tells you whether this node type has any required attributes.
const hasRequiredAttrs: () => boolean
-
isInGroup
-
Return true when this node type is part of the given group.
const isInGroup: (group: string) => boolean
-
validContent
-
Returns true if the given fragment is valid content for this node type.
const validContent: (content: ProseMirrorFragment) => boolean
ProseMirrorFragment
A 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.
-
constructor
-
new ProseMirrorFragment(): ProseMirrorFragment
-
content: readonly ProseMirrorNode[]
-
The child nodes in this fragment.
-
size: number
-
The size of the fragment, which is the total of the size of its content nodes.
-
empty: ProseMirrorFragment
-
An empty fragment. Intended to be reused whenever a node doesn’t contain anything (rather than allocating a new empty fragment for each leaf node).
-
get childCount(): number
-
The number of child nodes in this fragment.
-
get firstChild(): null | ProseMirrorNode
-
The first child of the fragment, or
null
if it is empty. -
get lastChild(): null | ProseMirrorNode
-
The last child of the fragment, or
null
if it is empty. -
addToEnd
-
Create a new fragment by appending the given node to this fragment.
const addToEnd: (node: ProseMirrorNode) => ProseMirrorFragment
-
addToStart
-
Create a new fragment by prepending the given node to this fragment.
const addToStart: (node: ProseMirrorNode) => ProseMirrorFragment
-
append
-
Create a new fragment containing the combined content of this fragment and the other.
const append: (other: ProseMirrorFragment) => ProseMirrorFragment
-
child
-
Get the child node at the given index. Raise an error when the index is out of range.
const child: (index: number) => ProseMirrorNode
-
cut
-
Cut out the sub-fragment between the two given positions.
const cut: (from: number, to?: number) => ProseMirrorFragment
-
descendants
-
Call the given callback for every descendant node.
pos
will be relative to the start of the fragment. The callback may returnfalse
to prevent traversal of a given node’s children.const descendants: (f: (node: ProseMirrorNode, pos: number, parent: null | ProseMirrorNode, index: number) => boolean | void) => void
-
eq
-
Compare this fragment to another one.
const eq: (other: ProseMirrorFragment) => boolean
-
findDiffEnd
-
Find 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.const findDiffEnd: (other: ProseMirrorFragment, pos?: number, otherPos?: number) => null | ({ a: number; b: number })
-
findDiffStart
-
Find the first position at which this fragment and another fragment differ, or
null
if they are the same.const findDiffStart: (other: ProseMirrorFragment, pos?: number) => null | number
-
forEach
-
Call
f
for every child node, passing the node, its offset into this parent node, and its index.const forEach: (f: (node: ProseMirrorNode, offset: number, index: number) => void) => void
-
maybeChild
-
Get the child node at the given index, if it exists.
const maybeChild: (index: number) => null | ProseMirrorNode
-
nodesBetween
-
Invoke 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
.const nodesBetween: (from: number, to: number, f: (node: ProseMirrorNode, start: number, parent: null | ProseMirrorNode, index: number) => boolean | void, nodeStart?: number, parent?: ProseMirrorNode) => void
-
replaceChild
-
Create a new fragment in which the node at the given index is replaced by the given node.
const replaceChild: (index: number, node: ProseMirrorNode) => ProseMirrorFragment
-
textBetween
-
Extract the text between
from
andto
. See the same method onNode
.const textBetween: (from: number, to: number, blockSeparator?: null | string, leafText?: null | string | ((leafNode: ProseMirrorNode) => string)) => string
-
toJSON
-
Create a JSON-serializeable representation of this fragment.
const toJSON: () => any
-
toString
-
Return a debugging string that describes this fragment.
const toString: () => string
-
from
-
Create 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.const from: (nodes?: null | ProseMirrorNode | ProseMirrorFragment | readonly ProseMirrorNode[]) => ProseMirrorFragment
-
fromArray
-
Build a fragment from an array of nodes. Ensures that adjacent text nodes with the same marks are joined together.
const fromArray: (array: readonly ProseMirrorNode[]) => ProseMirrorFragment
-
fromJSON
-
Deserialize a fragment from its JSON representation.
const fromJSON: (schema: Schema, value: any) => ProseMirrorFragment
ProseMirrorNode
This 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. See the guide for more information.
-
constructor
-
new ProseMirrorNode(): ProseMirrorNode
-
attrs: Attrs
-
An object mapping attribute names to values. The kind of attributes allowed and required are determined by the node type.
-
content: ProseMirrorFragment
-
A container holding the node’s children.
-
marks: readonly Mark[]
-
The marks (things like whether it is emphasized or part of a link) applied to this node.
-
text: undefined | string
-
For text nodes, this contains the node’s text content.
-
type: NodeType
-
The type of node that this is.
-
get childCount(): number
-
The number of children that the node has.
-
get children(): readonly ProseMirrorNode[]
-
The array of this node’s child nodes.
-
get firstChild(): null | ProseMirrorNode
-
Returns this node’s first child, or
null
if there are no children. -
get inlineContent(): boolean
-
True when this node allows inline content.
-
get isAtom(): boolean
-
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 theatom
property on a node’s spec (typically used when the node is displayed as an uneditable node view). -
get isBlock(): boolean
-
True when this is a block (non-inline node)
-
get isInline(): boolean
-
True when this is an inline node (a text node or a node that can appear among text).
-
get isLeaf(): boolean
-
True when this is a leaf node.
-
get isText(): boolean
-
True when this is a text node.
-
get isTextblock(): boolean
-
True when this is a textblock node, a block node with inline content.
-
get lastChild(): null | ProseMirrorNode
-
Returns this node’s last child, or
null
if there are no children. -
get nodeSize(): number
-
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).
-
get textContent(): string
-
Concatenates all the text nodes found in this fragment and its children.
-
canAppend
-
Test whether the given node’s content could be appended to this node. If that node is empty, this will only return true if there is at least one node type that can appear in both nodes (to avoid merging completely incompatible nodes).
const canAppend: (other: ProseMirrorNode) => boolean
-
canReplace
-
Test whether replacing the range between
from
andto
(by child index) with the given replacement fragment (which defaults to the empty fragment) would leave the node’s content valid. You can optionally passstart
andend
indices into the replacement fragment.const canReplace: (from: number, to: number, replacement?: ProseMirrorFragment, start?: number, end?: number) => boolean
-
canReplaceWith
-
Test whether replacing the range
from
toto
(by index) with a node of the given type would leave the node’s content valid.const canReplaceWith: (from: number, to: number, type: NodeType, marks?: readonly Mark[]) => boolean
-
check
-
Check whether this node and its descendants conform to the schema, and raise an exception when they do not.
const check: () => void
-
child
-
Get the child node at the given index. Raises an error when the index is out of range.
const child: (index: number) => ProseMirrorNode
-
childAfter
-
Find the (direct) child node after the given offset, if any, and return it along with its index and offset relative to this node.
const childAfter: (pos: number) => { index: number; node: null | ProseMirrorNode; offset: number }
-
childBefore
-
Find the (direct) child node before the given offset, if any, and return it along with its index and offset relative to this node.
const childBefore: (pos: number) => { index: number; node: null | ProseMirrorNode; offset: number }
-
contentMatchAt
-
Get the content match in this node at the given index.
const contentMatchAt: (index: number) => ContentMatch
-
copy
-
Create a new node with the same markup as this node, containing the given content (or empty, if no content is given).
const copy: (content?: null | ProseMirrorFragment) => ProseMirrorNode
-
cut
-
Create 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.const cut: (from: number, to?: number) => ProseMirrorNode
-
descendants
-
Call the given callback for every descendant node. Doesn’t descend into a node when the callback returns
false
.const descendants: (f: (node: ProseMirrorNode, pos: number, parent: null | ProseMirrorNode, index: number) => boolean | void) => void
-
eq
-
Test whether two nodes represent the same piece of document.
const eq: (other: ProseMirrorNode) => boolean
-
forEach
-
Call
f
for every child node, passing the node, its offset into this parent node, and its index.const forEach: (f: (node: ProseMirrorNode, offset: number, index: number) => void) => void
-
hasMarkup
-
Check whether this node’s markup correspond to the given type, attributes, and marks.
const hasMarkup: (type: NodeType, attrs?: null | Attrs, marks?: readonly Mark[]) => boolean
-
mark
-
Create a copy of this node, with the given set of marks instead of the node’s own marks.
const mark: (marks: readonly Mark[]) => ProseMirrorNode
-
maybeChild
-
Get the child node at the given index, if it exists.
const maybeChild: (index: number) => null | ProseMirrorNode
-
nodeAt
-
Find the node directly after the given position.
const nodeAt: (pos: number) => null | ProseMirrorNode
-
nodesBetween
-
Invoke 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 position relative to the original node (method receiver), 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.
const nodesBetween: (from: number, to: number, f: (node: ProseMirrorNode, pos: number, parent: null | ProseMirrorNode, index: number) => boolean | void, startPos?: number) => void
-
rangeHasMark
-
Test whether a given mark or mark type occurs in this document between the two given positions.
const rangeHasMark: (from: number, to: number, type: MarkType | Mark) => boolean
-
replace
-
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.const replace: (from: number, to: number, slice: Slice) => ProseMirrorNode
-
resolve
-
Resolve the given position in the document, returning an object with information about its context.
const resolve: (pos: number) => ResolvedPos
-
sameMarkup
-
Compare the markup (type, attributes, and marks) of this node to those of another. Returns
true
if both have the same markup.const sameMarkup: (other: ProseMirrorNode) => boolean
-
slice
-
Cut out the part of the document between the given positions, and return it as a
Slice
object.const slice: (from: number, to?: number, includeParents?: boolean) => Slice
-
textBetween
-
Get all text between positions
from
andto
. WhenblockSeparator
is given, it will be inserted to separate text from different block nodes. IfleafText
is given, it’ll be inserted for every non-text leaf node encountered, otherwiseleafText
will be used.const textBetween: (from: number, to: number, blockSeparator?: null | string, leafText?: null | string | ((leafNode: ProseMirrorNode) => string)) => string
-
toJSON
-
Return a JSON-serializeable representation of this node.
const toJSON: () => any
-
toString
-
Return a string representation of this node for debugging purposes.
const toString: () => string
-
fromJSON
-
Deserialize a node from its JSON representation.
const fromJSON: (schema: Schema, json: any) => ProseMirrorNode
ReplaceError
Extends Error
Error type raised by Node.replace
when given an invalid replacement.
ResolvedPos
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
.
-
constructor
-
new ResolvedPos(): ResolvedPos
-
depth: number
-
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.
-
parentOffset: number
-
The offset this position has into its parent node.
-
pos: number
-
The position that was resolved.
-
get doc(): ProseMirrorNode
-
The root node in which the position was resolved.
-
get nodeAfter(): null | ProseMirrorNode
-
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.
-
get nodeBefore(): null | ProseMirrorNode
-
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.
-
get parent(): ProseMirrorNode
-
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.
-
get textOffset(): number
-
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.
-
after
-
The (absolute) position directly after the wrapping node at the given level, or the original position when
depth
isthis.depth + 1
.const after: (depth?: null | number) => number
-
before
-
The (absolute) position directly before the wrapping node at the given level, or, when
depth
isthis.depth + 1
, the original position.const before: (depth?: null | number) => number
-
blockRange
-
Returns a range based on the place where this position and the given position diverge around block content. If both point into the same textblock, for example, a range around that textblock will be returned. If they point into different blocks, the range around those blocks in their shared ancestor is returned. You can pass in an optional predicate that will be called with a parent node to see if a range into that parent is acceptable.
const blockRange: (other?: ResolvedPos, pred?: (node: ProseMirrorNode) => boolean) => null | NodeRange
-
end
-
The (absolute) position at the end of the node at the given level.
const end: (depth?: null | number) => number
-
index
-
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 andp.index(1)
is 2.const index: (depth?: null | number) => number
-
indexAfter
-
The index pointing after this position into the ancestor at the given level.
const indexAfter: (depth?: null | number) => number
-
marks
-
Get 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.const marks: () => readonly Mark[]
-
marksAcross
-
Get the marks after the current position, if any, except those that are non-inclusive and not present at position
$end
. This is mostly useful for getting the set of marks to preserve after a deletion. Will returnnull
if this position is at the end of its parent node or its parent node isn’t a textblock (in which case no marks should be preserved).const marksAcross: ($end: ResolvedPos) => null | readonly Mark[]
-
max
-
Return the greater of this and the given position.
const max: (other: ResolvedPos) => ResolvedPos
-
min
-
Return the smaller of this and the given position.
const min: (other: ResolvedPos) => ResolvedPos
-
node
-
The ancestor node at the given level.
p.node(p.depth)
is the same asp.parent
.const node: (depth?: null | number) => ProseMirrorNode
-
posAtIndex
-
Get the position at the given index in the parent node at the given depth (which defaults to
this.depth
).const posAtIndex: (index: number, depth?: null | number) => number
-
sameParent
-
Query whether the given position shares the same parent node.
const sameParent: (other: ResolvedPos) => boolean
-
sharedDepth
-
The depth up to which this position and the given (non-resolved) position share the same parent nodes.
const sharedDepth: (pos: number) => number
-
start
-
The (absolute) position at the start of the node at the given level.
const start: (depth?: null | number) => number
Schema
A document schema. 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.
When given, the type parameters provide the names of the nodes and marks in this schema.
-
constructor
-
new Schema<Nodes extends string, Marks extends string>(spec: SchemaSpec<Nodes, Marks>): Schema<Nodes, Marks>
-
cached: { [key]: any }
-
An object for storing whatever values modules may want to compute and cache per schema. (If you want to store something in it, try to use property names unlikely to clash.)
-
linebreakReplacement: null | NodeType
-
The linebreak replacement node defined in this schema, if any.
-
marks: {readonly[name in string]: MarkType} & { [key]: MarkType }
-
A map from mark names to mark type objects.
-
nodes: {readonly[name in string]: NodeType} & { [key]: NodeType }
-
An object mapping the schema’s node names to node type objects.
-
spec: { marks: OrderedMap<MarkSpec>; nodes: OrderedMap<NodeSpec>; topNode?: string }
-
The spec on which the schema is based, with the added guarantee that its
nodes
andmarks
properties areOrderedMap
instances (not raw objects). -
topNodeType: NodeType
-
The type of the default top node for this schema.
-
mark
-
Create a mark with the given type and attributes.
const mark: (type: string | MarkType, attrs?: null | Attrs) => Mark
-
markFromJSON
-
Deserialize a mark from its JSON representation. This method is bound.
const markFromJSON: (json: any) => Mark
-
node
-
Create a node in this schema. The
type
may be a string or aNodeType
instance. Attributes will be extended with defaults,content
may be aFragment
,null
, aNode
, or an array of nodes.const node: (type: string | NodeType, attrs?: null | Attrs, content?: ProseMirrorNode | ProseMirrorFragment | readonly ProseMirrorNode[], marks?: readonly Mark[]) => ProseMirrorNode
-
nodeFromJSON
-
Deserialize a node from its JSON representation. This method is bound.
const nodeFromJSON: (json: any) => ProseMirrorNode
-
text
-
Create a text node in the schema. Empty text nodes are not allowed.
const text: (text: string, marks?: null | readonly Mark[]) => ProseMirrorNode
Slice
A 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).
-
constructor
-
new Slice(content: ProseMirrorFragment, openStart: number, openEnd: number): Slice
-
content: ProseMirrorFragment
-
The slice’s content.
-
openEnd: number
-
The open depth at the end.
-
openStart: number
-
The open depth at the start of the fragment.
-
empty: Slice
-
The empty slice.
-
get size(): number
-
The size this slice would add when inserted into a document.
-
eq
-
Tests whether this slice is equal to another slice.
const eq: (other: Slice) => boolean
-
toJSON
-
Convert a slice to a JSON-serializable representation.
const toJSON: () => any
-
fromJSON
-
Deserialize a slice from its JSON representation.
const fromJSON: (schema: Schema, json: any) => Slice
-
maxOpen
-
Create a slice from a fragment by taking the maximum possible open value on both side of the fragment.
const maxOpen: (fragment: ProseMirrorFragment, openIsolating?: boolean) => Slice
AttributeSpec
Used to define attributes on nodes or marks.
-
default?: any
-
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.
-
validate?: string | ((value: any) => void)
-
A function or type name used to validate values of this attribute. This will be used when deserializing the attribute from JSON, and when running
Node.check
. When a function, it should raise an exception if the value isn’t of the expected type or shape. When a string, it should be a|
-separated string of primitive types ("number"
,"string"
,"boolean"
,"null"
, and"undefined"
), and the library will raise an error when the value is not one of those types.
GenericParseRule
Fields that may be present in both tag and style parse rules.
-
attrs?: Attrs
-
Attributes for the node or mark created by this rule. When
getAttrs
is provided, it takes precedence. -
closeParent?: boolean
-
When true, finding an element that matches this rule will close the current node.
-
consuming?: boolean
-
By default, when a rule matches an element or style, no further rules get a chance to match it. By setting this to
false
, you indicate that even when this rule matches, other rules that come after it should also run. -
context?: string
-
When given, restricts this rule to only match when the current context—the parent nodes into which the content is being parsed—matches this expression. Should contain one or more node names or node group names followed by single or double slashes. For example
"paragraph/"
means the rule only matches when the parent node is a paragraph,"blockquote/paragraph/"
restricts it to be in a paragraph that is inside a blockquote, and"section//"
matches any position inside a section—a double slash matches any sequence of ancestor nodes. To allow multiple different contexts, they can be separated by a pipe (|
) character, as in"blockquote/|list_item/"
. -
ignore?: boolean
-
When true, ignore content that matches this rule.
-
mark?: string
-
The name of the mark type to wrap the matched content in.
-
priority?: number
-
Can be used to change the order in which the parse rules in a schema are tried. Those with higher priority come first. Rules without a priority are counted as having priority 50. This property is only meaningful in a schema—when directly constructing a parser, the order of the rule array is used.
-
skip?: boolean
-
When true, ignore the node that matches this rule, but do parse its content.
MarkSpec
Used to define marks when creating a schema.
-
attrs?: { [name]: AttributeSpec }
-
The attributes that marks of this type get.
-
excludes?: string
-
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).
-
group?: string
-
The group or space-separated groups to which this mark belongs.
-
inclusive?: boolean
-
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.
-
parseDOM?: readonly ParseRule[]
-
Associates DOM parser information with this mark (see the corresponding node spec field). The
mark
field in the rules is implied. -
spanning?: boolean
-
Determines whether marks of this type can span multiple adjacent nodes when serialized to DOM/HTML. Defaults to true.
-
toDOM?: (mark: Mark, inline: boolean) => DOMOutputSpec
-
Defines the default way marks of this type should be serialized to DOM/HTML. When the resulting spec contains a hole, that is where the marked content is placed. Otherwise, it is appended to the top node.
NodeSpec
A description of a node type, used when defining a schema.
-
atom?: boolean
-
Can be set to true to indicate that, though this isn’t a leaf node, it doesn’t have directly editable content and should be treated as a single unit in the view.
-
attrs?: { [name]: AttributeSpec }
-
The attributes that nodes of this type get.
-
code?: boolean
-
Can be used to indicate that this node contains code, which causes some commands to behave differently.
-
content?: string
-
The content expression for this node, as described in the schema guide. When not given, the node does not allow any content.
-
defining?: boolean
-
When enabled, enables both
definingAsContext
anddefiningForContent
. -
definingAsContext?: boolean
-
Determines whether this node is considered an important parent node during replace operations (such as paste). Non-defining (the default) nodes get dropped when their entire content is replaced, whereas defining nodes persist and wrap the inserted content.
-
definingForContent?: boolean
-
In inserted content the defining parents of the content are preserved when possible. Typically, non-default-paragraph textblock types, and possibly list items, are marked as defining.
-
draggable?: boolean
-
Determines whether nodes of this type can be dragged without being selected. Defaults to false.
-
group?: string
-
The group or space-separated groups to which this node belongs, which can be referred to in the content expressions for the schema.
-
inline?: boolean
-
Should be set to true for inline nodes. (Implied for text nodes.)
-
isolating?: boolean
-
When enabled (default is false), the sides of nodes of this type count as boundaries that regular editing operations, like backspacing or lifting, won’t cross. An example of a node that should probably have this enabled is a table cell.
-
leafText?: (node: ProseMirrorNode) => string
-
Defines the default way a leaf node of this type should be serialized to a string (as used by
Node.textBetween
andNode.textContent
). -
linebreakReplacement?: boolean
-
A single inline node in a schema can be set to be a linebreak equivalent. When converting between block types that support the node and block types that don’t but have
whitespace
set to"pre"
,setBlockType
will convert between newline characters to or from linebreak nodes as appropriate. -
marks?: string
-
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. -
parseDOM?: readonly TagParseRule[]
-
Associates DOM parser information with this node, which can be used by
DOMParser.fromSchema
to automatically derive a parser. Thenode
field in the rules is implied (the name of this node will be filled in automatically). If you supply your own parser, you do not need to also specify parsing rules in your schema. -
selectable?: boolean
-
Controls whether nodes of this type can be selected as a node selection. Defaults to true for non-text nodes.
-
toDebugString?: (node: ProseMirrorNode) => string
-
Defines the default way a node of this type should be serialized to a string representation for debugging (e.g. in error messages).
-
toDOM?: (node: ProseMirrorNode) => DOMOutputSpec
-
Defines the default way a node of this type should be serialized to DOM/HTML (as used by
DOMSerializer.fromSchema
). Should return a DOM node or an array structure that describes one, with an optional number zero (“hole”) in it to indicate where the node’s content should be inserted.For text nodes, the default is to create a text DOM node. Though it is possible to create a serializer where text is rendered differently, this is not supported inside the editor, so you shouldn’t override that in your text node spec.
-
whitespace?: "pre" | "normal"
-
Controls way whitespace in this a node is parsed. The default is
"normal"
, which causes the DOM parser to collapse whitespace in normal mode, and normalize it (replacing newlines and such with spaces) otherwise."pre"
causes the parser to preserve spaces inside the node. When this option isn’t given, butcode
is true,whitespace
will default to"pre"
. Note that this option doesn’t influence the way the node is rendered—that should be handled bytoDOM
and/or styling.
ParseOptions
These are the options recognized by the parse
and parseSlice
methods.
-
context?: ResolvedPos
-
A set of additional nodes to count as context when parsing, above the given top node.
-
findPositions?: { node: Node; offset: number; pos?: number }[]
-
When given, the parser will, beside parsing the content, record the document positions of the given DOM positions. It will do so by writing to the objects, adding a
pos
property that holds the document position. DOM positions that are not in the parsed content will not be written to. -
from?: number
-
The child node index to start parsing from.
-
preserveWhitespace?: boolean | "full"
-
By default, whitespace is collapsed as per HTML’s rules. Pass
true
to preserve whitespace, but normalize newlines to spaces, and"full"
to preserve whitespace entirely. -
to?: number
-
The child node index to stop parsing at.
-
topMatch?: ContentMatch
-
Provide the starting content match that content parsed into the top node is matched against.
-
topNode?: ProseMirrorNode
-
By default, the content is parsed into the schema’s default top node type. You can pass this option to use the type and attributes from a different node as the top container.
SchemaSpec
An object describing a schema, as passed to the Schema
constructor.
-
marks?: {[name in string]: MarkSpec} | OrderedMap<MarkSpec>
-
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.
-
nodes: {[name in string]: NodeSpec} | OrderedMap<NodeSpec>
-
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. -
topNode?: string
-
The name of the default top-level node for the schema. Defaults to
"doc"
.
StyleParseRule
A parse rule targeting a style property.
-
attrs?: Attrs
-
Attributes for the node or mark created by this rule. When
getAttrs
is provided, it takes precedence. -
clearMark?: (mark: Mark) => boolean
-
Style rules can remove marks from the set of active marks.
-
closeParent?: boolean
-
When true, finding an element that matches this rule will close the current node.
-
consuming?: boolean
-
By default, when a rule matches an element or style, no further rules get a chance to match it. By setting this to
false
, you indicate that even when this rule matches, other rules that come after it should also run. -
context?: string
-
When given, restricts this rule to only match when the current context—the parent nodes into which the content is being parsed—matches this expression. Should contain one or more node names or node group names followed by single or double slashes. For example
"paragraph/"
means the rule only matches when the parent node is a paragraph,"blockquote/paragraph/"
restricts it to be in a paragraph that is inside a blockquote, and"section//"
matches any position inside a section—a double slash matches any sequence of ancestor nodes. To allow multiple different contexts, they can be separated by a pipe (|
) character, as in"blockquote/|list_item/"
. -
getAttrs?: (node: string) => null | false | Attrs
-
A function used to compute the attributes for the node or mark created by this rule. Called with the style’s value.
-
ignore?: boolean
-
When true, ignore content that matches this rule.
-
mark?: string
-
The name of the mark type to wrap the matched content in.
-
priority?: number
-
Can be used to change the order in which the parse rules in a schema are tried. Those with higher priority come first. Rules without a priority are counted as having priority 50. This property is only meaningful in a schema—when directly constructing a parser, the order of the rule array is used.
-
skip?: boolean
-
When true, ignore the node that matches this rule, but do parse its content.
-
style: string
-
A CSS property name to match. This rule will match inline styles that list that property. May also have the form
"property=value"
, in which case the rule only matches if the property’s value exactly matches the given value. (For more complicated filters, usegetAttrs
and return false to indicate that the match failed.) Rules matching styles may only produce marks, not nodes. -
tag?: undefined
-
Given to make TS see ParseRule as a tagged union
Hide
TagParseRule
Parse rule targeting a DOM element.
-
attrs?: Attrs
-
Attributes for the node or mark created by this rule. When
getAttrs
is provided, it takes precedence. -
closeParent?: boolean
-
When true, finding an element that matches this rule will close the current node.
-
consuming?: boolean
-
By default, when a rule matches an element or style, no further rules get a chance to match it. By setting this to
false
, you indicate that even when this rule matches, other rules that come after it should also run. -
contentElement?: string | HTMLElement | ((node: Node) => HTMLElement)
-
For rules that produce non-leaf nodes, by default the content of the DOM element is parsed as content of the node. If the child nodes are in a descendent node, this may be a CSS selector string that the parser must use to find the actual content element, or a function that returns the actual content element to the parser.
-
context?: string
-
When given, restricts this rule to only match when the current context—the parent nodes into which the content is being parsed—matches this expression. Should contain one or more node names or node group names followed by single or double slashes. For example
"paragraph/"
means the rule only matches when the parent node is a paragraph,"blockquote/paragraph/"
restricts it to be in a paragraph that is inside a blockquote, and"section//"
matches any position inside a section—a double slash matches any sequence of ancestor nodes. To allow multiple different contexts, they can be separated by a pipe (|
) character, as in"blockquote/|list_item/"
. -
getAttrs?: (node: HTMLElement) => null | false | Attrs
-
A function used to compute the attributes for the node or mark created by this rule. Can also be used to describe further conditions the DOM element or style must match. When it returns
false
, the rule won’t match. When it returns null or undefined, that is interpreted as an empty/default set of attributes. -
getContent?: (node: Node, schema: Schema) => ProseMirrorFragment
-
Can be used to override the content of a matched node. When present, instead of parsing the node’s child nodes, the result of this function is used.
-
ignore?: boolean
-
When true, ignore content that matches this rule.
-
mark?: string
-
The name of the mark type to wrap the matched content in.
-
namespace?: string
-
The namespace to match. Nodes are only matched when the namespace matches or this property is null.
-
node?: string
-
The name of the node type to create when this rule matches. Each rule should have either a
node
,mark
, orignore
property (except when it appears in a node or mark spec, in which case thenode
ormark
property will be derived from its position). -
preserveWhitespace?: boolean | "full"
-
Controls whether whitespace should be preserved when parsing the content inside the matched element.
false
means whitespace may be collapsed,true
means that whitespace should be preserved but newlines normalized to spaces, and"full"
means that newlines should also be preserved. -
priority?: number
-
Can be used to change the order in which the parse rules in a schema are tried. Those with higher priority come first. Rules without a priority are counted as having priority 50. This property is only meaningful in a schema—when directly constructing a parser, the order of the rule array is used.
-
skip?: boolean
-
When true, ignore the node that matches this rule, but do parse its content.
-
tag: string
-
A CSS selector describing the kind of DOM elements to match.
Attrs
An object holding the attributes of a node.
Type: { [attr]: any }
DOMOutputSpec
A description of a DOM structure. Can be either a string, which is interpreted as a text node, a DOM node, which is interpreted as itself, a {dom, contentDOM}
object, or an array.
An array describes a DOM element. The first value in the array should be a string—the name of the DOM element, optionally prefixed by a namespace URL and a space. If the second element is plain object, it is interpreted as a set of attributes for the element. Any elements after that (including the 2nd if it’s not an attribute object) are interpreted as children of the DOM elements, and must either be valid DOMOutputSpec
values, or the number zero.
The number zero (pronounced “hole”) is used to indicate the place where a node’s child nodes should be inserted. If it occurs in an output spec, it should be the only child element in its parent node.
Type: string | DOMNode | ({ contentDOM?: HTMLElement; dom: DOMNode }) | readonly [string, ...any[]]
ParseRule
A value that describes how to parse a given DOM node or inline style as a ProseMirror node or mark.
Type: TagParseRule | StyleParseRule