├── .gitignore ├── LICENSE ├── README.md ├── assets ├── plugin.css └── plugin.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Bill Ryan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gitbook-plugin-etoc 2 | 3 | [](https://npmjs.org/package/gitbook-plugin-etoc) [](https://npmjs.org/package/gitbook-plugin-etoc) [](https://npmjs.org/package/gitbook-plugin-etoc) 4 | 5 | This plugin will add table of content to the page automatically. 6 | When you build the book, it will insert a table of content automatically or to place where you insert ``. Sometimes you may want to disable toc on some page, just add `` on the the markdown page. 7 | 8 | Demo site ==> https://yuanbin.gitbooks.io/test/content/ 9 | 10 | ## Sample 11 | 12 |  13 | 14 | ## Config 15 | 16 | Add `etoc` in `book.json` is enough for most users. 17 | 18 | ``` 19 | { 20 | "plugin": ["etoc"] 21 | } 22 | ``` 23 | 24 | It will add toc automatically if the markdown page meets following requirements. 25 | 26 | - `###` header3 - `mindepth` required to generate toc 27 | - `##` number of header2 greater or equal than lower bound(3 by default, controled by `h2lb`) 28 | 29 | The maxdepth of toc is `####` header4 by default. You can also change the default parameter such as: 30 | ``` 31 | { 32 | "plugins": [ 33 | "etoc" 34 | ], 35 | "pluginsConfig": { 36 | "etoc": { 37 | "h2lb": 3, 38 | "mindepth": 3, 39 | "maxdepth": 4, 40 | "notoc": false 41 | } 42 | } 43 | } 44 | ``` 45 | 46 | The configuration json schema can be found in [gitbook-plugin-etoc/package.json](https://github.com/billryan/gitbook-plugin-etoc/blob/master/package.json) 47 | 48 | ## LICENSE 49 | 50 | MIT 51 | -------------------------------------------------------------------------------- /assets/plugin.css: -------------------------------------------------------------------------------- 1 | #toc, .toc { 2 | background-color: #fcfcfc; 3 | border-radius: 4px; 4 | padding: 7px; 5 | display: table; 6 | font-size: 95%; 7 | line-height: 1.6em; 8 | } 9 | 10 | .toc ul, .toc p { 11 | margin-bottom: 0; 12 | } 13 | 14 | .toc ul { 15 | list-style-type: none; 16 | counter-reset: item; 17 | margin: 0; 18 | padding: 0; 19 | } 20 | 21 | .toc > ul { 22 | padding: 0px 8px; 23 | } 24 | 25 | .toc ul > li { 26 | display: table; 27 | counter-increment: item; 28 | margin-bottom: 0.6em; 29 | } 30 | 31 | .toc ul > li:before { 32 | content: counters(item, ".") ". "; 33 | display: table-cell; 34 | padding-right: 0.6em; 35 | } 36 | 37 | .toc li ul > li { 38 | margin: 0; 39 | } 40 | 41 | .toc li ul > li:before { 42 | content: counters(item, ".") " "; 43 | } 44 | -------------------------------------------------------------------------------- /assets/plugin.js: -------------------------------------------------------------------------------- 1 | require(['gitbook', 'jQuery'], function (gitbook, $) { 2 | function updateToc(_config) { 3 | var $toc = $( '#toc' ); 4 | 5 | if ( $toc.length > 0 ) {} 6 | 7 | return true; 8 | } 9 | 10 | gitbook.events.bind('start', function (e, config) { 11 | updateToc(); 12 | }); 13 | 14 | gitbook.events.bind('page.change', function () { 15 | updateToc(); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var toc = require('markdown-toc'); 2 | var slug = require('github-slugid'); 3 | var eol = require('os').EOL; 4 | 5 | module.exports = { 6 | book: { 7 | assets: './assets', 8 | css: [ 9 | "plugin.css" 10 | ], 11 | js: [ 12 | "plugin.js" 13 | ] 14 | }, 15 | hooks: { 16 | "page:before": function (page) { 17 | if (this.output.name != 'website') return page; 18 | var _notoc = this.config.get('pluginsConfig.etoc.notoc') || false; 19 | var _existstoc = /^\s*\s*$/im.test(page.content); 20 | var _existsnotoc = /^\s*\s*$/im.test(page.content); 21 | if (_existsnotoc) return page; 22 | if (_notoc && (!_existstoc)) return page; 23 | var _h2lb = this.config.get('pluginsConfig.etoc.h2lb') || 3; 24 | 25 | var _mindepth = this.config.get('pluginsConfig.etoc.mindepth') || 3; 26 | var _maxdepth = this.config.get('pluginsConfig.etoc.maxdepth') || 4; 27 | if (_mindepth > _maxdepth) { 28 | console.error("!!!mindepth should be less equal than than maxdepth"); 29 | return page; 30 | } 31 | var re = new RegExp('^#{' + _mindepth + '}[^#]', 'm'); 32 | var _h2num = (page.content.match(/^##[^#]/gm) || []).length; 33 | if (!(re.test(page.content) || (_h2num >= _h2lb))) return page; 34 | 35 | var _header = this.config.get('pluginsConfig.etoc.header') || 1; 36 | var headerReg = new RegExp('(^#{' + _header + '}[^#].*)', 'm'); 37 | if (!(_notoc || _existstoc)) { 38 | // insert after the header _header element 39 | page.content = page.content.replace(headerReg, '$1' + eol + '' + eol); 40 | } 41 | 42 | // markdown-toc does not pass options to generate, 43 | // we should escape not beginning with whitespace 44 | page.content = page.content.replace(/^(\S.*)(.*)$/m, '$1$2'); 45 | page.content = toc.insert(page.content, { 46 | slugify: function (str) { 47 | // handle '{#foo-bar}' syntax (urls) 48 | var m = str.match(/\{#([^\}]+)\}\s*$/); 49 | return m ? m[1].trim() : slug(str); 50 | }, 51 | maxdepth: _maxdepth 52 | }); 53 | page.content = page.content.replace('', ''); 54 | 55 | // handle '{#foo-bar}' syntax (names) 56 | var parts = page.content.split(''); 57 | parts[0] = parts[0].replace( 58 | /\[([^\]\{]+)\{#([^\}]+)\}\]\(#([^\)]+)\)/g, 59 | function(a, b, c, d) { return c != d ? a : ('[' + b.trim() + '](#' + c + ')'); }); 60 | page.content = parts.join(''); 61 | 62 | return page; 63 | }, 64 | 65 | "page": function (page) { 66 | // add toc id and class 67 | page.content = page.content.replace('', '