not able to add custom template files in gitbook t

2019-04-16 07:52发布

问题:

I am trying to create a custom gitbook theme and in that I also want to change the layout so that the book I create using the theme have the layout that I want. I copied the default templates dir in my assests dir of the custom theme and then modified the layout.html and header.html files as I wanted. Then to include the modified template files, I added the following attribute to the index.js file

module.exports = {
    book: {
        assets: "./assests",
        templates: {
            "layout":"templates/layout.html",
            "header":"templates/includes/book/header.html",
        },
......
......

However with this configuration, the generated book is not picking the template file changes. However I do see the css/js changes that I had done.

回答1:

Templates "layout" and "header" don't exist. You can only change:

  • site: template for the website
  • glossary: template for the glossary
  • langs: template for the choice of languages
  • page: template for the ebook

Changing templates is really not advised, you should use plugins to only extend html,css,js using: https://github.com/GitbookIO/plugin/blob/master/index.js#L2



回答2:

For the record, layout and header template files do exist if you're going the "unadvised" (emphasizing the unadvised nature of this) route of:

  1. Add "theme": "./customtheme" to your book.json file.
  2. Create your customtheme folder in the root with the files from the Gitbook repo
  3. Edit from there

This is so far the only way I've found edit your favicon, sidebar, header, and layout files. It's not recommended because you're no longer using the files in the repo, so updates could break it, but some things either aren't easy or possible to make changes without doing something messy and hacky like this. Hopefully simple things like updating a favicon, header, or sidebar could be made to be easier in the future. I've only found this solution after many, many google searches and plugin comparisons, so maybe some one has a better solution that I haven't found yet.