Skip to content

Commit 49540ff

Browse files
authored
Merge pull request #1029 from jcb91/toc
[toc2] add option toskip h1 headings for use as notebook title
2 parents 6ba5626 + 06b9523 commit 49540ff

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ The toc2 extension enables to collect all running headers and display them in a
1010
#### Second demo:
1111
![](demo2.gif)
1212

13-
The table of contents is automatically updated when modifications occur in the notebook. The toc window can be moved and resized. It can be docked as a sidebar or dragged from the sidebar into a floating window. The table of contents can be collapsed or the window can be completely hidden. The navigation menu can be enabled/disabled via the nbextensions configuration utility. It can also be resized. The position, dimensions, and states (that is 'collapsed' and 'hidden' states) are remembered (actually stored in the notebook's metadata) and restored on the next session. Headers can be skipped from being inserted in the toc by adding the html tag "<a class='tocSkip'>" at the end of the header line; eg in
13+
The table of contents is automatically updated when modifications occur in the notebook. The toc window can be moved and resized. It can be docked as a sidebar or dragged from the sidebar into a floating window. The table of contents can be collapsed or the window can be completely hidden. The navigation menu can be enabled/disabled via the nbextensions configuration utility. It can also be resized. The position, dimensions, and states (that is 'collapsed' and 'hidden' states) are remembered (actually stored in the notebook's metadata) and restored on the next session.
14+
15+
There is a configurable option to skip h1 headers from the ToC, to allow their use as a notebook
16+
title. However, this cause issues in latex exports, where h1 are converted to sections.
17+
Alternatively, headers of any level can be omitted from being the toc by adding an html tag with the
18+
css class `tocSkip` at the end of the header line; e.g. as in
19+
1420
```
15-
## title <a class='tocSkip'>"
21+
## title <a class="tocSkip">
1622
```
1723

1824
The toc window also provides two links in its header for further functionalities:
@@ -34,6 +40,7 @@ The initial configuration can be given using the IPython-contrib nbextensions fa
3440
- Widening the display area to fit the browser window (may be useful with sidebar option; default: true)
3541
- The numbering of headers (true by default)
3642
- Moving header title and menus on the left (default: true)
43+
- Skipping h1 headers, useful if you want to use h1 as unnumbered notebook title (default: false)
3744
- Customization of highlighting the title of currently selected/running sections.
3845
- Customization of background, fonts, border and highlighting colors in the toc window and navigation menus (as third demo).
3946

@@ -107,3 +114,5 @@ This option requires the IPython kernel and is not present with other kernels.
107114
- Updated README to please @KadeG in #871
108115
- @hiiwave, april 2017.
109116
- Support customization of background, fonts, border and highlighting colors in the toc window and navigation menus with PR [#969](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/pull/969)
117+
- @jfbercher, @louisabraham, @jcb91 July 2017. Add support for skipping h1
118+
headings, enabling their use as unnumbered notebook titles

src/jupyter_contrib_nbextensions/nbextensions/toc2/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ define(["require", "jquery", "base/js/namespace", 'services/config',
3232
'sidebar_border': '#EEEEEE',
3333
'navigate_text': '#333333',
3434
'navigate_num': '#000000',
35-
}
35+
},
36+
skip_h1_title: false,
3637
}
3738

3839
//.....................global variables....

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,15 @@ var table_of_contents = function (cfg,st) {
535535
var depth = 1; //var depth = ol_depth(ol);
536536
var li= ul;//yes, initialize li with ul!
537537
var all_headers= $("#notebook").find(":header");
538-
var min_lvl=1, lbl_ary= [];
538+
var min_lvl = 1 + Number(Boolean(cfg.skip_h1_title)), lbl_ary = [];
539539
for(; min_lvl <= 6; min_lvl++){ if(all_headers.is('h'+min_lvl)){break;} }
540540
for(var i= min_lvl; i <= 6; i++){ lbl_ary[i - min_lvl]= 0; }
541541

542542
//loop over all headers
543543
all_headers.each(function (i, h) {
544544
var level = parseInt(h.tagName.slice(1), 10) - min_lvl + 1;
545-
// skip below threshold
546-
if (level > cfg.threshold){ return; }
545+
// skip below threshold, or h1 ruled out by cfg.skip_h1_title
546+
if (level < 1 || level > cfg.threshold){ return; }
547547
// skip headings with no ID to link to
548548
if (!h.id){ return; }
549549
// skip toc cell if present

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Parameters:
1616
min: -1
1717
step: 1
1818
default: 4
19+
20+
- name: toc2.skip_h1_title
21+
description: |
22+
Skip h1 headings from numbering, so that they can serve as a notebook title.
23+
See the README for details, caveats and alternatives
24+
input_type: checkbox
25+
default: false
26+
1927
- name: toc2.toc_cell
2028
description: Add a Table of Contents cell at the top of the notebook
2129
input_type: checkbox

0 commit comments

Comments
 (0)