|
3 | 3 | A minimal, tiny jQuery plugin that will generate a table of contents, drawing from headings on the |
4 | 4 | page. |
5 | 5 |
|
6 | | -## Usage |
| 6 | +The generated TOCs are semantic, nested lists (`ul` or `ol`) with hash-link anchors to the headings. |
7 | 7 |
|
8 | | -After including jQuery (>= 1.6) and jquery.toc.js on your page, run a script like this: |
| 8 | +## Usage |
9 | 9 |
|
10 | | - $("#toc").toc(); |
| 10 | +Include jQuery (>= 1.6) and jquery.toc.js on your page. The plugin can then be used either via HTML5 |
| 11 | +data attributes, or via the programmatic API. |
11 | 12 |
|
12 | | -This will generate the TOC from all `h1`, `h2`, `h3`, and `h4` tags on the page, and insert it into |
13 | | -`#toc` (for example, `<div id="toc"></div>`.) The list itself will use `ul` tags, and each heading |
14 | | -on the page will get a nice ID, if it doesn't already have one. The IDs will be based on the text |
15 | | -content of the heading. |
| 13 | +### Via data attributes |
16 | 14 |
|
17 | | -### Options |
| 15 | +Minimal example: |
18 | 16 |
|
19 | | -The plugin has two arguments, both optional: |
| 17 | + <ul data-toc></ul> |
20 | 18 |
|
21 | | - $(...).toc(contentContainer, options); |
| 19 | +With options: |
22 | 20 |
|
23 | | -`contentContainer` is a selector where the plugin will look for headings to build up the TOC. |
| 21 | + <ol data-toc="div.container" data-toc-headings="h2,h3,h4"></ol> |
24 | 22 |
|
25 | | -`options` is a hash, with these keys: (all optional) |
| 23 | +### Via the JavaScript programmatic API |
26 | 24 |
|
27 | | -* `headings` is a string with a comma-separated list of selectors to be used as headings, in the |
28 | | - order which defines their relative heirarchy level. The default value of `"h1,h2,h3,h4"` will |
29 | | - select all `h1`, `h2`, `h3`, and `h4` to build the TOC, with `h1` being a level 1, `h2` a level 2, |
30 | | - and so on. You can use any valid list of jQuery selectors; for example, if you just want `h1` tags |
31 | | - with a specific class, and no `h4` tags, you could use `"h1.title,h2,h3"` for this option. |
32 | | -* `list` is the tag that's created for the lists. Valid options are `"ul"` and `"ol"`. |
| 25 | +Minimal example: |
33 | 26 |
|
34 | | -The default values are: |
| 27 | + <ul id="toc"></ul> |
| 28 | + ... |
| 29 | + <script type="text/javascript"> |
| 30 | + $("#toc").toc(); |
| 31 | + </script> |
35 | 32 |
|
36 | | - $(...).toc("body", {headings: "h1,h2,h3,h4", list: "ul"}); |
| 33 | +With options: |
37 | 34 |
|
38 | | -### Examples of Usage |
| 35 | + <ul id="toc"></ul> |
| 36 | + ... |
| 37 | + <script type="text/javascript"> |
| 38 | + $("#toc").toc("div.container", "h2,h3,h4"); |
| 39 | + </script> |
39 | 40 |
|
40 | | -Build a TOC using headings from a specific container element: |
| 41 | +### Options |
41 | 42 |
|
42 | | - $("#toc").toc("#container"); |
| 43 | + <ul data-toc="contentContainer" data-toc-headings="headings"></ul> |
43 | 44 |
|
44 | | -Build a TOC using specific kinds of headings: |
| 45 | + $(...).toc(contentContainer, headings); |
45 | 46 |
|
46 | | - $("#toc").toc("body", {headings: "h2.title,h3"}); |
| 47 | +The plugin has two parameters, both optional: |
47 | 48 |
|
48 | | -Build a TOC using numbered lists: |
| 49 | +* `contentContainer` is a selector where the plugin will look for headings to build up the TOC. The |
| 50 | + default value is `"body"`. |
| 51 | +* `headings` is a string with a comma-separated list of selectors to be used as headings, in the |
| 52 | + order which defines their relative heirarchy level. The default value of `"h1,h2,h3,h4"` will |
| 53 | + select all `h1`, `h2`, `h3`, and `h4` to build the TOC, with `h1` being a level 1, `h2` a level 2, |
| 54 | + and so on. You can use any valid list of jQuery selectors; for example, if you just want `h1` tags |
| 55 | + with a specific class, and no `h4` tags, you could use `"h1.title,h2,h3"` for this parameter. |
49 | 56 |
|
50 | | - $("#toc").toc("body", {list: "ol"}); |
| 57 | +In addition, the plugin will create nested lists of the same type (`ul` or `ol`) as the element that |
| 58 | +it's called on. |
51 | 59 |
|
52 | 60 | ### Automatic ID generation |
53 | 61 |
|
54 | 62 | The plugin generates hash-links to the headings on the page, to allow users to jump to the heading |
55 | 63 | by clicking in the generated table of contents. This feature requires that the headings have IDs |
56 | | -assigned; if they do not, the plugin will generate IDs for you. |
| 64 | +assigned; if they do not, the plugin will generate and assign IDs automatically. |
57 | 65 |
|
58 | 66 | The generated IDs are based on the text inside the headings, and uses two simple rules: |
59 | 67 |
|
|
0 commit comments