ChatGPT: Use longer Markdown fences so code blocks don’t break

PROBLEM:

The ChatGPT UI often wraps code blocks in triple backticks (e.g. ```markdown or ```csharp). When the content of that block also contains triple backticks (```), the inner fence prematurely closes the block and the rendering breaks on the chat page.

SOLUTION:

Markdown allows fences of three or more backticks. So using a longer fence avoids collisions with backticks inside the content.

Have ChatGPT automatically choose a fence length that exceeds any backtick run inside the block. Concretely:

  • If a code block’s content contains ``` then wrap the block with four backticks (````lang).
  • If it contains ```` then use five, and so on.

You could encode this as a system instruction like:

“When emitting fenced code, set the fence length to 1 + the longest backtick run appearing in the code block. Preserve any language info string. If the info string would contain backticks, switch to a tilde fence of appropriate length.”

example (markdown uses quad quotes to contain a JSON code block):

Markdown block use quad quotes in order not to break after JSON

JSON inside markdown:

```json
{ "param": "value" }
```

This small change would prevent broken formatting and aligns with CommonMark/GFM rules for fenced code blocks.

1 Like