Code Block Indentation

Use case or problem

  • The ability to indent code blocks.
  • For example, I’m trying to have a code block indented under a bullet point, clearly indicating that it pertains to that bullet point.
  • Right now, a code block always spans across the width of the whole document.

Proposed solution

  • When “```” is typed, start the code block from that position (Ex: 2 indents inside).
  • Also, perhaps, be able to have the cursor on the “outside” of the code block so that we can indent the code block as we wish (just like how the bullet points work).
6 Likes

I think using pure HTML should work as a workaround (perhaps an unsatisfying one):

<ul>
<li>Not code</li>
<li>
<pre><code>Start of the code

More code

End of the code</code></pre>
</li>
<li>Also not code</li>
</ul>

This works in Live Preview, but Reading View garbles it. I’m not sure if it supports language highlighting (and if so, how to trigger it).

1 Like

Thanks for the suggestion. Though, as you mentioned, it’s a bit unsatisfying. :smiling_face_with_tear:

Are you sure this doesn’t work? Works for me.

I indent twice (in my case it’s 4 spaces) and, in both Live Preview and Reading View, the code block is:

  1. properly indented under the bullet point
  2. numbered lists continue as expected

screenshot.2022-09-25T094111Z

screenshot.2022-09-25T094118Z

2 Likes

There’s similar feature request. Pasting here for cross reference. Also just to add, it’s a workable solution to indent but visually not consistent especially if u have indentation guide/line

3 Likes

Thanks for the comment. Yea so, I am able to achieve code block indentation. But with some visual inconsistencies.

My Live Preview

My Reading View

It would be nice if for the live preview, the code block’s left side starts at the indentation (as mentioned in the issue you referenced).

And as it gets nested more and more, the indentation seems to increase visually in live preview. The code (//) start becomes more distant in comparison to the bullet point text.

3 Likes

I think a fundamental issue with this is how a code block’s Markdown syntax is usually multiline, i.e.,

  • ```
    line 1
    line 2
    ```

But a bullet point is treated as everything after a - or * on that one line. So how does the note parser make a multiline syntax object like code block, blockquote or callout adhere to one bullet point? does each ‘line’ of the code block have its own bullet point at the same indent level? Seems wrong. But if you don’t do this, then is the Markdown incorrect now?

I’m really not sure about this, perhaps this is one advantage of a format like org-mode over Markdown… Unless Obsidian and other Markdown tools are just smart enough to find the ending ``` or > or whatever ending multiline syntax there is and treat all of that as under the bullet point (but I don’t think they are, unlike the indented preview in this post’s Markdown).

List items can contain multiple lines — they just need to be indented correctly (which can be tricky). I’m still not sure how a code block with no preceding paragraph would work in a list item (especially in original Markdown which lacks fenced blocks), but FYI.

+1 this is an important issue to fix