mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.64k stars 386 forks source link

Multi-line metadata comments broken in RMarkdown #1029

Open matthew-brett opened 1 year ago

matthew-brett commented 1 year ago

Consider the following RMarkdown notebook:

---
jupyter:
  jupytext:
    text_representation:
      extension: .Rmd
      format_name: rmarkdown
---

Some text.

<!-- #region {"some_meta": "yes", "more_meta": "affirmative"} -->
Region cell with one-line header comment.
<!-- #endregion -->

More text.

<!-- #region {"some_meta": "yes",
"more_meta": "affirmative"} -->
Region cell with multi-line header comment.
<!-- #endregion -->

Notice it has two near-identical cell-delimiting HTML comment blocks, where one has a leading comment marker that is all on one line, and the second has the same comment marker, but split over two lines. Jupytext parses this to ipynb as:

{'nbformat': 4,
 'nbformat_minor': 5,
 'metadata': {'jupytext': {'text_representation': {'extension': '.Rmd',
    'format_name': 'rmarkdown'},
   'main_language': 'python',
   'cell_metadata_json': True}},
 'cells': [{'id': '121bf246',
   'cell_type': 'markdown',
   'source': 'Some text.',
   'metadata': {}},
  {'id': '9884f382',
   'cell_type': 'markdown',
   'source': 'Region cell with one-line header comment.',
   'metadata': {'some_meta': 'yes', 'more_meta': 'affirmative'}},
  {'id': '7d1350d3',
   'cell_type': 'markdown',
   'source': 'More text.\n\n<!-- #region {"some_meta": "yes",\n"more_meta": "affirmative"} -->\nRegion cell with multi-line header comment.\n<!-- #endregion -->',
   'metadata': {}}]}

Notice that the one-line HTML comment cell does get parsed as its own cell with metadata, but the two-line HTML comment cell does not, and remains as an HTML comment inside an embedding Markdown cell.

The one-line and two-line HTML header comments seem syntactically identical, and my text editor will insert line-breaks automatically in long comments, so it would be a great help if the RMarkdown -> ipynb parser would read both comments as valid meta-data specifiers for new cells. Is that practical?

I suppose may be related to https://github.com/mwouts/jupytext/issues/1012

mwouts commented 1 year ago

Hi @matthew-brett , thank you for suggesting this! I've not had much time lately, but when possible I will see if I can do something about this. Thanks