Skip to content

Feature request: Support for code folding buttons #764

Closed

Description

@bblodfon

Hi,

I recently searched for an option to have buttons that hide the R code output since it was not supported via a simple option in bookdown::gitbook. Using this stackoverflow answer I implemented somewhat of a hacked version with nice CSS (from the united bootstrap theme) for the buttons and it looks like this. For the individual R chunks the solution is nice, though for the hide-show-all button that is in the right-up corner, the solution is sowewhat (!) hacked (position: fixed in the CSS). But it works so it's good enough for me :)

I was considering though that it would be really nice, to have the option integrated globally in gitbook, where the hide-show-all button would be in the toolbar (like all the other sharing buttons - github, fb, etc.) and the config would look somewhat like this:

bookdown::gitbook:
  config:
    show_codefolding_buttons: true # to show all the buttons
    hide_code: true # initial configuration, no R code is shown if above is true

BR, John.


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.name/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('bookdown'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('rstudio/bookdown').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

Activity

  1. changed the title [-]Support for code folding buttons (suggestion)[/-] [+]Feature request: Support for code folding buttons[/+] on Feb 19, 2020
  2. bblodfon commented on Mar 9, 2020

    @bblodfon
    ContributorAuthor

    Based on the previosuly-mentioned stackoverflow answer, I implemented an R package with a bookdown template that uses a code-folding buttons. Check it here: https://github.com/bblodfon/rtemps.

    This is more like a quick-hacked-working solution - I don't know if this should be implemented as part of the main bookdown package or not at this point.

  3. cderv commented on Mar 24, 2021

    @cderv
    Collaborator

    Thanks for this suggestion. I believe this is duplicated of #395.

    We would definitely welcome a PR so that it is supported in bookdown::gitbook(). It is a feature that has a lot of 👍

    Are you willing to submit a PR ?

  4. added
    feature
    on Mar 24, 2021
  5. bblodfon commented on Mar 24, 2021

    @bblodfon
    ContributorAuthor

    Hi @cderv , as I explain in the original post above it is a very hacked solution the one I implemented in rtemps. If it's still welcomed though, I will need the time to look at the bookdown code, which currently I don't have. Maybe during summer or afterwards as a side-project, but no guarantees!

  6. cderv commented on Mar 24, 2021

    @cderv
    Collaborator

    That is ok. We may look into ourself before then. Thanks for your quick answer.

  7. atusy commented on Mar 31, 2021

    @atusy
    Collaborator

    Let me give people a prototype.
    Just like html_document, initial state can be "show" or "hide" by editing {"initial-state": "show"}

    ---
    output: bookdown::gitbook
    ---
    
    ```{=html}
    <!-- This part can be programmatically generated with htmltools. Thus we can control initial state from the YAML front matter-->
    <script id="code-folding-options" type="application/json">
      {"initial-state": "show"}
    </script>
    ```
    
    ```{js}
    document.addEventListener("DOMContentLoaded", function() {
      const languages = ['r', 'python', 'bash', 'sql', 'cpp', 'stan', 'julia', 'foldable'];
      const options = JSON.parse(document.getElementById("code-folding-options").text);
      const show = options["initial-state"] !== "hide";
      Array.from(document.querySelectorAll("pre.sourceCode")).map(function(pre) {
        const classList = pre.classList;
        if (languages.some(x => classList.contains(x))) {
          const div = pre.parentElement;
          const state = show || classList.contains("fold-show") && !classList.contains("fold-hide") ? " open" : "";
          div.outerHTML = `<details${state}><summary></summary>${div.outerHTML}</details>`;
        }
      });
    });
    ```
    
    ```{css}
    summary {
      display: list-item;
      text-align: right;
    }
    summary::after {
      content: 'Source'
    }
    details[open] > summary::after {
      content: 'Hide'
    }
    ```
    
    ```{r}
    head(mtcars)
    ```
    
  8. atusy commented on Mar 31, 2021

    @atusy
    Collaborator

    I think the above JS is light enough to be an optional feature of rmarkdown::html_vignette as well.
    So I'd like to suggest include this feature as htmlDependency in the rmarkdown package, and bookdown package depends on it.

  9. added
    next
    on Mar 31, 2021
  10. cderv commented on Mar 31, 2021

    @cderv
    Collaborator

    @atusy you can open a feature request in rmarkdown linking to this one so that it could be consider there.
    I know html_vignette() is designed to be lightweight but it seems small enough to be added when opt-in.

    Thank you for all your ideas !

  11. atusy commented on Mar 31, 2021

    @atusy
    Collaborator

    Great. I will.

  12. isaactpetersen commented on Feb 16, 2022

    @isaactpetersen

    Any update on this? I'm very interested in bookdown support for code folding using collapsible code chunks. This would prevent users from having to read through many lines of code (unless they want to), while still making the code available.

  13. self-assigned this
    on Feb 16, 2022
  14. atusy commented on Feb 16, 2022

    @atusy
    Collaborator

    Sorry, I was totally forgetting about this issue.
    I assigned myself and will work on this or next weekends.

  15. atusy commented on Feb 17, 2022

    @atusy
  16. 2 remaining items

  17. atusy commented on Mar 31, 2022

    @atusy
    Collaborator

    Finally, I am working on this issue.
    Once the PR is merged in rmarkdown, we can apply the feature to bookdown, too.
    rstudio/rmarkdown#2340

  18. isaactpetersen commented on Mar 31, 2022

    @isaactpetersen

    Great, thanks very much for the update and the addition to rmarkdown! Will be exciting to implement this in bookdown. Much appreciated.

  19. isaactpetersen commented on Jul 13, 2022

    @isaactpetersen

    @atusy Is codefolding able to incorporated into bookdown now that it has been merged into rmarkdown? Any updates on this would be greatly appreciated. Thanks!

  20. atusy commented on Aug 14, 2022

    @atusy
    Collaborator

    @isaactpetersen Yes, it should be. I've been less active these days... I'll make some time this week.

  21. atusy commented on Aug 14, 2022

    @atusy
    Collaborator

    @isaactpetersen
    I opened a PR. You can try it with remotes::install_github("rstudio/bookdown#1368")

  22. moved this from Backlog to In Progress in R Markdown Team Projectson Aug 15, 2022
  23. added a commit that references this issue on Aug 15, 2022
    e40bfeb
  24. Repository owner moved this from In Progress to Done in R Markdown Team Projectson Aug 15, 2022
  25. isaactpetersen commented on Aug 16, 2022

    @isaactpetersen

    Great, thank you! @atusy

  26. github-actions commented on Feb 15, 2023

    @github-actions

    This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

  27. locked as resolved and limited conversation to collaborators on Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featurenext

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions