jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

Code cells with tildes and double newline breaks .md to .ipynb conversion

Open edublancas opened this issue 4 years ago • 4 comments

Hi, I realized that using tildes and double newline for markdown breaks conversion to ipynb. I'm using jupytext 1.13.0.

Example (double newline between import and def):

~~~python
import math


def x():
    pass
~~~

Generates ipynb:

{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1ef9d2ed",
   "metadata": {},
   "source": [
    "~~~python\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6384c75c",
   "metadata": {},
   "source": [
    "def x():\n",
    "    pass\n",
    "~~~"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

This does not happen if I remove the double newline. It also works fine if I use backticks.

Attaching files to reproduce the error and the output ipynb files: jupytext.zip

edublancas avatar Oct 12 '21 03:10 edublancas

Hi @edublancas , yes sorry about that, Jupytext does not recognize code cells with tildes. For this reason you get a first markdown cell. And then, in the plain Jupytext Markdown format, two blank lines are understood as a Markdown cell break, so you get a second markdown cell...

So... may I ask if you have a reason to prefer ~~~python over ```python?

If so, did you try the md:myst format? Tildes might work better there (keep us posted!)

mwouts avatar Oct 12 '21 17:10 mwouts

I'm using tildes because the docs mention that you can use them if you don't want Jupyter to recognize them as code cells (I have some .md files where some snippets are just informative, not meant to be executed). The docs suggest a few ways to do this and using tildes seemed like the cleanest one. In the end, I used backticks and the <!-- #md --> markers.

That's why I thought it was a bug, but if that's the intended behavior maybe remove the suggestion to use tildes in the docs? (happy to open a PR)

edublancas avatar Oct 12 '21 17:10 edublancas

I'm using tildes because the docs mention that you can use them if you don't want Jupyter to recognize them as code cells

Oh sure, I see! Thanks for confirming. I'll have a look and see if I can write a fix for this.

mwouts avatar Oct 12 '21 19:10 mwouts

Awesome, thanks!

edublancas avatar Oct 12 '21 20:10 edublancas