Collapsible code block comments

Use case or problem

Obsidian is awesome for taking notes on code.

I have gotten in the habit of explaining my code or code that I am learning about using the following setup:

  • comment of LINE 1
    • comment of LINE 2
      • comment of LINE 3
LINE 1
	LINE 2
		LINE 3

But, it can become quite arduous to keep the comments and the lines straight, especially for complex code.

Proposed solution

It would be helpful if the implementation of code blocks allowed for collapsible comments that get rendered into the view of the code block in the note without having to actually add those comments into the code.

Instead, one possible solution would be to use the matching indentation setup, like I use, and even let the collapsible code block comments have a collapse/expand all command as well as individual collapse and expand functionality for each comment via a button on the comment as well as possibly hotkeys triggered from the current line in the comment hierarchy or the code.

In addition to allowing us to keep the actual code clean from comments, I could also imagine some sort of highlighting/color coordination setup that would make drafting the comments easier. In order to make this work, the code block would render the collapsible comments while we are modifying them in the comment hierarchy.

This could create a situation that would be a throwback to the old days of split screen with edit on left and preview on right except both would actually be using Live Preview, but the split screen would be necessary to allow for an easy side by side of the comment and the code hierarchy. I imagine all kinds of additional features where there could be folding and expanding of the visible code based on folding and expanding of the comment hierarchy and vice versa.

I understand I will have to likely remove a bunch from this request to make it more valid and realistic, but I just wanted to get the idea down while it was fresh. It could be a cool standalone tool or a plugin, but something tells me a feature like this might actually register as a feature request because it could make Obsidian that much better for a usage I imagine is quite common.

Thanks!

I see this more as an idea for a plugin than for core functionality.

In the meantime, perhaps the following idea could be useful to you:

Export your code as a PDF (you can use Obsidian for that), then annotate it using PDF++. The annotations aren’t stored in the PDF itself, but in markdown notes. You have full freedom to structure them however you like — use headers, collapsible bullet points, etc., all within markdown files.

The notes are linked to the PDF, so clicking on a note takes you to the corresponding line of code, and vice versa (you also get code highlighting). Obviously, you won’t get “synchronized collapsing,” since the PDF is immutable.

Here is an example of a code (in pdf) and annotations side-by-side. I used the mode “link” and “text” in PDF++ so that the link pasted in the markdown from the PDF contains only the selected text, but you have plenty of options.

2 Likes

Thanks for the thoughtful response. This is definitely helpful for situations where I’m sure the code won’t change. But for me that is often not the case.

And I will likely switch this to a plugin. Just have to think about it. Sometimes it is made clear by moderators that requesting a feature should be switched to a plugin request.