Python implementation of core ProseMirror modules for collaborative editing
Project description
prosemirror-py
This package provides Python implementations of the following ProseMirror packages:
prosemirror-model
version 1.18.1prosemirror-transform
version 1.7.1prosemirror-test-builder
prosemirror-schema-basic
version 1.1.2prosemirror-schema-list
The original implementation has been followed as closely as possible during translation to simplify keeping this package up-to-date with any upstream changes.
Why?
ProseMirror provides a powerful toolkit for building rich-text editors, but it's JavaScript-only. Until now, the only option for manipulating and working with ProseMirror documents from Python was to embed a JS runtime. With this translation, you can now define schemas, parse documents, and apply transforms directly via a native Python API.
Status
The full ProseMirror test suite has been translated and passes. This project only supports Python 3. There are no type annotations at the moment, although the original has annotations available in doc comments.
Usage
Since this library is a direct port, the best place to learn how to use it is the official ProseMirror documentation. Here is a simple example using the included "basic" schema:
from prosemirror.transform import Transform
from prosemirror.schema.basic import schema
# Create a document containing a single paragraph with the text "Hello, world!"
doc = schema.node("doc", {}, [
schema.node("paragraph", {}, [
schema.text("Hello, world!")
])
])
# Create a Transform which will be applied to the document.
tr = Transform(doc)
# Delete the text from position 3 to 5. Adds a ReplaceStep to the transform.
tr.delete(3, 5)
# Make the first three characters bold. Adds an AddMarkStep to the transform.
tr.add_mark(1, 4, schema.mark("strong"))
# This transform can be converted to JSON to be sent and applied elsewhere.
assert [step.to_json() for step in tr.steps] == [{
'stepType': 'replace',
'from': 3,
'to': 5
}, {
'stepType': 'addMark',
'mark': {'type': 'strong', 'attrs': {}},
'from': 1,
'to': 4
}]
# The resulting document can also be converted to JSON.
assert tr.doc.to_json() == {
'type': 'doc',
'content': [{
'type': 'paragraph',
'content': [{
'type': 'text',
'marks': [{'type': 'strong', 'attrs': {}}],
'text': 'Heo'
}, {
'type': 'text',
'text': ', world!'
}]
}]
}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for prosemirror-0.3.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b29282387c3d551f15fac08db57c635bbfe55a1a0f26231d3982555eb3c6ffd2 |
|
MD5 | ea6080c1c603cb4302f86eece44c397d |
|
BLAKE2b-256 | d6f56ff5df8b32d487a0a1e7bbc1ed990309958b0ba215fcd5ec9bd929791f2f |