What is the correct way to add a code block formatted in a certain language in a list item with proper indentation(have the code block indented with respect to the list item it belongs to. This is what I tried:
Try putting normal text on the same line as the list marker, then a blank line, and then the code block indented 2 spaces plus the indentation level of the list item. It looks to me like this works, but I didn’t test extensively and I don’t remember the content of prior discussions of the issue. I had to correct the indentation of the autocompleted final row of backticks.
This is based on the rules of CommonMark for multi-line list items, which I don’t fully understand: CommonMark Spec
The original Markdown spec simply says that subsequent paragraphs in a list item are indented a level, and code blocks 2 levels (because a code block is normally indented 1 level). Daring Fireball: Markdown Syntax Documentation
Original Markdown doesn’t have code blocks that are “fenced” with backticks and so doesn’t address that, but simply indenting the code block under the list item (with a blank line between) per this rule also seems to work.
you are seeing the below box because the first line of your code block ```js has spaces at the start. If you remove them to be at the start it will disappear.
FYI this is only because you have a bullet point before the code block. for some reason not showing after.
The trick to getting code block to align with indentation level is to completely write it on the base level, and then insert it using f.ex. tabs to the proper indentation level. This needs to be done for the start marker, the actual code, and for the end marker.
Especially if you skip indenting the end marker of your code block, things looks wonky.
The example above, shows code block properly aligned from first through fourth level of indentation. Source mode to the left, reading view to the right.
Tabs = tabulator, I’ve just hit the tabulator key in front of the various lines in the code blocks.
The same effect can be achieved inserting the spaces in front of every line in the code block. The key being: add the same amount of white space characters in front of every line.
Once one line, like the end line, has a different amount of space in front of it the extra lines start appearing, and misalignment occurs.