rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.78k stars 1.27k forks source link

How to add custom Gitbook files declared in gitbook.r #640

Closed rblades closed 6 years ago

rblades commented 6 years ago

Hi there,

I am having trouble adding a custom plugin-search.js file to my Gitbook. I modified the contents of the plugin-search.js file in the package (located in /resources/gitbook/js) and want to add and declare it as a custom file in my js folder.

I created a js folder, a custom HTML file called custom-search.html, and declared it in the _output.yml by doing the following:

  includes:
      after_body: custom-search.html

My custom-search.html file contains the javascript declaration <script src="js/plugin-search.js"></script>.

When I build the book it adds the <script src="js/plugin-search.js"></script> before the closing of the <body> tag, but this does not override the default <script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>.

The only other solution I can think of is loading a custom gitbook.r file and modifying the relevant source location below:

gitbook_dependency = function() {
  assets = bookdown_file('resources', 'gitbook')
  owd = setwd(assets); on.exit(setwd(owd), add = TRUE)
  app = if (file.exists('js/app.min.js')) 'app.min.js' else 'app.js'
  list(jquery_dependency(), htmltools::htmlDependency(
    'gitbook', '2.6.7', src = assets,
    stylesheet = file.path('css', c(
      'style.css', 'plugin-bookdown.css', 'plugin-highlight.css',
      'plugin-search.css', 'plugin-fontsettings.css'
    )),
    script = file.path('js', c(
      app, 'lunr.js', 'plugin-search.js', 'plugin-sharing.js',
      'plugin-fontsettings.js', 'plugin-bookdown.js', 'jquery.highlight.js'
    ))
  ))
}

The actual change to plugin-search.js increases the accessibility of the search function by adding a <label> to assist screen readers. If you are interested, I can submit a pull request. You can view my changes on my Bookdown fork.

Many thanks,

Rob

yihui commented 6 years ago

I can take a look at a PR. Thanks!

rblades commented 6 years ago

Hi there,

I was wondering about the status of this issue. Would you like me to submit a pull request?

Thank you,

Rob

yihui commented 6 years ago

Yes.

rblades commented 6 years ago

Perfect, thanks. Pull for reference

github-actions[bot] commented 4 years ago

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.