Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom toc position #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,16 @@ plugins:

##### for Heading and TOC

* `toc`

Generate a Table of Contents.
**default**: `true`
_**since**: <!-- add version -->_

* `toc_title`

Set the title text of _Table of Content_.
**default**: `Table of Content`
Set the title text of _Table of Contents_.
**default**: `Table of Contents`
_**since**: `v0.4.0`_

* `heading_shift`
Expand All @@ -194,6 +200,24 @@ plugins:
Set the page `id` of `nav` url. If the `id` matches in this list, it will be excluded from the heading number addition and table of contents.
**default**: `[]`

* Changing the position of the TOC

By default, the TOC is inserted at the beginning of the document.
You can change the position of the TOC by adding a `- toc` entry to the nav section.

```
nav:
- Foreword: foreword.md
- toc
- Introduction: introduction.md
...
```

Note: The TOC must be toplevel.
It can not be inserted in a nested docment section.

_**since**: <!-- add version -->_

##### for Page

* `exclude_pages`
Expand Down
5 changes: 4 additions & 1 deletion mkdocs_with_pdf/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ def add_stylesheet(stylesheet: str):
add_stylesheet(self._theme.get_stylesheet(self._options.debug_html))

for page in self._nav:
content = self._get_content(soup, page)
if getattr(page, "url", None) == "toc" and self._options.toc:
content = soup.new_tag('article', id='doc-toc')
else:
content = self._get_content(soup, page)
if content:
soup.body.append(content)

Expand Down
2 changes: 2 additions & 0 deletions mkdocs_with_pdf/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Options(object):
('custom_template_path',
config_options.Type(str, default="templates")),

('toc', config_options.Type(bool, default=True)),
('toc_title', config_options.Type(str, default="Table of contents")),
('heading_shift', config_options.Type(bool, default=True)),
('toc_level', config_options.Type(int, default=2)),
Expand Down Expand Up @@ -75,6 +76,7 @@ def __init__(self, local_config, config, logger: logging):
self.custom_template_path = local_config['custom_template_path']

# TOC and Chapter heading
self.toc = local_config['toc']
self.toc_title = local_config['toc_title']
self.heading_shift = local_config['heading_shift']
self.toc_level = local_config['toc_level']
Expand Down
19 changes: 16 additions & 3 deletions mkdocs_with_pdf/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def make_indexes(soup: PageElement, options: Options) -> None:
options {Options} -- The options of this sequence.
"""

if options.toc:
_make_indexes(soup, options)


def _make_indexes(soup: PageElement, options: Options) -> None:
# Step 1: (re)ordered headdings
_inject_heading_order(soup, options)

Expand Down Expand Up @@ -40,7 +45,11 @@ def makeLink(h: Tag) -> Tag:
options.logger.debug(f"| [{h.get_text(separator=' ')}]({ref})")
return li

toc = soup.new_tag('article', id='doc-toc')
toc = soup.find(id='doc-toc')
custom_doc_pos = True
if not toc:
toc = soup.new_tag('article', id='doc-toc')
custom_doc_pos = False
title = soup.new_tag('h1')
title.append(soup.new_string(options.toc_title))
toc.append(title)
Expand All @@ -51,6 +60,9 @@ def makeLink(h: Tag) -> Tag:
headings = soup.find_all(['h1', 'h2', 'h3'])
for h in headings:

if h.find(string=options.toc_title):
continue

if h.name == 'h1':

h1li = makeLink(h)
Expand Down Expand Up @@ -84,8 +96,9 @@ def makeLink(h: Tag) -> Tag:
else:
continue
pass

soup.body.insert(0, toc)

if not custom_doc_pos:
soup.body.insert(0, toc)


def _inject_heading_order(soup: Tag, options: Options):
Expand Down