Best way to create expandable big blocks of code

What I’m trying to do

I sometimes save big blocks of code in Obsidian, but I don’t want to scroll through them every time I enter the note where they are saved.

Things I have tried

I solve this by placing them inside an expandable callout, but having to type a “>” for every single line feels tedious when the code block is very long, even if I press “option” to make it faster.
I’ve also tried using <details> and <summary> HTML tags, but then the block of code becomes a string instead of remaining an Obsidian code block that you can copy with one click, which is very useful.

Another user posted about this but it hasn’t been solved: Trying to hide code block

It feels like there must be a better way to do this. How do you work with big blocks of code in Obsidian? Thank you!

There might be better ways, but you could put the code block between two headers:

Header 1

code block

Next header

You fold header 1 and it’s gone. I often format the ‘stop header’ like this:
# .
This way it is unobtrusive, but I still see that I put a ‘stop header’ there.

You can also use the creases plugin, to keep designated headers folded at all times - or create a button that folds all creases, if you want to be a bit geeky :wink:

2 Likes

Multiple option exists depending on your needs, wants and knowledge.

  • Default callouts – Like you’ve already explored a bit, but can be a bit cumbersome due to the need for quoting everything. That can be a little better by using the Command palette > Toggle blockquote command, and it’ll still give you that copy option you want.
  • Admonition callouts – A plugin for callouts, which uses an outer codeblock to encompass your markdown into a callout. Clear pro for this one is that the code block is left unaltered. Clear con for this one is that renamed links do not get updated within this callout type
  • details/summary html – As you’ve experienced this does solve it to some extent, but it’s rather limiting with strings/markdown/html-stuff
  • CSS styling – There also in an option to apply custom CSS styling, which could allow for display of a given amount of lines, and then scroll for the rest of the code. This would allow for smaller code blocks to show in full (without expanding), whilst larger code blocks would require scrolling. It could be possible to add collapsing to the code block, as well, but I’m not sure of that one. But I do know it’s possible to limit the height of the code blocks, even though some of the CSS gurus needs to help us with the exact code to use.
  • Header folding – Another option could be to add headings in front of the code block, which do allow for the section to be collapsed. But I’m not sure if possible, at least I’ve not found a way, to make these section be collapsed by default.

So there you’ve got some options to consider, and play around with.

2 Likes

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