Skip to main content

Python implementation of converting ProseMirror doc to markdown with TaskList support

Project description

prosemirror-py-markdown

中文指引

Python implementation of converting ProseMirror doc to markdown with TaskList support

Forked from prosemirror-py-converter with additional features and improvements.

Core features

  • Based on ProseMirror basic model
  • Support custom extension
  • ProseMirror document converter: Json to Markdown in ProseMirror format
  • ✨ TaskList support - Convert ProseMirror task lists to markdown checkboxes
  • Improved node rendering logic for better markdown output
  • Mainly used for data analysis and display after spider ProseMirror documents

Quick start

  • Install prosemirror-py-markdown
pip3 install prosemirror-py-markdown
  • Convert ProseMirror document to Markdown
from pmconverter import prose2markdown

doc = {
     "type": "doc",
     "content": [
         {
             "type": "heading",
             "attrs": {
                 "level": 3
             },
             "content": [
                 {
                     "type": "text",
                     "text": "Example heading."
                 }
             ]
         }
     ]
}
print(prose2markdown(doc))
  • output markdown
### Example heading.

TaskList Example

from pmconverter import prose2markdown

doc = {
    "type": "doc",
    "content": [
        {
            "type": "task_list",
            "content": [
                {
                    "type": "task_item",
                    "attrs": {"checked": False},
                    "content": [
                        {"type": "text", "text": "Unchecked task"}
                    ]
                },
                {
                    "type": "task_item",
                    "attrs": {"checked": True},
                    "content": [
                        {"type": "text", "text": "Checked task"}
                    ]
                }
            ]
        }
    ]
}
print(prose2markdown(doc))

Output:

- [ ] Unchecked task
- [x] Checked task

Standard ProseMirror implementation

mark type list

  • link
  • bold
  • strong
  • code
  • italic
  • strike
  • subscript
  • superscript
  • underline

node type list

  • doc
  • heading
  • paragraph
  • image
  • bullet_list
  • ordered_list
  • task_list ✨ (new)
  • task_item ✨ (new)
  • table
  • blockquote
  • code_block
  • hard_break
  • horizontal_rule

Custom ProseMirror extension examples

iwiki doc converter

  • custom mark converter example
from pmconverter.basic_model import CommonSimpleMark
from pmconverter.model_factory import register_mark_class


class CustomMark(CommonSimpleMark):

    def __init__(self):
        super().__init__()
        self.type = "custom_mark"
        self.md_pre_mark = "<u>"
        self.md_after_mark = "</u>"


register_mark_class("custom_mark", CustomMark)
  • custom node converter example
from pmconverter.basic_model import Node
from pmconverter.model_factory import register_node_class


class CustomeImage(Node):

    def __init__(self):
        super().__init__()
        self.type = "custom_image"

    def convert_to_markdown(self, **kwargs) -> str:
        name = self.get_attr("name", "")
        url = self.get_attr("url", "")
        return f"![{name}]({url})"
        

register_node_class("custom_image", CustomeImage)

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

prosemirror_py_markdown-1.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prosemirror_py_markdown-1.0.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file prosemirror_py_markdown-1.0.1.tar.gz.

File metadata

  • Download URL: prosemirror_py_markdown-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for prosemirror_py_markdown-1.0.1.tar.gz
Algorithm Hash digest
SHA256 acceaf814af22f14fc3da08ae23ffce31994b0325cc6b770c60d98de25412ace
MD5 82d5b207fecf604dfa476ba4f1881b90
BLAKE2b-256 c68cb37d89c3988e768503ad6bff1d9a3a4c00adf76c898d528939db5765c629

See more details on using hashes here.

File details

Details for the file prosemirror_py_markdown-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for prosemirror_py_markdown-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e56a6ad07b5a41db01e2f7a4acf02dee559ab98c039eae5fc46f3fcf2e8725b
MD5 a43762a4ebefffc2a83b28eb4221edee
BLAKE2b-256 ec8150c07d1d9718c1db51ead79188f915644c9dfa8b5134b743d71dc3703fec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page