4

It is critical that people reading my Bookdown project do not skip chapters. While it is explicitly stated in the document, further discouraging this by automatically collapsing the sidebar would be really helpful :o)

According to this Github issue there is no default explicit option to do this, but is there any other way?

snug.gy
  • 113
  • 3

1 Answers1

6

Welcome to stackoverflow!

Using e.g. the inspect feature of Chrome on a bookdown::gitbook you see that the first element in the DOM after <body> is a <div> which 'contains' the whole book. This <div> has multiple classes, one of them is with-summary and this is the one you'd like to remove.

I think the quickest way to do this is jusing jquery:

  1. Set up an HTML file

    header.html

    <script>
     window.addEventListener("DOMContentLoaded", function(){
      $("div").first().removeClass("with-summary");
     });
    </script>
    
  2. Include the file in the document header using the YAML option includes in your

    .Rmd

    ---
    title: "My Title"
    output: 
      bookdown::gitbook:
        includes:
          in_header: header.html
    ---
    

After the page load, the jquery function will pick the first <div> element in the DOM and remove the class. Note that this will hide the sidebar if the user navigates to another chapter, too.

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22