Files
pandoc/test/ipynb/mime.ipynb
Kolen Cheung 20eb8ac7fd ipynb writer: handle cell output with raw block of markdown (#7563)
Write RawBlock of markdown in code-cell output.

#7561 makes the ipynb reader reads code-cell output with mime
"text/markdown" to a RawBlock of markdown

This commit makes the ipynb writer writes this RawBlock of markdown
back inside a code-cell output with the same mime, preserving this
information in round-trip

Add tests of ipynb reader (#7561) and ipynb writer (#7563)'s ability to
handle a "text/markdown" mime type in a code-cell output
2021-12-09 20:36:56 -08:00

4.1 KiB

In [1]:
from __future__ import annotations

from dataclasses import dataclass
In [2]:
import IPython

print(IPython.__version__)
7.29.0

Supported IPython display formatters:

In [3]:
ip = get_ipython()
for mime in ip.display_formatter.formatters:
    print(mime)
text/plain
text/html
text/markdown
image/svg+xml
image/png
application/pdf
image/jpeg
text/latex
application/json
application/javascript

Let's write a simple class that will output different mime:

In [4]:
@dataclass
class Mime:
    math: str

    def _repr_mimebundle_(
        self,
        include: Container[str] | None = None,
        exclude: Container[str] | None = None,
        **kwargs,
    ) -> dict[str, str]:
        string = self.math
        data = {
            "text/plain": string,
            "text/html": (latex := f"\\[{string}\\]"),
            "text/markdown": f"$${string}$$",
            # "image/svg+xml":,
            # "image/png":,
            # "application/pdf":,
            # "image/jpeg":,
            "text/latex": latex,
            # "application/json":,
            # "application/javascript":,
        }
        if include:
            data = {k: v for k, v in data.items() if k in include}
        if exclude:
            data = {k: v for k, v in data.items() if k not in exclude}
        return data
In [5]:
mime = Mime("E = mc^2")
In [6]:
mime
Out [6]:
\[E = mc^2\]

Note that #7561 made ipynb reader aware of this, and #7563 made ipynb writer aware of this.