Bookdown and gitbook

Viewed 519

I am a relatively experienced user of bookdown - one project makes complicated and formatted LaTeX and HTML outputs flawlessly and I'm happy with it.

The other project is strictly HTML and I want to insert a javascript script that I've used in other projects that add a "go to the top" link when the page scrolls a set amount. It works everywhere ... except in Bookdown. I use the includes to add the appropriate lines in the header and after the body.

I suspect that what keeps it from working is the gitbook previous and next arrows that appear on every page and take up 90 pixels as a stripe on the left and right of every HTML window. I've no clue how to test this...except that if I just export the raw HTML, the to-the-top stuff works.

My questions:

  1. I cannot for the life of me figure out how to not build an HTML output from bookdown without using the gitbook theme. The documentation is really difficult to follow if you learn by example. Here's my yaml header in index.rmd

    --- 
    title: "Quarks, Spacetime, and the Big Bang"
    author: "Raymond Brock"
    date: "`r Sys.Date()`"
    site: bookdown::bookdown_site
    bookdown::gitbook:
      config:
        toc:
          collapse: section
          toc_depth: 1
          scroll_highlight: yes
          before: null
      number_sections: TRUE
    output:
      bookdown::gitbook:
        includes:
          after_body: myscripts.html
          in_header: mybutton_style.html
      bookdown::html_book:
        css: mystyle.css
        includes:
          after_body: myscripts.html
          before_body: mybutton.html
          in_header: mybutton_style.html
    documentclass: book
    bibliography: [AtG2.bib]
    biblio-style: apalike
    link-citations: yes
    description: 'A high level romp through the smallest and the largest things in the universe'
    cover-image: images/beginning/higgs.jpg
    ---
    

    You can see the includes that add the code necessary for the to-the-top link (notice that I have a slideshow javascript happening which works just fine, verifying my use of the includes).

    myscripts.html

     <!-- slide show -->
    
    <script>
    var slideIndex = 1;
    showSlides(slideIndex);
    
    function plusSlides(n) {
      showSlides(slideIndex += n);
    }
    
    function currentSlide(n) {
      showSlides(slideIndex = n);
    }
    
    function showSlides(n) {
      var i;
      var slides = document.getElementsByClassName("mySlides");
      var dots = document.getElementsByClassName("dot");
      if (n > slides.length) {slideIndex = 1}    
      if (n < 1) {slideIndex = slides.length}
      for (i = 0; i < slides.length; i++) {
          slides[i].style.display = "none";  
      }
      for (i = 0; i < dots.length; i++) {
          dots[i].className = dots[i].className.replace(" active", "");
      }
      slides[slideIndex-1].style.display = "block";  
      dots[slideIndex-1].className += " active";
    }
    </script>
    
    <button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
    
    <script>
    //Get the button
    var mybutton = document.getElementById("myBtn");
    
    // When the user scrolls down 20px from the top of the document, show the button
    window.onscroll = function() {scrollFunction()};
    
    function scrollFunction() {
      if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        mybutton.style.display = "block";
      } else {
        mybutton.style.display = "none";
      }
    }
    
    // When the user clicks on the button, scroll to the top of the document
    function topFunction() {
      document.body.scrollTop = 0;
      document.documentElement.scrollTop = 0;
    }
    </script>
    

    mybottonstyle.html

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 20px;
    }
    
    #myBtn {
      display: none;
      position: fixed;
      bottom: 20px;
      right: 600px;
      z-index: 99;
      font-size: 18px;
      border: none;
      outline: none;
      background-color: red;
      color: white;
      cursor: pointer;
      padding: 15px;
      border-radius: 4px;
    }
    
    #myBtn:hover {
      background-color: #555;
    }
    </style>
    

    How can I get rid of the gitbook stuff and just produce clean HTML driven my my css but still retain a TOC sidebar??

  2. Or, if I must use gitbook for the sidebar, how can I get rid of the right and left arrow slices that I think are in the way of my javascript to-the-top tool? With the TOC sidebar, they are redundant and not necessary at all.

0 Answers
Related