Skip to content

Fix fenced code blocks for languages like c# and f# - #759

Merged
nicholasserra merged 1 commit into
trentm:masterfrom
nk-batman21:fix-fenced-code-lang-with-hash
Nov 2, 2026
Merged

nicholasserra merged 1 commit into
trentm:masterfrom
nk-batman21:fix-fenced-code-lang-with-hash

Conversation

@nk-batman21

Copy link
Copy Markdown
Contributor

With the fenced-code-blocks extra, a fence whose language name contains # or . is not treated as a fence at all. c#, f# and vb.net are Pygments lexer names, and GitHub highlights a ```c# block as C#.

For example:

```c#
#region Setup
var x = 1;
#endregion
```

Some *text* here.

```python
print('hi')
```

On master this renders <p>```c#</p>, then <h1>region Setup</h1> and <h1>endregion</h1>. The closing fence of the C# block then opens a code block that swallows "Some text here." and the ```python line, so code and prose swap places for the rest of the document. This happens with or without Pygments installed.

This adds # and . to the characters allowed in the language name in fenced_code_block_re. A fence with words after the name (```c# title) is still not a fence, and a block with no language whose first line starts with # still gets no language (#396).

Ran: make testone on Python 3.9 and 3.14 (303 tests pass); the new fixture fails without the change.
Checked: ```c# with highlightjs-lang, with safe mode, and inside a list item and a blockquote now renders the same way as ```python.

The language name after an opening fence only allowed word characters,
`+` and `-`, so a fence such as ```c#, ```f# or ```vb.net was not
treated as a fence. Its code was parsed as Markdown (C# `#region` lines
became headers) and the closing fence opened a new code block.

Also allow `#` and `.`, which Pygments uses in lexer names.
@nk-batman21
nk-batman21 force-pushed the fix-fenced-code-lang-with-hash branch from 2d1973e to fc0ddb0 Compare November 1, 2026 23:09
@nicholasserra

Copy link
Copy Markdown
Collaborator

Thanks!

@nicholasserra
nicholasserra merged commit cecee4f into trentm:master Nov 2, 2026
32 of 36 checks passed
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