Repository: mwouts/jupytext

Issue:
Bibliography section becomes code
This is really to ask for advice about a problem I have with jupyter-book files.

For some files, I want to list ciations.  The obvious way to do this, is to include a version of this Markdown cell at the end of the page (RMarkdown format) - see https://jupyterbook.org/en/stable/content/citations.html#local-bibliographies

~~~
```{bibliography}
```
~~~

However, Jupytext interprets this text as a code cell of language `bibliography`, so running `jupytext --to Rmd` on a file with this cell, results in:

~~~
```{bibliography language="bibliography"}

```
~~~

In the notebook interface, this also appears as a code cell.

Do you have any advice for avoiding this behavior?

Thanks as ever.

Discussion:
github-actions[bot]: Thank you for making this pull request.

Did you know? You can try it on Binder: [![Binder:lab](https://img.shields.io/badge/binder-jupyterlab-0172B2.svg)](https://mybinder.org/v2/gh/mwouts/jupytext/fix_bibliography_in_rmarkdown?urlpath=lab/tree/demo/get_started.ipynb) or [![Binder:notebook](https://img.shields.io/badge/binder-notebook-0172B2.svg)](https://mybinder.org/v2/gh/mwouts/jupytext/fix_bibliography_in_rmarkdown?filepath=demo).

Also, the version of Jupytext developed in this PR can be installed with `pip`:
```
pip install git+https://github.com/mwouts/jupytext.git@fix_bibliography_in_rmarkdown
```
(this requires `nodejs`, see more at [Developing Jupytext](https://jupytext.readthedocs.io/en/latest/developing.html))
<!-- thollander/actions-comment-pull-request "binder_link" -->

Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 45e8a50..2498ce1 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -11,7 +11,10 @@ on:
     paths-ignore:
       - "CHANGELOG.md"
     branches: [main]
-  pull_request:
+  # We use pull_request_target rather than pull_request
+  # because the latter does not let us comment on PR
+  # coming from a fork.
+  pull_request_target:
     branches: [main]
   schedule:
     - cron: "0 11 * * 4"
diff --git a/.github/workflows/step_comment-pr.yml b/.github/workflows/step_comment-pr.yml
index 38d06b5..b3d8a52 100644
--- a/.github/workflows/step_comment-pr.yml
+++ b/.github/workflows/step_comment-pr.yml
@@ -8,7 +8,7 @@ jobs:
   comment-pr:
     runs-on: ubuntu-latest
     name: Comment PR
-    if: github.event_name == 'pull_request'
+    if: github.event_name == 'pull_request_target'
     steps:
       - name: Checkout
         uses: actions/checkout@v3
diff --git a/src/jupytext/cell_reader.py b/src/jupytext/cell_reader.py
index 738cd24..ba09b83 100644
--- a/src/jupytext/cell_reader.py
+++ b/src/jupytext/cell_reader.py
@@ -443,9 +443,12 @@ class MarkdownCellReader(BaseCellReader):
                         return i - 1, i, False
                     return i, i, False
 
-                if self.start_code_re.match(line) and not line.startswith(
-                    "```{bibliography}"
-                ):
+                if self.start_code_re.match(line):
+                    if line.startswith("```{bibliography}"):
+                        in_explicit_code_block = True
+                        prev_blank = 0
+                        continue
+
                     # Cells with a .noeval attribute are markdown cells #347
                     _, metadata = self.options_to_metadata(
                         self.start_code_re.findall(line)[0]


Test patch:
diff --git a/tests/functional/simple_notebooks/test_read_simple_rmd.py b/tests/functional/simple_notebooks/test_read_simple_rmd.py
index 56958a1..0a15cf6 100644
--- a/tests/functional/simple_notebooks/test_read_simple_rmd.py
+++ b/tests/functional/simple_notebooks/test_read_simple_rmd.py
@@ -274,10 +274,16 @@ become a code cell
 
 ```{bibliography}
 ```
+
+```{r}
+6
+```
 """,
 ):
     nb = jupytext.reads(rmd, fmt="Rmd")
-    assert len(nb.cells) == 1, nb.cells
+    assert len(nb.cells) == 2, nb.cells
     assert nb.cells[0].cell_type == "markdown"
+    assert nb.cells[1].cell_type == "code"
+    assert nb.cells[1].source == "6"
     rmd2 = jupytext.writes(nb, fmt="Rmd")
     compare(rmd2, rmd)