|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# `@svelte-put/toc` |
| 4 | + |
| 5 | +[![npm.badge]][npm] [![bundlephobia.badge]][bundlephobia] |
| 6 | + |
| 7 | +Svelte `<Toc>` component and `use:toc` action for building table of contents |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +## Table of Contents |
| 12 | + |
| 13 | +<details open> |
| 14 | + <summary>Show / hide</summary> |
| 15 | + |
| 16 | +- [`@svelte-put/toc`](#svelte-puttoc) |
| 17 | + - [Table of Contents](#table-of-contents) |
| 18 | + - [`svelte-put`](#svelte-put) |
| 19 | + - [Installation](#installation) |
| 20 | + - [Acknowledgement](#acknowledgement) |
| 21 | + - [Usage](#usage) |
| 22 | + - [Action](#action) |
| 23 | + - [Component](#component) |
| 24 | + - [Documentation](#documentation) |
| 25 | + - [Typescript support](#typescript-support) |
| 26 | + |
| 27 | +</details> |
| 28 | + |
| 29 | +## `svelte-put` |
| 30 | + |
| 31 | +This package is part of the [@svelte-put][github.monorepo] family. For contributing guideline and more, refer to its [readme][github.monorepo]. |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +```bash |
| 36 | +npm install -D @svelte-put/toc |
| 37 | +``` |
| 38 | + |
| 39 | +```bash |
| 40 | +yarn add -D @svelte-put/toc |
| 41 | +``` |
| 42 | + |
| 43 | +```bash |
| 44 | +pnpm add -D @svelte-put/toc |
| 45 | +``` |
| 46 | + |
| 47 | +## [Changelog][github.changelog] |
| 48 | + |
| 49 | +## Acknowledgement |
| 50 | + |
| 51 | +This package rely on svelte action strategy and attempts to stay minimal. If you are looking for a declarative, component-oriented solution, check out [Janosh][janosh]'s [svelte-toc]. |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +### Action |
| 56 | + |
| 57 | +The `use:toc` action will search for all matching DOM elements and |
| 58 | +transform them to support hash navigation (anchor element with id attribute). |
| 59 | +By default, all heading elements are collected. You can customize the behavior |
| 60 | +with the action parameters [TocParameters][github.api.TocParameters] |
| 61 | + |
| 62 | +<details open> |
| 63 | + <summary>Show / hide</summary> |
| 64 | + |
| 65 | +```svelte |
| 66 | +<script> |
| 67 | + import { toc } from '@svelte-put/toc'; |
| 68 | + import type { TocEventDetails } from '@svelte-put/toc'; |
| 69 | +
|
| 70 | + function onToc(e: CustomEvent<TocEventDetails>) { |
| 71 | + const { items } = e.detail.items; |
| 72 | + for (const item of items) { |
| 73 | + const { id, element, text, p, anchor } = item; |
| 74 | + // do stuff |
| 75 | + console.log(item); |
| 76 | + } |
| 77 | + } |
| 78 | +</script> |
| 79 | +
|
| 80 | +<svelte:body use:toc on:toc={onToc} /> |
| 81 | +
|
| 82 | +<h1>Heading</h1> |
| 83 | +<h2 id="manual-id">Some other content</h2> |
| 84 | +<h2>Content without explicit id will be slugified into an id with maximum length of 50 characters</h2> |
| 85 | +
|
| 86 | +``` |
| 87 | + |
| 88 | +</details> |
| 89 | + |
| 90 | +After `toc` has completed its execution, the HTML in example above will be transformed, to: |
| 91 | + |
| 92 | +<details open> |
| 93 | + <summary>Show / hide</summary> |
| 94 | + |
| 95 | +```html |
| 96 | +<h1 class="toc-element" style="position: relative;"> |
| 97 | + <a |
| 98 | + class="toc-anchor" |
| 99 | + href="#heading" |
| 100 | + style="position: relative;" |
| 101 | + > |
| 102 | + Heading |
| 103 | + </a> |
| 104 | + <p |
| 105 | + class="toc-p" |
| 106 | + id="note" |
| 107 | + style="bottom: 100%; position: absolute; visibility: hidden; margin-top: -96px; height: 96px;" |
| 108 | + ></p> |
| 109 | +</h1> |
| 110 | + |
| 111 | +<h2 class="toc-element" style="position: relative;"> |
| 112 | + <a |
| 113 | + class="toc-anchor" |
| 114 | + href="#manual-id" |
| 115 | + style="position: relative;" |
| 116 | + > |
| 117 | + Some other content |
| 118 | + </a> |
| 119 | + <p |
| 120 | + class="toc-p" |
| 121 | + id="note" |
| 122 | + style="bottom: 100%; position: absolute; visibility: hidden; margin-top: -96px; height: 96px;" |
| 123 | + ></p> |
| 124 | +</h2> |
| 125 | + |
| 126 | +<h2 class="toc-element" style="position: relative;"> |
| 127 | + <a |
| 128 | + class="toc-anchor" |
| 129 | + href="#content-without-explicit-id-will-be-slugified-into" |
| 130 | + style="position: relative;" |
| 131 | + > |
| 132 | + Content without explicit id will be slugified into an id with maximum length of 50 characters |
| 133 | + </a> |
| 134 | + <p |
| 135 | + class="toc-p" |
| 136 | + id="note" |
| 137 | + style="bottom: 100%; position: absolute; visibility: hidden; margin-top: -96px; height: 96px;" |
| 138 | + ></p> |
| 139 | +</h2> |
| 140 | +``` |
| 141 | + |
| 142 | +</details> |
| 143 | + |
| 144 | +### Component |
| 145 | + |
| 146 | +The exported `Toc` component internally uses `<svelte:body use:toc />`; customization can be done by |
| 147 | +providing [TocParameters][github.api.TocParameters] through the `parameters` prop. |
| 148 | + |
| 149 | +The component will print out a simple `ul` with some default margin for `li` depending on the heading level. For a simple blog post, this should be enough. For more complex use cases, you can override one of [TocSlots][github.api.TocSlots]. |
| 150 | + |
| 151 | +If the component interface does not provide enough customization flexibility for you, consider open an [issue][github.issues] to discuss or use the `use:toc` action interface instead. |
| 152 | + |
| 153 | +Consider the same example as in the previous section but now we use the component instead: |
| 154 | + |
| 155 | +<details open> |
| 156 | + <summary>Show / hide</summary> |
| 157 | + |
| 158 | +```svelte |
| 159 | +<script> |
| 160 | + import Toc from '@svelte-put/toc/Toc.svelte'; |
| 161 | +</script> |
| 162 | +
|
| 163 | +<Toc on:toc> |
| 164 | + <svelte:fragment slot="anchor" let:item> |
| 165 | + <a href="#{item.id}" class="custom-anchor-tag">{item.text}</a> |
| 166 | + </svelte:fragment> |
| 167 | +</Toc> |
| 168 | +
|
| 169 | +<!-- some headings like the previous example --> |
| 170 | +``` |
| 171 | + |
| 172 | +</details> |
| 173 | + |
| 174 | +The HTML generated will look similar to |
| 175 | + |
| 176 | +<details open> |
| 177 | + <summary>Show / hide</summary> |
| 178 | + |
| 179 | +```html |
| 180 | +<ul class="toc-ul"> |
| 181 | + <li class="toc-li toc-li--h1"> |
| 182 | + <a class="custom-anchor-tag" href="#heading"> |
| 183 | + Heading |
| 184 | + </a> |
| 185 | + </li> |
| 186 | + <li class="toc-li toc-li--h2"> |
| 187 | + <a class="custom-anchor-tag" href="#manual-id"> |
| 188 | + Some other content |
| 189 | + </a> |
| 190 | + </li> |
| 191 | + <li class="toc-li toc-li--h2"> |
| 192 | + <a class="custom-anchor-tag" href="#content-without-explicit-id-will-be-slugified-into"> |
| 193 | + Content without explicit id will be slugified into an id with maximum length of 50 characters |
| 194 | + </a> |
| 195 | + </li> |
| 196 | +</ul> |
| 197 | +``` |
| 198 | + |
| 199 | +And the CSS for this list is |
| 200 | + |
| 201 | + |
| 202 | +<details open> |
| 203 | + <summary>Show / hide</summary> |
| 204 | + |
| 205 | +```css |
| 206 | +.toc-li.toc-li--h1 { |
| 207 | + font-weight: bold; |
| 208 | +} |
| 209 | +.toc-li.toc-li--h2 { |
| 210 | + margin-left: 1rem; |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +</details> |
| 215 | + |
| 216 | + |
| 217 | +</details> |
| 218 | + |
| 219 | +## Documentation |
| 220 | + |
| 221 | +### Typescript support |
| 222 | + |
| 223 | +<details open> |
| 224 | + <summary> app.d.ts: show / hide </summary> |
| 225 | + |
| 226 | +```typescript |
| 227 | +/// <reference types="@sveltejs/kit" /> |
| 228 | +/// <reference types="svelte" /> |
| 229 | + |
| 230 | +// Typescript support in svelte-kit, see |
| 231 | +// https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-using-an-attributeevent-on-a-dom-element-and-it-throws-a-type-error |
| 232 | + |
| 233 | +declare namespace svelte.JSX { |
| 234 | + interface HTMLAttributes<T> { |
| 235 | + // on:toc |
| 236 | + ontoc?: (event: CustomEvent<import('@svelte-put/toc').TocEventDetails>) => void; |
| 237 | + } |
| 238 | +} |
| 239 | +``` |
| 240 | + |
| 241 | +</details> |
| 242 | + |
| 243 | +For detailed documentation, see the [extracted API][github.api]. |
| 244 | + |
| 245 | +<!-- github specifics --> |
| 246 | +[github.monorepo]: https://github.com/vnphanquang/svelte-put |
| 247 | +[github.changelog]: https://github.com/vnphanquang/svelte-put/blob/main/packages/actions/toc/CHANGELOG.md |
| 248 | +[github.issues]: https://github.com/vnphanquang/svelte-put/issues?q= |
| 249 | +[github.api]: https://github.com/vnphanquang/svelte-put/blob/main/packages/actions/toc/api/docs/index.md |
| 250 | +[github.api.TocProps]: https://github.com/vnphanquang/svelte-put/blob/main/packages/actions/toc/api/docs/toc.tocprops.md |
| 251 | +[github.api.TocSlots]: https://github.com/vnphanquang/svelte-put/blob/main/packages/actions/toc/api/docs/toc.tocslots.md |
| 252 | +[github.api.TocParameters]: https://github.com/vnphanquang/svelte-put/blob/main/packages/actions/toc/api/docs/toc.tocparameters.md |
| 253 | + |
| 254 | +<!-- heading badge --> |
| 255 | +[npm.badge]: https://img.shields.io/npm/v/@svelte-put/toc |
| 256 | +[npm]: https://www.npmjs.com/package/@svelte-put/toc |
| 257 | +[bundlephobia.badge]: https://img.shields.io/bundlephobia/minzip/@svelte-put/toc?label=minzipped |
| 258 | +[bundlephobia]: https://bundlephobia.com/package/@svelte-put/toc |
| 259 | + |
| 260 | +<!-- external resources --> |
| 261 | +[svelte-toc]: https://github.com/janosh/svelte-toc |
| 262 | +[janosh]: https://github.com/janosh |
0 commit comments