Correct way to align code block inside a list item

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:

I get a strange empty block in reading mode.

Have you searched the forum? I’m pretty sure this has come up before but I don’t recall if there’s a solution.

Yes, I did not find an explanation. Am I doing something wrong? is this a bug?

I think I have same issue, I’m looking for a way to get code blocks in list and it just doesn’t seem to work at all.

Single line codes are fine

image

But code block just breaks things

image

Seems like I’d have to remove the list and insert the code block but that’s not a solution, because it’s not indented

image

Example of how I would expect it to work

This seems like very basic functionality, so I guess I just don’t know how to make this work.

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.

1 Like

This is what I get in Edit View

image

And this is Read View

image

It’s still completely messed up :frowning:

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.

1 Like

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.

4 Likes

This looks great. But what is f.ex. tabs ?

1 Like

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.

2 Likes

Oh, I thought f.ex. tabs is some plug-in or something… and it’s abbreviation for for example :smiley:

This works, thank you very much!

imageimage

yes! that did it! no idea what is “f.ex” but I just put everything as outdented as possible and then indented. Thanks so much!

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