diff --git a/README.md b/README.md index 8773573910..2a7993b97a 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,17 @@ This theme can be used just as other [Jekyll themes][1]. [Fork][3] this repository and add your markdown posts to the `_posts` folder. +## How to generate TOC + +The jekyll-gitbook theme leverages [jekyll-toc][4] to generate the *Contents* for the page. +The TOC feature is not enabled by default. To use the TOC feature, modify the TOC +configuration in `_config.yml`: + +```yaml +toc: + enabled: true +``` + ## License This work is open sourced under the Apache License, Version 2.0. @@ -36,3 +47,4 @@ Copyright 2019 Tao He. [1]: https://pages.github.com [2]: https://pages.github.com/themes [3]: https://github.com/sighingnow/jekyll-gitbook/fork +[4]: https://github.com/allejo/jekyll-toc diff --git a/_config.yml b/_config.yml index 894908743b..3e823bda67 100644 --- a/_config.yml +++ b/_config.yml @@ -13,6 +13,11 @@ url: 'https://sighingnow.github.io' baseurl: '/jekyll-gitbook' rss: RSS +toc: + enabled: true + h_min: 1 + h_max: 3 + # markdown render engine. markdown: kramdown kramdown: diff --git a/_includes/toc-date.html b/_includes/toc-date.html index 892865ab0e..23b4d26c8b 100644 --- a/_includes/toc-date.html +++ b/_includes/toc-date.html @@ -27,6 +27,11 @@ {{ post.title | escape }} + {% if site.toc.enabled %} + {% if page.url == post.url %} + {% include toc.html html=content h_min=site.toc.h_min h_max=site.toc.h_max %} + {% endif %} + {% endif %} {% endfor %} diff --git a/_includes/toc.html b/_includes/toc.html new file mode 100644 index 0000000000..8c71007227 --- /dev/null +++ b/_includes/toc.html @@ -0,0 +1,182 @@ +{% capture tocWorkspace %} + {% comment %} + Copyright (c) 2017 Vladimir "allejo" Jimenez + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, + copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following + conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + {% endcomment %} + {% comment %} + Version 1.1.0 + https://github.com/allejo/jekyll-toc + + "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe + + Usage: + {% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %} + + Parameters: + * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll + + Optional Parameters: + * sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC + * class (string) : '' - a CSS class assigned to the TOC + * id (string) : '' - an ID to assigned to the TOC + * h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored + * h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored + * ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list + * item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level + * submenu_class (string) : '' - add custom class(es) for each child group of headings; has support for '%level%' placeholder which is the current "submenu" heading level + * base_url (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content + * anchor_class (string) : '' - add custom class(es) for each anchor element + * skip_no_ids (bool) : false - skip headers that do not have an `id` attribute + + Output: + An ordered or unordered list representing the table of contents of a markdown block. This snippet will only + generate the table of contents and will NOT output the markdown given to it + {% endcomment %} + + {% capture newline %} + {% endcapture %} + {% assign newline = newline | rstrip %} + + {% capture deprecation_warnings %}{% endcapture %} + + {% if include.baseurl %} + {% capture deprecation_warnings %}{{ deprecation_warnings }}{{ newline }}{% endcapture %} + {% endif %} + + {% if include.skipNoIDs %} + {% capture deprecation_warnings %}{{ deprecation_warnings }}{{ newline }}{% endcapture %} + {% endif %} + + {% capture jekyll_toc %}{% endcapture %} + {% assign orderedList = include.ordered | default: false %} + {% assign baseURL = include.base_url | default: include.baseurl | default: '' %} + {% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %} + {% assign minHeader = include.h_min | default: 1 %} + {% assign maxHeader = include.h_max | default: 6 %} + {% assign nodes = include.html | strip | split: ' maxHeader %} + {% continue %} + {% endif %} + + {% assign _workspace = node | split: '' | first }}>{% endcapture %} + {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} + + {% if include.item_class and include.item_class != blank %} + {% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %} + {% endif %} + + {% if include.submenu_class and include.submenu_class != blank %} + {% assign subMenuLevel = currLevel | minus: 1 %} + {% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %} + {% endif %} + + {% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} + + {% if htmlID %} + {% capture anchorAttributes %} href="{% if baseURL %}{{ baseURL }}{% endif %}#{{ htmlID }}"{% endcapture %} + + {% if include.anchor_class %} + {% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %} + {% endif %} + + {% capture listItem %}{{ anchorBody }}{% endcapture %} + {% elsif skipNoIDs == true %} + {% continue %} + {% else %} + {% capture listItem %}{{ anchorBody }}{% endcapture %} + {% endif %} + + {% if currLevel > lastLevel %} + {% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %} + {% elsif currLevel < lastLevel %} + {% assign repeatCount = lastLevel | minus: currLevel %} + + {% for i in (1..repeatCount) %} + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} + {% endfor %} + + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} + {% else %} + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} + {% endif %} + + {% capture jekyll_toc %}{{ jekyll_toc }}{{ listItem }}{% endcapture %} + + {% assign lastLevel = currLevel %} + {% assign firstHeader = false %} + {% endfor %} + + {% assign repeatCount = minHeader | minus: 1 %} + {% assign repeatCount = lastLevel | minus: repeatCount %} + {% for i in (1..repeatCount) %} + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} + {% endfor %} + + {% if jekyll_toc != '' %} + {% assign rootAttributes = '' %} + {% if include.class and include.class != blank %} + {% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} + {% endif %} + + {% if include.id and include.id != blank %} + {% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %} + {% endif %} + + {% if rootAttributes %} + {% assign nodes = jekyll_toc | split: '>' %} + {% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} + {% endif %} + {% endif %} +{% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc }} diff --git a/_posts/2019-04-28-howto.md b/_posts/2019-04-28-howto.md index bb740f669d..e33798e7d5 100644 --- a/_posts/2019-04-28-howto.md +++ b/_posts/2019-04-28-howto.md @@ -6,7 +6,7 @@ category: Jekyll layout: post --- -This theme can be used just as other [Jekyll themes][1]. +The jekyll-gitbook theme can be used just as other [Jekyll themes][1]. [Fork][2] this repository and add your markdown posts to the `_posts` folder, then push to your own Github repository. No need to push generated HTML bundle. diff --git a/_posts/2019-04-29-license.md b/_posts/2019-04-29-license.md index bd799a0de8..793e52a6ee 100644 --- a/_posts/2019-04-29-license.md +++ b/_posts/2019-04-29-license.md @@ -1,7 +1,7 @@ --- title: License author: Tao He -date: 2019-04-28 +date: 2019-04-29 category: Jekyll layout: post --- diff --git a/_posts/2021-08-10-toc.md b/_posts/2021-08-10-toc.md new file mode 100644 index 0000000000..d4cf483dbf --- /dev/null +++ b/_posts/2021-08-10-toc.md @@ -0,0 +1,170 @@ +--- +title: How to Generate TOC +author: Tao He +date: 2019-04-28 +category: Jekyll +layout: post +--- + +The jekyll-gitbook theme leverages [jekyll-toc][1] to generate the *Contents* for the page. +The TOC feature is not enabled by default. To use the TOC feature, modify the TOC +configuration in `_config.yml`: + +```yaml +toc: + enabled: true +``` + +Why this repo +------------- + +long contents ..... + +1. a +2. b +3. c +4. d + +Why this repo +------------- + +long contents ..... + ++ 1 ++ 2 ++ 3 ++ 4 + +Why this repo +------------- + +long contents ..... + +1. e +2. f +3. g +4. h + +Why this repo +------------- + ++ 5 ++ 6 ++ 7 ++ 8 + +Why this repo +------------- + +long contents ..... + +1. a +2. b +3. c +4. d + +Why this repo +------------- + +long contents ..... + ++ 1 ++ 2 ++ 3 ++ 4 + +Why this repo +------------- + +long contents ..... + +1. e +2. f +3. g +4. h + +Why this repo +------------- + ++ 5 ++ 6 ++ 7 ++ 8 + +Why this repo +------------- + +long contents ..... + +1. a +2. b +3. c +4. d + +Why this repo +------------- + +long contents ..... + ++ 1 ++ 2 ++ 3 ++ 4 + +Why this repo +------------- + +long contents ..... + +1. e +2. f +3. g +4. h + +Why this repo +------------- + ++ 5 ++ 6 ++ 7 ++ 8 + +Why this repo +------------- + +long contents ..... + +1. a +2. b +3. c +4. d + +Why this repo +------------- + +long contents ..... + ++ 1 ++ 2 ++ 3 ++ 4 + +Why this repo +------------- + +long contents ..... + +1. e +2. f +3. g +4. h + +Why this repo +------------- + ++ 5 ++ 6 ++ 7 ++ 8 + +[1]: https://github.com/allejo/jekyll-toc