Skip to content

Fix fenced code blocks ending early at a line ending with backticks - #760

Open
nk-batman21 wants to merge 1 commit into
trentm:masterfrom
nk-batman21:fix-fenced-code-closing-fence
Open

nk-batman21 wants to merge 1 commit into
trentm:masterfrom
nk-batman21:fix-fenced-code-closing-fence

Conversation

@nk-batman21

Copy link
Copy Markdown
Contributor

With the fenced-code-blocks extra, a line inside a code block that ends with the opening backticks closes the block there, wherever the backticks sit on the line. This happens whenever the code itself contains a Markdown fence, for example a Python function whose docstring has an indented example:

```
def greet():
    """Say hello.

    ```
    greet()
    ```
    """
```

Some *text* between the blocks.

On master the block ends at the indented ``` line. greet() becomes a separate code block, the real closing fence then opens a new block that swallows "Some text between the blocks.", and code and prose swap places for the rest of the document. A code line such as Wrap code in ``` does the same.

This is a regression from #418, which changed the closing fence from ^```[ \t]*\n to \1[ \t]*\n and dropped the ^. This puts the anchor back and lets the closing fence be indented up to 3 more spaces than the opening fence or be longer than it. Both of those already closed a block by matching partway through the line, and they no longer leave a stray whitespace or blank line at the end of the code. A closing fence indented 4 or more spaces past the opening one no longer closes the block, as in CommonMark.

Ran: make testone on Python 3.9 to 3.14 (304 tests pass) and make testredos; the new fixture fails without the change.
Checked: the docstring example is fixed with Pygments, highlightjs-lang and safe mode too. Fences inside list items, blockquotes, footnotes and admonitions render as before, and the nested fences from #327 still work.

The closing fence pattern was not anchored to the start of a line, so
any line inside a fenced code block that ended with the opening fence
closed the block there. A Python docstring with an indented ``` example
split the block, and the rest of the document swapped code and text.

Require the closing fence to start a line again, as it did before
pull trentm#418. Allow it to be indented up to three more spaces than the
opening fence and to be longer than it, which already closed a block.
@nicholasserra

Copy link
Copy Markdown
Collaborator

Looks reasonable, merge master in or rebase and should be able to merge if passes CI thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants