Allow nested code blocks / allow triple backticks in code blocks rendering result

Use case or problem

Can’t nest a code block inside a code block, the render result is glitchy

image

The only real use case I could think of (also the situation I ran into) is in some markdown syntax tutorial where you want to show the code and render result side by side:
image

but theoretically speaking, in a code block anything should be treated as a part of the code, and should not interfere with the render result. For now, the only language I know that is using the triple backtick in the syntax is markdown, but it might be the case that:

  1. someone might want a triple backtick in their comments or strings

    image

  2. some other languages may implement this in the future. for example, Golang already uses a pair of single backticks to mark a raw string, while Python uses triple quotation marks, so it would not be surprising. If some languages decide to use triple backtick as a part of normal syntax in the future, this would be really frustrating.

Proposed solution

As far as I know, there is no standard for code block implementation in markdown. Currently, the only syntax required is a pair of triple backticks (in different lines), the former marks the start, and the latter marks the end. So by this grammar, there can’t be any triple backticks in between, which means there can’t be them in the code block.

But the user might want to include triple backticks in their rendering result, hence I propose some work around here:

  1. Add a way to escape the triple backtick, for example, use slash before the backtick.
    image
    this should be rendered as normal triple backticks inside the code block, as in
    markdown the render result is what really matters.

  2. Allow the user to optionally mark the start and the end for the code block, in a way this is similar to how raw string is implemented in cpp
    image
    the above syntax is to optionally add a marker after the language code, and the
    the renderer will treat everything as a part of the code block including triple backticks
    until the comment contains the same marker. The last triple backtick is not included in
    the render result.

    for backward compatibility, the end mark could be on the next line of the
    latter triple backticks, since currently if anything is on the same line it will be treated as
    a start point and the render result will be wrong. Or maybe it’s not needed since the
    current rendering is glitchy anyway and we just implement a working solution.

There could be other ways to implement this feature, the core idea here is to make it possible to include triple backticks in a glitchless rendering result.

Current workaround (optional)

don’t nest or tolerate the glitchy result

Related feature requests (optional)

related to a bug report by me

The syntax highlighting is off, but would using ~~~ inside a code block work for now?

```markdown
# title

python code:
~~~python
print("hello world")
~~~
```

That, with a note below, says

The tildes inside the code block stand for backticks, due to markdown currently doesn’t support nested code blocks

It is a workaround for now, you can consider it as a subclass of “don’t nest” or as a standalone method “alternate symbol”, it works but it’s not perfect.

A code block fence is three or more backticks or tildes, so as long as you’re varying what you’re using as the fence, there is no issue nesting code blocks.

E.g. I sometimes use 5 backticks to show examples using 4 backticks around markdown and queries using the normal 3 backticks.

2 Likes

omg it worked

my bad I never thought of reading documents outside the Obsidian help page

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.