• book.json

    book.json

    gitbook 在编译书籍的时候会读取书籍源码顶层目录中的 book.js 或者 book.json,这里以 book.json 为例,参考 gitbook 文档 可以知道,book.json 支持如下配置:

    1. {
    2. // Folders to use for output
    3. // Caution: it overrides the value from the command line
    4. // It's not advised this option in the book.json
    5. "output": null,
    6. // Generator to use for building
    7. // Caution: it overrides the value from the command line
    8. // It's not advised this option in the book.json
    9. "generator": "site",
    10. // Book metadats (somes are extracted from the README by default)
    11. "title": null,
    12. "description": null,
    13. "isbn": null,
    14. // For ebook format, the extension to use for generation (default is detected from output extension)
    15. // "epub", "pdf", "mobi"
    16. // Caution: it overrides the value from the command line
    17. // It's not advised this option in the book.json
    18. "extension": null,
    19. // Plugins list, can contain "-name" for removing default plugins
    20. "plugins": [],
    21. // Global configuration for plugins
    22. "pluginsConfig": {
    23. "fontSettings": {
    24. "theme": "sepia", "night" or "white",
    25. "family": "serif" or "sans",
    26. "size": 1 to 4
    27. }
    28. },
    29. // Variables for templating
    30. "variables": {},
    31. // Links in template (null: default, false: remove, string: new value)
    32. "links": {
    33. // Custom links at top of sidebar
    34. "sidebar": {
    35. "Custom link name": "https://customlink.com"
    36. },
    37. // Sharing links
    38. "sharing": {
    39. "google": null,
    40. "facebook": null,
    41. "twitter": null,
    42. "weibo": null,
    43. "all": null
    44. }
    45. },
    46. // Options for PDF generation
    47. "pdf": {
    48. // Add page numbers to the bottom of every page
    49. "pageNumbers": false,
    50. // Font size for the fiel content
    51. "fontSize": 12,
    52. // Paper size for the pdf
    53. // Choices are [ua0’, ua1’, ua2’, ua3’, ua4’, ua5’, ua6’, ub0’, ub1’, ub2’, ub3’, ub4’, ub5’, ub6’, ulegal’, uletter’]
    54. "paperSize": "a4",
    55. // Margin (in pts)
    56. // Note: 72 pts equals 1 inch
    57. "margin": {
    58. "right": 62,
    59. "left": 62,
    60. "top": 36,
    61. "bottom": 36
    62. },
    63. //Header HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
    64. "headerTemplate": null,
    65. //Footer HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
    66. "footerTemplate": null
    67. }
    68. }

    注意:上面的内容直接从 gitbook 文档 中复制,所以可能过期!

    首先,将这个文件放到书籍代码顶层目录中,命名为 book.json,然后编译书籍:

    1. $ gitbook build

    可以看到,编译完成,使用

    1. $ gitbook serve

    然后将浏览器指向 http://127.0.0.1:4000,可以看到,什么都没有改变!

    是的,虽然这里 book.json 文件非法,但是 gitbook build 并没有报错!所以,用户需要自己准备工具来保证 book.json 必须是一个合法的 JSON 文件,并且不能含有非法配置项。

    首先,删除注释项,以及空行,如果是在 vim 中,可以执行下面的命令:

    1. :%g/\s*\/\//d
    2. :%g/^\s*$/d

    然后,使用 python 来检查 book.json 是否合法,同样,在 vim 中执行下面的命令:

    1. :%!python -m json.tool

    很显然,下面的配置不能通过,所以删去(注:但是默认主题却是使用的这个配置!)。

    1. "pluginsConfig": {
    2. "fontSettings": {
    3. "theme": "sepia", "night" or "white",
    4. "family": "serif" or "sans",
    5. "size": 1 to 4
    6. }
    7. },

    最后,剩下的内容如下:

    1. {
    2. "description": null,
    3. "extension": null,
    4. "generator": "site",
    5. "isbn": null,
    6. "links": {
    7. "sharing": {
    8. "all": null,
    9. "facebook": null,
    10. "google": null,
    11. "twitter": null,
    12. "weibo": null
    13. },
    14. "sidebar": {}
    15. },
    16. "output": null,
    17. "pdf": {
    18. "fontSize": 12,
    19. "footerTemplate": null,
    20. "headerTemplate": null,
    21. "margin": {
    22. "bottom": 36,
    23. "left": 62,
    24. "right": 62,
    25. "top": 36
    26. },
    27. "pageNumbers": false,
    28. "paperSize": "a4"
    29. },
    30. "plugins": [],
    31. "title": null,
    32. "variables": {}
    33. }

    现在,修改一些配置,修改后为:

    1. {
    2. "author": "Chengwei Yang <me@chengweiyang.cn>",
    3. "description": "This is a sample book created by gitbook",
    4. "extension": null,
    5. "generator": "site",
    6. "isbn": null,
    7. "links": {
    8. "sharing": {
    9. "all": null,
    10. "facebook": null,
    11. "google": null,
    12. "twitter": null,
    13. "weibo": null
    14. },
    15. "sidebar": {
    16. "Chengwei's Blog": "http://www.chengweiyang.cn"
    17. }
    18. },
    19. "output": null,
    20. "pdf": {
    21. "fontSize": 12,
    22. "footerTemplate": null,
    23. "headerTemplate": null,
    24. "margin": {
    25. "bottom": 36,
    26. "left": 62,
    27. "right": 62,
    28. "top": 36
    29. },
    30. "pageNumbers": false,
    31. "paperSize": "a4"
    32. },
    33. "plugins": [],
    34. "title": "Sample GitBook",
    35. "variables": {}
    36. }

    现在,重新编译书籍,预览效果,如下图所示:

    configure book.json

    可以看到,书籍的标题变成了 “Sample GitBook”,而且在左边的导航栏中添加了一个链接!

    需要注意的是:GitBook.com 上的书籍标题经试验不能通过配置 book.json 的方式修改 title,需要在书籍的属性页面中的 ‘Settings’ 中进行修改!