Skip to main content

A JSON renderer for the Mistune Markdown parser

Project description

Mistune JSON

A JSON renderer for the Mistune Markdown parser.

Supported HTML elements

So far, the HTML elements supported by this renderer are limited to:

  • Paragraph, <p>
  • Image, <img>
  • Ordered lists, <ol>
  • Unordered lists, <ul>
  • All heading elements, <h1> through <h6>
  • Links, <a>
  • Emphasis, <em>
  • Strong, <strong>
  • Blockquote, <blockquote>
  • Line breake, <br>
  • Thematic breake, <hr>
  • Both inline and block code

How to use the JSON renderer

Installation

The package is published in PyPI, so it can be installed through pip (as any other package):

pip install mistune-json

Usage

import mistune
from mistune_json import JsonRenderer

# Create a renderer instance
renderer = JsonRenderer()

# Create a Markdown parser with the JSON renderer
markdown = mistune.create_markdown(renderer=renderer)

# Parse Markdown text
result = markdown("# Hello, world!")

print(result)
# {'content': [{'type': 'h', 'content': [{'type': 'text', 'content': 'Hello, world!'}], 'level': 1}]}

Output Schema

The JSON output is a dictionary with a content key containing a list of nodes. Each node has a type field identifying its kind.

Node Types

Type Description Fields
text Plain text content content (str)
p Paragraph content (list of inline nodes)
h Heading (h1-h6) content (list), level (int 1-6)
code Code block (fenced) content (str), lang (str, optional)
codespan Inline code content (str)
blockquote Block quote content (list of inline nodes)
ol Ordered list content (str), start (int, optional)
ul Unordered list content (str)
a Link content (str), href (str), title (str, optional)
img Image src (str), alt (str), title (str, optional)
em Emphasis (italic) content (str)
strong Strong (bold) content (str)
hr Thematic break (horizontal rule) -
br Line break -

Example

Input:

# Title

This is a paragraph with **bold** and *italic* text.

![alt text](image.png)

- Item 1
- Item 2

Output:

{
  "content": [
    {"type": "h", "content": [{"type": "text", "content": "Title"}], "level": 1},
    {"type": "p", "content": [
      {"type": "text", "content": "This is a paragraph with "},
      {"type": "strong", "content": "bold"},
      {"type": "text", "content": " and "},
      {"type": "em", "content": "italic"},
      {"type": "text", "content": " text."}
    ]},
    {"type": "img", "src": "image.png", "alt": "alt text"},
    {"type": "ul", "content": "..."}
  ]
}

Limitations

The following elements are not yet supported:

  • Tables (headers and rows)
  • Definition lists
  • Task lists

Extensibility

You can subclass JsonRenderer to customize the JSON output by overriding two hooks:

create_node(node_type, data)

Called for every node before it's returned. Override to add custom fields to nodes.

from mistune_json import JsonRenderer

class CustomRenderer(JsonRenderer):
    def create_node(self, node_type, data):
        node = super().create_node(node_type, data)
        node["source"] = "mistune-json"  # Add source to all nodes
        return node

finalize_output(output)

Called after all content is rendered. Override to transform the final output.

from mistune_json import JsonRenderer

class CustomRenderer(JsonRenderer):
    def finalize_output(self, output):
        output["meta"] = {"generated_by": "my-app"}
        return output

Full Example

import mistune
from mistune_json import JsonRenderer
from datetime import datetime

class CustomRenderer(JsonRenderer):
    def create_node(self, node_type, data):
        node = super().create_node(node_type, data)
        node["rendered_at"] = datetime.now().isoformat()
        return node
    
    def finalize_output(self, output):
        output["meta"] = {"version": "1.0"}
        return output

renderer = CustomRenderer()
markdown = mistune.create_markdown(renderer=renderer)
result = markdown("# Hello")

# Each node now has 'rendered_at', and output has 'meta'
print(result)

Contributing and Feedback

Contributions are welcome! Feel free to open issues for:

  • Enhancements - Request new features or supported elements
  • Bugs - Report problems with the JSON output structure

Repository: https://github.com/fernandonino/mistune-json

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

mistune_json-0.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

mistune_json-0.2.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file mistune_json-0.2.0.tar.gz.

File metadata

  • Download URL: mistune_json-0.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Darwin/24.6.0

File hashes

Hashes for mistune_json-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8cfb6944c8a66fbb6fe8f2954ac3d451a6af9d62164e896121b46a664ed081eb
MD5 0ee5e66adf650fd325a6ea2a9edd143b
BLAKE2b-256 a97370b13c971ee98730d2fed9cf50b998fdb4f5d065bbdf99203a2b57d57332

See more details on using hashes here.

File details

Details for the file mistune_json-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mistune_json-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Darwin/24.6.0

File hashes

Hashes for mistune_json-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f433ef5cca387ba45f27118f1ccffaf2247e8164eec334028d7013c28a7326b
MD5 ff56c169f6d0b1d4e716462131f981ab
BLAKE2b-256 559c36253497c4b918b50a76acf72f4e9fe98352381f3a52e6f11786c073bf3a

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