Giter VIP home page Giter VIP logo

prosemirror-py's Introduction

prosemirror-py

CI Code Coverage Python Version PyPI Package License Fellow Careers

This package provides Python implementations of the following ProseMirror packages:

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. The code has type annotations to support mypy or other typechecking tools.

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!'
        }]
    }]
}

prosemirror-py's People

Contributors

sciyoshi avatar ericls avatar dependabot-preview[bot] avatar dependabot[bot] avatar ernesto-at-fellow avatar p7g avatar tktech avatar johanneswilm avatar dennmat-hurdle avatar cpmsmith avatar mattmikolay avatar elgow avatar brechtm avatar gantoine avatar jsonn avatar smoores-dev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.