155

Background

When answering questions on SO I generally like to ensure that my code is runnable by including sample data, so someone can copy, paste, and run it, seeing exactly what I'd proposed.

However, often this setup code takes up a lot of space before the code of interest, so I have to put the interesting code in my answer, followed by a runnable example with the full code.

Example of such an answer: Match any patterns in a table?

Suggestion

Have a way to collapse/hide code which is superfluous to the answer, but can be expanded.

var viewModel = function() {
  this.expanded = ko.observable(false);
  this.notexpanded = ko.observable(true);

  this.toggle = function () {
    this.expanded(!this.expanded());
    this.notexpanded(!this.expanded());
  };
};

ko.applyBindings(new viewModel());
.expand {
  color: red;
  background-color: lightgrey;
}
.collapsible {
  background-color: lightgrey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<pre data-bind="visible: expanded, click: toggle" class="collapsible">
declare @patterns table 
(
	pattern nvarchar(16) not null
)
declare @data table
(
	datum nvarchar(16) not null
)
insert @patterns
values ('host1%')
,('%host2')
insert @data
values ('host1234')
, ('234host1')
, ('host2345')
, ('345host2')

</pre>
<a class="expand" href="#" data-bind="visible: notexpanded, click: toggle">-Expandible for Code Context-</a>
<pre>
select *
from @data
where not exists 
(
	select top 1 1
	from @patterns
	where datum like pattern
)
</pre>

Ironically I've used Code Snippets to demonstrate the functionality I'm describing (run the above to see); the difference being the above code-snippet is used to embed executable code into a post, whilst I'm only suggesting having the show-hide aspect of this feature (since this could be implemented in a language independent way, and for little effort).

21
  • 11
    That's a nice idea. I tend to push the setup code to the back but having the stuff that directly answers the problem at the top would be very nice..
    – TaW
    Commented Aug 9, 2015 at 7:18
  • We'd probably need to talk about other languages than SQL. Posting everything in a small runnable example requires sometimes a bunch of different files, settings etc. What if the question is about a simple algorithm or a technical approach? Commented Aug 9, 2015 at 17:08
  • 3
    @KonradViltersten. I was thinking purely of collapsible regions; so no awareness of language / syntax required; just some markdown to say “hide/collapse this region by default”.
    – JohnLBevan
    Commented Aug 10, 2015 at 0:29
  • 8
    That, my friend is a great idea. It's already in effect, although maybe not in the most optimal way with respect to programming. I suggest you go to Puzzles site (it's part of StackExchange network) and see how they present hints in a non-spoiling syntax. Commented Aug 10, 2015 at 6:17
  • 4
    We're thinking about this, but discoverability would be a concern. However, we recently started giving some thought to including an actual code editor to our post editor (see this post), and this option could be included there (maybe not in a first iteration though). I'll make this a status-review, but keep in mind that this change is not right around the corner.
    – Thomas Orozco Staff
    Commented Aug 10, 2015 at 12:14
  • 3
    We are happy with anything between 6 to 8 weeks....
    – rene
    Commented Aug 10, 2015 at 12:26
  • 2
    @KonradViltersten If you're talking about the blockquote that only appears on mouse-over, those unfortunately take up pretty much the same amount of space as not using them (AFAIK). Commented Aug 10, 2015 at 12:27
  • @JohnLBevan Were you able to do the toggle in Markdown? Or did you need regular HTML?
    – user3373470
    Commented Aug 10, 2015 at 12:54
  • 1
    @HunterStevens : I'm not sure I understand the question, sorry. What I'd like would be to do this in Markdown. What I did... If you mean the above Code Snippet toggle, that's OOTB functionality using Javascript/HTML/CSS Snippet (Ctrl+M); used to add runnable samples. If you mean the sample code in that snippet, that uses knockout.js (stackoverflow.com/questions/10577326/…). If something else, please let me know.
    – JohnLBevan
    Commented Aug 10, 2015 at 15:17
  • 1
    @JohnLBevan I meant the CodeSnippet toggle, when you click the link. I do not know if it is possible to do it in vanilla markdown. I have not even seen it in Github-flavored markdown. I did not know you can CTRL+M to do that kind of stuff. I support your request, however, as it would shorten A LOT of long questions by new users.
    – user3373470
    Commented Aug 10, 2015 at 15:32
  • 2
    @HunterStevens - ironically, new users are unlikely to use it.
    – Peter Wone
    Commented Aug 11, 2015 at 6:17
  • 2
    Years later, I'd like to know if this has been implemented as this is exactly what I sometimes am looking for
    – Rafalon
    Commented Jul 11, 2018 at 9:28
  • 1
    I'm a beginner at SO and I'd like to know how can I do the same collapsible block as the topic starter did in his question?
    – finnan
    Commented Dec 13, 2018 at 23:06
  • 3
    @finnan see meta.stackexchange.com/a/261081/199916 (or simply click edit on the above question to see the related code). The key is <!-- begin snippet: js hide: true --> stuff-to-hide <!-- end snippet -->/
    – JohnLBevan
    Commented Dec 13, 2018 at 23:24
  • 3
    I would like to be able to hide collapse arbitrary parts of the post like this, not just code blocks. I write very detailed answers sometimes. Commented Jan 25, 2023 at 21:22

1 Answer 1

-2

This seems to be available now, the "Show code snippet" toggle appears in the example provided in the question post. However, our team is going to take a little time in the future to dig into this one more before we consider it completed so I've updated the status to deferred.

3
  • 6
    Collapsing a snippet was always possible, also when this question was posted in 2015 (as mentioned by the asker). However, that was not what this feature request was about. A snippet is only for javascript/html/css code and contains a button to run this code directly within the question. It should not be used for code in any other programming .language such as Python or C#. The feature request is to implement the same functionality as already exists for snippets, but then for regular code blocks, i.e., code in triple backticks or indented with four spaces.
    – Marijn
    Commented Jan 26, 2023 at 7:35
  • eh, javascript's the only important language anyway
    – Kevin B
    Commented Feb 24, 2023 at 20:41
  • Would be a very nice feature!
    – Synoon
    Commented May 25, 2023 at 7:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .