Skip to content

Collapsible contents (code block) in comments / spoiler tag #166

Open

Description

@aensley

Related: #154, isaacs/github#208

GitHub really needs a way to collapse long contents. Being able to collapse entire comments may suffice for most. To me however, the ideal solution would be to collapse any code block with more than 10 lines (or whatever number) by default. The code block would be replaced by the text:

Code (130 lines) - Expand

Where clicking "Expand" reveals the code block. Being able to specify a collapsible code block with ````collapse` would work equally as well. I recall reading a discussion about this feature involving some GitHub developers. I can't find the link to that discussion, but their response at the time was "We'll implement it when CommonMark does."

Here's the CommonMark discussion: http://talk.commonmark.org/t/what-could-a-spoiler-tag-extension-look-like/767

The GitHub Markdown guide hasn't been updated in over 2 years: https://guides.github.com/features/mastering-markdown/

Perhaps it's time to stop waiting for CommonMark to change and simply add this functionality to GitHub's Web UI; no MarkDown hacks required.

Activity

  1. Mottie commented on Jun 27, 2016

    @Mottie

    In case you're interested, I just created a userscript to collapse code & long quote blocks in comments.

    github-collapse-in-comment

  2. aensley commented on Jun 28, 2016

    @aensley
    Author

    @Mottie That is fantastic! That is exactly what I'm looking for. Any chance you could turn that into Chrome Extension so that it could be synced across browsers?

  3. Mottie commented on Jun 28, 2016

    @Mottie

    That shouldn't be a problem, but what do you mean by "synced across browsers"?

  4. aensley commented on Jun 28, 2016

    @aensley
    Author

    If you setup your Google account on Google Chrome, it can sync extensions and settings between different instances of Google Chrome (laptop, desktop, chromebook, etc.)

  5. Mottie commented on Jun 28, 2016

    @Mottie

    Oh, you can do that with Tampermonkey's TESLA.

    And now that I think about, it might be a bit more painful than I initially thought. If I make more than one addon, I'll need to move each userscript into its own subdirectory with a manifest.json. That might take a bit longer.

  6. Mottie commented on Jul 4, 2016

    @Mottie
  7. ide commented on Jul 30, 2016

    @ide

    For what it's worth, collapsible sections are supported:

    Click to expand

    roflcopter

    <details>
      <summary>Click to expand</summary>
      whatever
    </details>
  8. Mottie commented on Jul 30, 2016

    @Mottie

    @ide The details block won't work in Firefox until v49 (see necolas/normalize.css#604).

  9. Mottie commented on Sep 21, 2016

    @Mottie

    Firefox v49 is now live (since yesterday)!

  10. TWiStErRob commented on Oct 7, 2016

    @TWiStErRob

    Building on @ide's answer, here's how to handle code blocks inside <details>:

    stuff with _mark_ **down**

    formatted heading with a

    code block

    Collapsible until here.

    The key is to wrap the whole contents inside the <p>:

    <details><summary>stuff with *mark* **down**</summary><p>
    
    ## _formatted_ **heading** with [a](link)
    
    ---
    {{standard 3-backtick code block omitted from here due to escaping issues}}
    ---
    
    Collapsible until here.
    </p></details>
    

    For more details see: http://stackoverflow.com/a/39920717/253468

  11. noam3127 commented on Dec 25, 2016

    @noam3127

    Though not directly related to this discussion, I just published a handy new Chrome extension that enables code-folding when viewing files in GitHub and I hope many people find it as useful as I do. Works for any indentation style - tabs, 2 spaces, 4 spaces, etc. https://github.com/noam3127/github-code-folding

  12. Bluscream commented on Dec 30, 2016

    @Bluscream
    Click to expand whatever
  13. caseydwayne commented on Jan 3, 2017

    @caseydwayne

    Solution provided above.

    Search not, future seekers. Close issue?

  14. TWiStErRob commented on Jan 3, 2017

    @TWiStErRob

    @caseydwayne I object to closing, I don't think of <details> as a solution, more like a workaround/hack. It's HTML after all, not GFM. It has issues, like not being cross-browser and non-trivial nesting of code blocks I pointed out above.

  15. 30 remaining items

  16. Kebap commented on Dec 1, 2020

    @Kebap

    How can I use checkmarks inside spoiler? This is a checkmark:

    • Click me to toggle checked state

    Where * [ ] starts an unchecked, and * [x] starts a checked line.

    However inside the spoiler, toggling state will collapse the spoiler..!

    Open me and check three marks!
    • Click me, please!
    • Me too!
    • And me again!

    This is especially troublesome in longer spoilers than I showed above.
    You will loose the scroll-state, and need to click much more than thrice.

  17. Wohlstand commented on Feb 8, 2021

    @Wohlstand

    When using the tag, pictures getting not working:

    some ![peshka](https://user-images.githubusercontent.com/6751442/107185174-fcd8c980-69f2-11eb-89d4-0843c47d781b.jpeg)

    At Wiki this won't show, at issue comment here it works:
    peshka

  18. andreymal commented on Feb 8, 2021

    @andreymal

    Just add a empty line after <details>/<summary>

    Click me

    peshka

  19. pilyay commented on Apr 28, 2021

    @pilyay

    Hello!

    I want to make the name of the spoiler as a link, how do you think such an entry in markdown is correct?

    [<details><summary>Chapter 1</summary>](#chapter_1)
    
        * [Chapter 1.1](#chapter_1_1)
        * [Chapter 1.2](#chaper_1_2)
    
    </details>
    
  20. glibg10b commented on Jun 23, 2021

    @glibg10b

    @pilyay

    <details>
      <summary><a href="#chapter_1">Chapter 1</a></summary>
    
      * [Chapter 1.1](#chapter_1_1)
      * [Chapter 1.2](#chaper_1_2)
    
    </details>
    Chapter 1
  21. Berchon commented on Jul 4, 2021

    @Berchon

    I'm trying to do the same as @pilyay , but unfortunately your suggestion didn't work for me, @glibg10b .

  22. glibg10b commented on Jul 4, 2021

    @glibg10b

    @Berchon maybe try a different browser? I think <details/>'s implementation varies between browsers. I am using Firefox and it works fine.

  23. TWiStErRob commented on Jul 4, 2021

    @TWiStErRob

    @Berchon what does "doesn't work" mean in your case? I tried Android Chrome and it was hard to expand because we can't click on the link or the arrow, only on the right side of the line. After that it jumps to heading as expected. I guess adding more (unlinked) text would make this better. Note: the empty line after summary and before details is sometimes very important.

  24. porg commented on Oct 8, 2021

    @porg

    @ide and @TWiStErRob thanks for your practical solutions!

  25. aniespica commented on Aug 1, 2022

    @aniespica

    How can I use checkmarks inside spoiler? This is a checkmark:

    • Click me to toggle checked state

    Where * [ ] starts an unchecked, and * [x] starts a checked line.

    However inside the spoiler, toggling state will collapse the spoiler..!

    Open me and check three marks!
    This is especially troublesome in longer spoilers than I showed above. You will loose the scroll-state, and need to click much more than thrice.

    Am I having these problems any solution?

  26. a-pav commented on Jun 23, 2024

    @a-pav

    Is there any shortcut / autocomplete / button to spawn a <detail> tag, ready to be filled in?
    I hate that every time I have to look it up or type it down!

  27. vintagesucks commented on Jun 26, 2024

    @vintagesucks

    Is there any shortcut / autocomplete / button to spawn a tag, ready to be filled in?

    @a-pav The Refined GitHub browser extension adds a button to do just that:

    grafik

  28. PieterjanDeClippel commented on Jan 27, 2025

    @PieterjanDeClippel

    Works without any extension whatsoever:

    Click to expand
    <details>
    <summary>Click to expand</summary>
    
    ```html
    <h1>Hello world</h1>
    <h2>This is me</h2>
    ```
    
    </details>

    Note that you need to leave an addtitional newline around the code block

    Test using an image

    peshka

    Here's a gist

  29. TWiStErRob commented on Jan 27, 2025

    @TWiStErRob

    @PieterjanDeClippel yes it does. The point in recent conversations is that a UI interaction is better than manual typing/Google-copy-paste, an interaction would ideally take care of annoyances like remembering to insert those new lines.

  30. itsramiel commented on May 14, 2025

    @itsramiel

    Works without any extension whatsoever:

    Click to expand

    Click to expand
    <h1>Hello world</h1>
    <h2>This is me</h2>
    Note that you need to leave an addtitional newline around the code block

    Test using an image

    Here's a gist

    Legend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions