0

Prologue

It seems this is a duplicate of an almost 6 year request. Fair enough, though I think this question adds new value (e.g. the <pre><code></code></pre>+< comparison format mess-up).

This 5 years comment got my attention:

+1 This fix is super important. So annoying to have to copy to an external editor to get the correct indentation. – Andrew Mao Sep 18 '13 at 20:14

Just as well it's super important.

By the way, thanks to that request, I found the only bearable workaround.

The original beginning

I want to write a series of numbered steps, one of which involves some code and I want the code aligned with the list. As stated in this answer:

Unfortunately, this is an ambiguity in the Markdown specification.

Bottom line is: As sad as it is, this is unfixable.

OK, then. Warning: from now, I'll invert usual edit convention to avoid adding a quote level that might interfere with the explanation.





Let's say I'm writing an answer. First, I write...

Follow these steps:

  1. Copy this code in a .foo file.

Now I open my file, copy the code and paste it. It's maybe 20 lines, but I'll represent them with (...). Then you get:

Follow these steps:

  1. Copy this code in a .foo file.

First line of code (...) result = number < 2 ? x+3 : y-1; (...) Last line of code

Of course, this is not the intended result, so I select my piece of code and click the curly-brackets icon:

Follow these steps:

  1. Copy this code in a .foo file.

    First line of code (...) result = number < 2 ? x+3 : y-1; (...) Last line of code

No, no, no... This is not what I meant either. Maybe there's an ambiguity in the Markdown specification but, what I wanted, I made it clear clicking that icon: I wanted it to be code. The editor took an unambiguous action, added four spaces to every line, turned it into an ambiguous syntax and chose to render it the wrong way. I clearly see this as a bug. It would have been an ambiguous case if I would have added the spaces by hand but not clicking the Code Sample icon.

The editor should have taken account of the list and added eight spaces (better yet, 4*(levels+1) spaces), that is the advanced help/Code answer to this FAQ.

  1. Copy this code in a .foo file.

    First line of code
    (...)
                result = number < 2 ? x+3 : y-1;
    (...)
    Last line of code
    

What am I supposed to do? Add 4 extra spaces by hand to every line? Open a code editor and add extra levels of indentation, then copy from there? (In the previous example it's easy but with a not so long code or if you edit it afterwards, it's a real PITA.) Maybe the editor thinks I'm writing the code live so I'll write the needed spaces too. What if I am writing an answer like this?

The hacks I found don't work well. You can use: <!-- --> but doesn't add indentation.

  1. Copy this code in a .foo file.
First line of code
(...)
            result = number < 2 ? x+3 : y-1;
(...)
Last line of code

Or you can add <pre><code></code></pre>. You'll have to Edit to see the raw of this part but it's worth it.

  1. Copy this code in a .foo file.
    First line of code
    (...)
                result = number 

Oops! You have code and is indented, but as I used a < comparison (who would expect that in a piece of code?), the format crashed. And it didn't recover until the word "comparison", don't ask me why. Note what a mess. If you are reading the raw of this part you'll see nothing exotic.


Back to normal.





Compare the last with pictures: Unexotic raw

Messy paragraph





tl;dr: the hacky fixes to avoid adding 4x spaces to every line of code don't actually work. The last one f*cks the format up so badly that I won't dare to add backquotes since then (edit the question, add a backquoted word to this paragraph and enjoy the fun). And it doesn't work well, since the code is indented, not the gray box.

BUG: When curly-brackets icon is clicked inside a list, it should not be the user business how the editor manages to make it appear the selected text as indented code aligned with the list item, and it's possible. Don't change the markdown specification, don't change the rendering engine: just change the code-sample/Ctrl+K/curly-brackets behavior to take account of the context.

FEATURE: What about add/remove code indentation buttons?

PS: Revising the question some part may seem harsh. Sorry. Not intended at all.

7
  • I sometimes use nonbreakable spaces to force formatting that isn't otherwise available. Unicode U+00A0. Jul 19, 2018 at 13:58
  • Though this is a question about Do you type it or write &nbsp;?
    – cdlvcdlv
    Jul 19, 2018 at 14:48
  • I use AutoHotkey so I can just type <Ctrl><Space>. You can also use 00A0<Alt><X> in Word, and then copy and paste the character . . . Jul 19, 2018 at 15:42
  • Right after the answer you're quoting is another answer that says you need 8x spaces for proper code formatting and indentation in a list.
    – muru
    Jul 20, 2018 at 1:40
  • I know, read again: «The editor should have taken account of the list and added eight spaces (better yet, 4*(levels+1) spaces), that is the advanced help/Code answer to this FAQ». Just after that, I show what you're pointing at, i.e., how you can do it by hand, which shouldn't be necessary (not to mention the nuisance). The point of this question is: it's not me which needs 8 spaces but the editor: «... change the code-sample/Ctrl+K/curly-brackets behavior to take account of the context».
    – cdlvcdlv
    Jul 20, 2018 at 8:02
  • 2
    All of that could have been conveyed in a tenth of this post.
    – muru
    Jul 20, 2018 at 8:36
  • If you write little, you get a lot of futile objections. If you get ahead of them, someone will tell you are writing too much.
    – cdlvcdlv
    Jul 20, 2018 at 8:50

0