19

I've just answered a question in SO where I had to post four snippets of XML (yes, it was a JavaEE question, how did you guess?)

I made an effort to keep the snippets short and meaningful, but all this code can clutter the visuals of the page, and moves attention away from the rest of the answer.

The site can already detect which parts of the question or answer are code, and paints them in a different font and background. What if it could also add a small toggle button that hides/display each snippet of code?

An example. Displayed: A crudely drawn image of a widget whose purpose is to collapse a block of code, complete with an arrow pointing to this widget and a description "button to hide snippet" of said widget.

Hidden: The same widget as before, only this time it is in a different state and the associated block of code has been truncated according to the requested feature's behaviour. An arrow continues to point at the widget but it is accompanied by another arrow pointing out that an ellipsis (...) symbol should be used to indicate the aforementioned truncation has taken place.

Please acknowledge my superior Photoshop skillzs.

6
  • It's a bit of a clunkily edited question, but this proposal which asks for code collapse (and then some) has been denied.
    – Grace Note StaffMod
    Commented Nov 24, 2010 at 19:26
  • @26: you've inflicted superfluous "u"s on blind people yet again! Why do you hate the blind so?
    – Shog9
    Commented Nov 24, 2010 at 20:16
  • 1
    @shognine: I appeal to only the most Canadian of blind people. The blind people of lesser countries will have to make do.
    – Welbog
    Commented Nov 24, 2010 at 20:17
  • @26: Aye. A thousand apologies. I promise I'll be more careful next time.
    – Leonel
    Commented Nov 24, 2010 at 20:36
  • 1
    Aside: the site does not "detect" code, it provides a formatting option for code (well, two one for inline and one for blocks). Commented Nov 24, 2010 at 21:12
  • See also: meta.stackoverflow.com/questions/302078/collapsible-code-markup
    – JohnLBevan
    Commented Nov 24, 2016 at 18:48

1 Answer 1

20

For the record, this now exists (at least for some languages).

<!- begin snippet: js hide: true -->

<!-- language: lang-js -->

alert(2+40);

<!-- end snippet -->

Note: <!- begin should actually be <!-- begin, but because of the way HTML-comments work, doing so would not display the above code.

Adding the missing hyphen, it will look like:

alert(2+40);

3
  • 2
    but what if i want to hide a sample response of some code/command? "Show code snippet" is not what it should say Commented Nov 22, 2017 at 9:17
  • @joeybaruch You really should be able to put your own text in there. I think at least that it's obvious it's a dropdown. Then you can name each of the dropdowns something like 'index.html', 'main.css', 'main.js'
    – Sam
    Commented Jun 28, 2019 at 10:11
  • 1
    8 years later, is this really all there is? Is there no generic collapsible block that can be titled as desired?
    – David Gard
    Commented Aug 2, 2023 at 14:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.