@mdsvex/plugin-toc 0.0.0-stage → 0.1.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +197 -2
- package/dist/Toc.svelte +61 -0
- package/dist/Toc.svelte.d.ts +23 -0
- package/dist/directives.d.ts +1 -0
- package/dist/directives.js +2 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +209 -0
- package/dist/tree.d.ts +33 -0
- package/dist/tree.js +23 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 pngwn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,198 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mdsvex/plugin-toc
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A table of contents for [mdsvex](https://mdsvex.pngwn.io): the headings of each document as data, a component that renders them as a nested list, and a `::toc` directive that puts that list in the document.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add --save-dev @mdsvex/plugin-toc@next
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
// vite.config.js
|
|
11
|
+
import { mdsvex } from 'mdsvex';
|
|
12
|
+
import { toc } from '@mdsvex/plugin-toc';
|
|
13
|
+
|
|
14
|
+
export default {
|
|
15
|
+
plugins: [mdsvex({ heading_ids: true, plugins: [toc()] })],
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```md
|
|
20
|
+
# Getting started
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
### With pnpm
|
|
25
|
+
|
|
26
|
+
## Configure
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import Page, { plugin_data } from './getting-started.svx';
|
|
31
|
+
|
|
32
|
+
plugin_data.toc.headings;
|
|
33
|
+
// [
|
|
34
|
+
// { depth: 2, id: 'install', text: 'Install', position: { start, end } },
|
|
35
|
+
// { depth: 3, id: 'with-pnpm', text: 'With pnpm', position: { start, end } },
|
|
36
|
+
// { depth: 2, id: 'configure', text: 'Configure', position: { start, end } },
|
|
37
|
+
// ]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
It needs Svelte 5.20 or later.
|
|
41
|
+
|
|
42
|
+
## The data
|
|
43
|
+
|
|
44
|
+
`plugin_data.toc.headings` is a flat list, in the order the headings are written. Each entry has:
|
|
45
|
+
|
|
46
|
+
- **`depth`**, 1 to 6.
|
|
47
|
+
- **`id`**, the id the heading has in the page, so `'#' + id` links to it.
|
|
48
|
+
- **`text`**, the text a reader sees, without markup. Emphasis, links and code are their text, an image is its alt, and a character reference such as `&` is its character. What only exists once the page runs is not in it: a `{expression}`, a component, a directive.
|
|
49
|
+
- **`position`**, where the heading is in the source, as `{ start, end }` with an `offset`, a `line` from 1 and a `column` from 0 each.
|
|
50
|
+
|
|
51
|
+
The list is flat because that is what the document holds and what is easy to filter. `toc_tree` nests it:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { toc_tree } from '@mdsvex/plugin-toc';
|
|
55
|
+
|
|
56
|
+
toc_tree(plugin_data.toc.headings);
|
|
57
|
+
// [
|
|
58
|
+
// { depth: 2, id: 'install', text: 'Install', children: [
|
|
59
|
+
// { depth: 3, id: 'with-pnpm', text: 'With pnpm', children: [] },
|
|
60
|
+
// ] },
|
|
61
|
+
// { depth: 2, id: 'configure', text: 'Configure', children: [] },
|
|
62
|
+
// ]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Each heading goes under the nearest heading before it that is less deep, so one that skips a level still has a parent.
|
|
66
|
+
|
|
67
|
+
The data reaches a page the three ways all [plugin data](https://mdsvex.pngwn.io/docs#plugin-data) does: the `plugin_data` export of the document, the `plugin_data` prop of its template, and a `?data` import, which parses a document without rendering it:
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
const pages = import.meta.glob('./docs/*.svx', { query: '?data', eager: true });
|
|
71
|
+
// pages['./docs/intro.svx'].plugin_data?.toc?.headings
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**A document with nothing to list has no `toc` key**, so read it as `plugin_data?.toc?.headings ?? []`. The package types the key, by adding to the `PluginData` interface of mdsvex. That is in effect once TypeScript sees an import of the package, which a `vite.config.ts` gives it. With a config it does not check, add `import type {} from '@mdsvex/plugin-toc';` to a declaration file such as `src/app.d.ts`.
|
|
75
|
+
|
|
76
|
+
## It needs `heading_ids`
|
|
77
|
+
|
|
78
|
+
The plugin makes no ids. It lists the id each heading has after the [`heading_ids`](https://mdsvex.pngwn.io/docs#heading_ids) option of mdsvex has run.
|
|
79
|
+
|
|
80
|
+
- **With `heading_ids` off the compile fails** with a `TocError` whose `code` is `'heading_ids'`, as soon as a document has a heading to list.
|
|
81
|
+
- A heading that ends up with no id, such as `## {title}`, is left out, with a `heading_without_id` warning.
|
|
82
|
+
- An id a parse plugin set is listed like any other.
|
|
83
|
+
- A heading typed as HTML (`<h2 id="x">`) is not a markdown heading and is not listed.
|
|
84
|
+
|
|
85
|
+
A plugin is not told the options of the compile, so the plugin reads this off the document: it fails when no heading has an id and one that would be listed has text an id could be made from.
|
|
86
|
+
|
|
87
|
+
## Options
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
toc({
|
|
91
|
+
min_depth: 1 | 2 | 3 | 4 | 5 | 6, // 2
|
|
92
|
+
max_depth: 1 | 2 | 3 | 4 | 5 | 6, // 3
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The defaults leave out the `#` title of the page and anything below `###`. A value that is no heading depth throws a `TocError` with the `code` `'option'` when the plugin is made.
|
|
97
|
+
|
|
98
|
+
## Leaving a heading out
|
|
99
|
+
|
|
100
|
+
Put a `<!-- no-toc -->` comment in the heading:
|
|
101
|
+
|
|
102
|
+
```md
|
|
103
|
+
## Changelog <!-- no-toc -->
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The heading keeps its id and its anchor. The comment is no part of its text or its id, and a renderer that knows nothing of it, such as GitHub, shows nothing for it.
|
|
107
|
+
|
|
108
|
+
## Which headings
|
|
109
|
+
|
|
110
|
+
Every markdown heading inside the depths is listed, wherever it is written: in a block quote, a list item, an element, a `{#if}` block or a container directive.
|
|
111
|
+
|
|
112
|
+
- **[`@mdsvex/plugin-steps`](https://www.npmjs.com/package/@mdsvex/plugin-steps)** keeps its headings, so each step is listed.
|
|
113
|
+
- **[`@mdsvex/plugin-tabs`](https://www.npmjs.com/package/@mdsvex/plugin-tabs)** turns the headings that start a tab into labels, which have no id, so they are not listed. A heading inside a panel is. The order of the plugins does not matter.
|
|
114
|
+
|
|
115
|
+
## `Toc.svelte`
|
|
116
|
+
|
|
117
|
+
The component renders the list, nested. In a template, give it the data:
|
|
118
|
+
|
|
119
|
+
```svelte
|
|
120
|
+
<script>
|
|
121
|
+
import Toc from '@mdsvex/plugin-toc/Toc.svelte';
|
|
122
|
+
|
|
123
|
+
let { plugin_data, children } = $props();
|
|
124
|
+
</script>
|
|
125
|
+
|
|
126
|
+
<aside>
|
|
127
|
+
<Toc headings={plugin_data?.toc?.headings} aria-label="On this page" />
|
|
128
|
+
</aside>
|
|
129
|
+
<main>{@render children()}</main>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
```html
|
|
133
|
+
<nav aria-label="On this page" class="toc">
|
|
134
|
+
<ul>
|
|
135
|
+
<li>
|
|
136
|
+
<a href="#install">Install</a>
|
|
137
|
+
<ul>
|
|
138
|
+
<li><a href="#with-pnpm">With pnpm</a></li>
|
|
139
|
+
</ul>
|
|
140
|
+
</li>
|
|
141
|
+
<li><a href="#configure">Configure</a></li>
|
|
142
|
+
</ul>
|
|
143
|
+
</nav>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
| prop | |
|
|
147
|
+
| ------------------------ | ----------------------------------------------------------------------- |
|
|
148
|
+
| `headings` | the flat list, `undefined` renders nothing |
|
|
149
|
+
| `min_depth`, `max_depth` | narrow the list, such as `max_depth={2}` for a sidebar |
|
|
150
|
+
| `label` | a snippet, rendered in a `<p class="toc-label">` that names the `<nav>` |
|
|
151
|
+
| `class` | added to `toc` |
|
|
152
|
+
| anything else | an attribute of the `<nav>`, such as `aria-label` |
|
|
153
|
+
|
|
154
|
+
With nothing to list it renders nothing, not an empty `<nav>`. It ships no stylesheet.
|
|
155
|
+
|
|
156
|
+
## `::toc`
|
|
157
|
+
|
|
158
|
+
On a line of its own, `::toc` renders the same component in the document:
|
|
159
|
+
|
|
160
|
+
```md
|
|
161
|
+
# Getting started
|
|
162
|
+
|
|
163
|
+
::toc[On this page]
|
|
164
|
+
|
|
165
|
+
## Install
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
- **The brackets are the label.** `::toc` alone has none.
|
|
169
|
+
- **`::toc(max_depth=2)`** and `min_depth` narrow that list. The data keeps every entry.
|
|
170
|
+
- **Any other arg is an attribute of the `<nav>`**, such as `::toc(class=inline)`.
|
|
171
|
+
- **It lists the whole document**, the headings after it as well. Each heading is added to the data as it closes, with its id, and the component reads the data from the context of the document.
|
|
172
|
+
- **While a document streams**, the list grows as headings arrive, in a view that gives `Node.svelte` the live data (`PluginStream` in `@mdsvex/render/stream`).
|
|
173
|
+
- **With nothing to list it renders nothing**, and the build warns `no_headings`.
|
|
174
|
+
- A `toc` export from your own directives module replaces the component. It gets the `label` snippet and the args, and reads the headings from the context, `getContext(Symbol.for('mdsvex.plugin_data')).current.toc.headings`. Type what it reads as `PluginDataContext` from `mdsvex/compile`, and the headings are `TocHeading[]`.
|
|
175
|
+
|
|
176
|
+
## Warnings
|
|
177
|
+
|
|
178
|
+
| code | when |
|
|
179
|
+
| -------------------- | ---------------------------------------------------------------------------------------- |
|
|
180
|
+
| `skipped_level` | an entry is more than one level below the heading before it, such as a `###` after a `#` |
|
|
181
|
+
| `heading_without_id` | a heading inside the depths has no id and is left out |
|
|
182
|
+
| `no_headings` | a `::toc` has nothing to list |
|
|
183
|
+
| `bad_depth` | `min_depth` or `max_depth` of a `::toc` is no heading depth, the arg is ignored |
|
|
184
|
+
| `not_a_leaf` | `toc` was written as a container (`:::toc`) or inline (`:toc[]`), which list nothing |
|
|
185
|
+
|
|
186
|
+
## With `compile`
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
import { compile } from 'mdsvex';
|
|
190
|
+
|
|
191
|
+
const { data, warnings } = compile(source, {
|
|
192
|
+
heading_ids: true,
|
|
193
|
+
plugins: [toc()],
|
|
194
|
+
});
|
|
195
|
+
data?.toc?.headings;
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
`@mdsvex/parse` used on its own makes no heading ids, so there the plugin lists only the headings another plugin gave an id. A tree built from the wire format has no positions, so its entries have no `position`.
|
package/dist/Toc.svelte
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { getContext } from 'svelte';
|
|
3
|
+
import { toc_tree } from './tree.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {{
|
|
7
|
+
* headings?: import('./tree.js').TocHeading[] | string,
|
|
8
|
+
* min_depth?: number | string,
|
|
9
|
+
* max_depth?: number | string,
|
|
10
|
+
* label?: import('svelte').Snippet,
|
|
11
|
+
* children?: import('svelte').Snippet,
|
|
12
|
+
* class?: import('svelte/elements').ClassValue,
|
|
13
|
+
* [attribute: string]: unknown,
|
|
14
|
+
* }} Props
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** @type {Props} */
|
|
18
|
+
let {
|
|
19
|
+
headings,
|
|
20
|
+
min_depth = 1,
|
|
21
|
+
max_depth = 6,
|
|
22
|
+
label,
|
|
23
|
+
children: _children,
|
|
24
|
+
class: class_name,
|
|
25
|
+
...rest
|
|
26
|
+
} = $props();
|
|
27
|
+
|
|
28
|
+
const id = $props.id();
|
|
29
|
+
|
|
30
|
+
// what the plugin wrote, from a compiled document or a stream, current changes as headings arrive
|
|
31
|
+
/** @type {import('mdsvex/compile').PluginDataContext | undefined} */
|
|
32
|
+
const data = getContext(Symbol.for('mdsvex.plugin_data'));
|
|
33
|
+
|
|
34
|
+
const tree = $derived.by(() => {
|
|
35
|
+
// an arg is always a string, so headings written in a directive are json
|
|
36
|
+
/** @type {import('./tree.js').TocHeading[]} */
|
|
37
|
+
const all =
|
|
38
|
+
typeof headings === 'string'
|
|
39
|
+
? JSON.parse(headings)
|
|
40
|
+
: (headings ?? data?.current?.toc?.headings ?? []);
|
|
41
|
+
const min = Number(min_depth);
|
|
42
|
+
const max = Number(max_depth);
|
|
43
|
+
return toc_tree(all.filter((heading) => heading.depth >= min && heading.depth <= max));
|
|
44
|
+
});
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
{#snippet list(/** @type {import('./tree.js').TocNode[]} */ nodes)}
|
|
48
|
+
<ul>
|
|
49
|
+
{#each nodes as node (node)}
|
|
50
|
+
<li>
|
|
51
|
+
<a href="#{node.id}">{node.text}</a>{#if node.children.length > 0}{@render list(node.children)}{/if}
|
|
52
|
+
</li>
|
|
53
|
+
{/each}
|
|
54
|
+
</ul>
|
|
55
|
+
{/snippet}
|
|
56
|
+
|
|
57
|
+
{#if tree.length > 0}
|
|
58
|
+
<nav aria-labelledby={label ? `${id}-label` : undefined} {...rest} class={['toc', class_name]}>
|
|
59
|
+
{#if label}<p class="toc-label" id="{id}-label">{@render label()}</p>{/if}{@render list(tree)}
|
|
60
|
+
</nav>
|
|
61
|
+
{/if}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default Toc;
|
|
2
|
+
type Toc = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<Props>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const Toc: import("svelte").Component<{
|
|
7
|
+
[attribute: string]: unknown;
|
|
8
|
+
headings?: import("./tree.js").TocHeading[] | string;
|
|
9
|
+
min_depth?: number | string;
|
|
10
|
+
max_depth?: number | string;
|
|
11
|
+
label?: import("svelte").Snippet;
|
|
12
|
+
children?: import("svelte").Snippet;
|
|
13
|
+
class?: import("svelte/elements").ClassValue;
|
|
14
|
+
}, {}, "">;
|
|
15
|
+
type Props = {
|
|
16
|
+
headings?: import("./tree.js").TocHeading[] | string;
|
|
17
|
+
min_depth?: number | string;
|
|
18
|
+
max_depth?: number | string;
|
|
19
|
+
label?: import("svelte").Snippet;
|
|
20
|
+
children?: import("svelte").Snippet;
|
|
21
|
+
class?: import("svelte/elements").ClassValue;
|
|
22
|
+
[attribute: string]: unknown;
|
|
23
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as toc } from './Toc.svelte';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Bundle } from 'mdsvex/compile';
|
|
2
|
+
import type { TocHeading } from './tree.js';
|
|
3
|
+
export { toc_tree } from './tree.js';
|
|
4
|
+
export type { TocHeading, TocNode, TocPoint } from './tree.js';
|
|
5
|
+
export interface TocOptions {
|
|
6
|
+
/** the least deep heading in the table, 2 by default, which leaves the title of the page out */
|
|
7
|
+
min_depth?: number;
|
|
8
|
+
/** the deepest heading in the table, 3 by default */
|
|
9
|
+
max_depth?: number;
|
|
10
|
+
}
|
|
11
|
+
/** what the plugin leaves in `plugin_data.toc`, a document with no entry has no such key */
|
|
12
|
+
export interface TocData {
|
|
13
|
+
/** in the order they are written, `toc_tree` nests them */
|
|
14
|
+
headings: TocHeading[];
|
|
15
|
+
}
|
|
16
|
+
declare module 'mdsvex/compile' {
|
|
17
|
+
interface PluginData {
|
|
18
|
+
toc?: TocData;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export type TocErrorCode = 'option' | 'heading_ids';
|
|
22
|
+
/** an option that is no heading depth, or a document whose headings have no ids */
|
|
23
|
+
export declare class TocError extends Error {
|
|
24
|
+
code: TocErrorCode;
|
|
25
|
+
/** the document, undefined for an option and when the compile was given no filename */
|
|
26
|
+
filename: string | undefined;
|
|
27
|
+
constructor(code: TocErrorCode, message: string, filename?: string);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* a table of contents as data, the headings of a document in
|
|
31
|
+
* `plugin_data.toc.headings`, added as each heading arrives, and a `::toc`
|
|
32
|
+
* directive whose component lists them from that data
|
|
33
|
+
*
|
|
34
|
+
* the ids come from the `heading_ids` option of mdsvex, which has to be on
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* import { toc } from '@mdsvex/plugin-toc';
|
|
38
|
+
*
|
|
39
|
+
* mdsvex({ heading_ids: true, plugins: [toc()] });
|
|
40
|
+
*/
|
|
41
|
+
export declare function toc(options?: TocOptions): Bundle;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
export { toc_tree } from './tree.js';
|
|
2
|
+
const PACKAGE = '@mdsvex/plugin-toc';
|
|
3
|
+
/** the key of plugin_data, the name of the directive and of its component */
|
|
4
|
+
const NAME = 'toc';
|
|
5
|
+
/** a heading that holds this comment is left out */
|
|
6
|
+
const MARKER = 'no-toc';
|
|
7
|
+
/** an option that is no heading depth, or a document whose headings have no ids */
|
|
8
|
+
export class TocError extends Error {
|
|
9
|
+
constructor(code, message, filename) {
|
|
10
|
+
super(`[${PACKAGE}] ${message}`);
|
|
11
|
+
this.name = 'TocError';
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.filename = filename;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function depth_option(name, value, fallback) {
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
return fallback;
|
|
19
|
+
if (typeof value === 'number' &&
|
|
20
|
+
Number.isInteger(value) &&
|
|
21
|
+
value >= 1 &&
|
|
22
|
+
value <= 6)
|
|
23
|
+
return value;
|
|
24
|
+
throw new TocError('option', `${name} must be a heading depth from 1 to 6, got ${JSON.stringify(value)}`);
|
|
25
|
+
}
|
|
26
|
+
const REFERENCE = /&(?:#(\d{1,7})|#[xX]([0-9a-fA-F]{1,6})|(amp|lt|gt|quot|apos|nbsp));/g;
|
|
27
|
+
const NAMED = {
|
|
28
|
+
amp: '&',
|
|
29
|
+
lt: '<',
|
|
30
|
+
gt: '>',
|
|
31
|
+
quot: '"',
|
|
32
|
+
apos: "'",
|
|
33
|
+
nbsp: String.fromCharCode(0xa0),
|
|
34
|
+
};
|
|
35
|
+
/** a numeric reference and the few named ones html needs are their character, any other stays as written */
|
|
36
|
+
function reference(_match, decimal, hex, name) {
|
|
37
|
+
if (name !== undefined)
|
|
38
|
+
return NAMED[name];
|
|
39
|
+
const code = decimal !== undefined ? parseInt(decimal, 10) : parseInt(hex, 16);
|
|
40
|
+
if (code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff))
|
|
41
|
+
return String.fromCharCode(0xfffd);
|
|
42
|
+
return String.fromCodePoint(code);
|
|
43
|
+
}
|
|
44
|
+
const is_element = /^[a-z][^.:]*$/;
|
|
45
|
+
/** the text heading_ids makes the id from, an image is its alt, what svelte or a component renders is nothing */
|
|
46
|
+
function text_of(node) {
|
|
47
|
+
let out = '';
|
|
48
|
+
for (let child = node.first_child; child !== null; child = child.next) {
|
|
49
|
+
switch (child.type) {
|
|
50
|
+
case 'text': {
|
|
51
|
+
const text = child.text_content;
|
|
52
|
+
out +=
|
|
53
|
+
text.indexOf('&') === -1 ? text : text.replace(REFERENCE, reference);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
// code shows a reference as written
|
|
57
|
+
case 'code_span':
|
|
58
|
+
out += child.text_content;
|
|
59
|
+
break;
|
|
60
|
+
case 'mustache':
|
|
61
|
+
case 'svelte_tag':
|
|
62
|
+
case 'svelte_block':
|
|
63
|
+
case 'html_comment':
|
|
64
|
+
case 'directive_inline':
|
|
65
|
+
case 'directive_leaf':
|
|
66
|
+
case 'directive_container':
|
|
67
|
+
break;
|
|
68
|
+
case 'html': {
|
|
69
|
+
const tag = child.attrs.tag;
|
|
70
|
+
if (typeof tag === 'string' && is_element.test(tag))
|
|
71
|
+
out += text_of(child);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
default:
|
|
75
|
+
out += text_of(child);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
/** anywhere inside, an anchor that wraps the content of the heading holds the comment too */
|
|
81
|
+
function is_marked(node) {
|
|
82
|
+
for (let child = node.first_child; child !== null; child = child.next) {
|
|
83
|
+
if (child.type === 'html_comment') {
|
|
84
|
+
if (child.text_content.trim() === MARKER)
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
else if (is_marked(child))
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
const DEPTH = /^[1-6]$/;
|
|
93
|
+
/** a depth written as an arg of the directive, the fallback and a warning for anything else */
|
|
94
|
+
function depth_arg(list, ctx, name, fallback, warn) {
|
|
95
|
+
const value = list.attrs.args?.[name];
|
|
96
|
+
if (value === undefined)
|
|
97
|
+
return fallback;
|
|
98
|
+
if (DEPTH.test(value))
|
|
99
|
+
return Number(value);
|
|
100
|
+
if (warn)
|
|
101
|
+
ctx.warn('bad_depth', `${name} of ::${NAME} must be a heading depth from 1 to 6, got ${JSON.stringify(value)}`, list);
|
|
102
|
+
return fallback;
|
|
103
|
+
}
|
|
104
|
+
const HAS_WORD = /[\p{L}\p{N}]/u;
|
|
105
|
+
function seen_of(ctx) {
|
|
106
|
+
var _a;
|
|
107
|
+
return ((_a = ctx.scratch).seen ?? (_a.seen = { before: 0, any_id: false, sluggable: false }));
|
|
108
|
+
}
|
|
109
|
+
/** the entry of a heading as it closes, when it is one the table lists */
|
|
110
|
+
function close_heading(node, ctx, min, max) {
|
|
111
|
+
// a plugin may have made it something else, a label of tabs say
|
|
112
|
+
if (node.type !== 'heading' || is_marked(node))
|
|
113
|
+
return;
|
|
114
|
+
const seen = seen_of(ctx);
|
|
115
|
+
const id = node.attrs.id;
|
|
116
|
+
const has_id = typeof id === 'string' && id !== '';
|
|
117
|
+
if (has_id)
|
|
118
|
+
seen.any_id = true;
|
|
119
|
+
const depth = node.depth;
|
|
120
|
+
const last = seen.before;
|
|
121
|
+
seen.before = depth;
|
|
122
|
+
if (depth < min || depth > max)
|
|
123
|
+
return;
|
|
124
|
+
if (last !== 0 && depth > last + 1) {
|
|
125
|
+
ctx.warn('skipped_level', `a level ${depth} heading follows a level ${last} heading, a level is skipped`, node);
|
|
126
|
+
}
|
|
127
|
+
const text = text_of(node).trim();
|
|
128
|
+
if (!has_id) {
|
|
129
|
+
if (HAS_WORD.test(text))
|
|
130
|
+
seen.sluggable = true;
|
|
131
|
+
ctx.warn('heading_without_id', 'a heading with no id is left out of the table of contents', node);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const heading = { depth, id, text };
|
|
135
|
+
const position = node.position;
|
|
136
|
+
if (position !== undefined && position.end !== undefined) {
|
|
137
|
+
heading.position = {
|
|
138
|
+
start: { ...position.start },
|
|
139
|
+
end: { ...position.end },
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
ctx.add('headings', heading);
|
|
143
|
+
}
|
|
144
|
+
/** once the document is whole, the checks that need every heading */
|
|
145
|
+
function finish(_root, ctx) {
|
|
146
|
+
const seen = seen_of(ctx);
|
|
147
|
+
// the options of the compile are not passed to a plugin, so this is read off the tree
|
|
148
|
+
if (seen.sluggable && !seen.any_id) {
|
|
149
|
+
throw new TocError('heading_ids', `no heading of ${ctx.filename ?? 'the document'} has an id, turn on the heading_ids option of mdsvex, this plugin makes no ids of its own`, ctx.filename);
|
|
150
|
+
}
|
|
151
|
+
const lists = ctx.scratch.lists;
|
|
152
|
+
if (lists === undefined)
|
|
153
|
+
return;
|
|
154
|
+
const headings = (ctx.data.headings ?? []);
|
|
155
|
+
for (const list of lists) {
|
|
156
|
+
const min = depth_arg(list, ctx, 'min_depth', 1, false);
|
|
157
|
+
const max = depth_arg(list, ctx, 'max_depth', 6, false);
|
|
158
|
+
if (!headings.some(({ depth }) => depth >= min && depth <= max)) {
|
|
159
|
+
ctx.warn('no_headings', `::${NAME} has no headings to list and renders nothing`, list);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* a table of contents as data, the headings of a document in
|
|
165
|
+
* `plugin_data.toc.headings`, added as each heading arrives, and a `::toc`
|
|
166
|
+
* directive whose component lists them from that data
|
|
167
|
+
*
|
|
168
|
+
* the ids come from the `heading_ids` option of mdsvex, which has to be on
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* import { toc } from '@mdsvex/plugin-toc';
|
|
172
|
+
*
|
|
173
|
+
* mdsvex({ heading_ids: true, plugins: [toc()] });
|
|
174
|
+
*/
|
|
175
|
+
export function toc(options = {}) {
|
|
176
|
+
const min = depth_option('min_depth', options.min_depth, 2);
|
|
177
|
+
const max = depth_option('max_depth', options.max_depth, 3);
|
|
178
|
+
if (min > max) {
|
|
179
|
+
throw new TocError('option', `min_depth ${min} is deeper than max_depth ${max}`);
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
name: NAME,
|
|
183
|
+
module: `${PACKAGE}/directives`,
|
|
184
|
+
directives: {
|
|
185
|
+
[NAME]: {
|
|
186
|
+
parse(node, ctx) {
|
|
187
|
+
var _a;
|
|
188
|
+
if (node.type !== 'directive_leaf') {
|
|
189
|
+
ctx.warn('not_a_leaf', `${NAME} lists nothing as a container or inline, write ::${NAME} on a line of its own`, node);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
// a block directive has its args by now
|
|
193
|
+
depth_arg(node, ctx, 'min_depth', 1, true);
|
|
194
|
+
depth_arg(node, ctx, 'max_depth', 6, true);
|
|
195
|
+
// for the document hook, in the order they are written, no block directive is revoked
|
|
196
|
+
((_a = ctx.scratch).lists ?? (_a.lists = [])).push(node);
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
parse: {
|
|
201
|
+
heading: {
|
|
202
|
+
parse(node, ctx) {
|
|
203
|
+
return () => close_heading(node, ctx, min, max);
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
document: finish,
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
package/dist/tree.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** a place in the source of a document, line from 1 and column from 0 */
|
|
2
|
+
export interface TocPoint {
|
|
3
|
+
offset: number;
|
|
4
|
+
line: number;
|
|
5
|
+
column: number;
|
|
6
|
+
}
|
|
7
|
+
/** one heading of a document, in the order they are written */
|
|
8
|
+
export interface TocHeading {
|
|
9
|
+
/** 1 to 6 */
|
|
10
|
+
depth: number;
|
|
11
|
+
/** the id the heading has in the page, a link to it is `#` and the id */
|
|
12
|
+
id: string;
|
|
13
|
+
/** the text a reader sees, without markup */
|
|
14
|
+
text: string;
|
|
15
|
+
/** where the heading is in the source, a tree built from the wire has none */
|
|
16
|
+
position?: {
|
|
17
|
+
start: TocPoint;
|
|
18
|
+
end: TocPoint;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** a heading with the deeper headings that follow it */
|
|
22
|
+
export interface TocNode extends TocHeading {
|
|
23
|
+
children: TocNode[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* nests a flat list of headings, each one under the nearest heading before it
|
|
27
|
+
* that is less deep, so a heading that skips a level still has a parent
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* toc_tree(plugin_data.toc.headings);
|
|
31
|
+
* // [{ depth: 2, id: 'install', text: 'Install', children: [{ depth: 3, ... }] }]
|
|
32
|
+
*/
|
|
33
|
+
export declare function toc_tree(headings: readonly TocHeading[]): TocNode[];
|
package/dist/tree.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* nests a flat list of headings, each one under the nearest heading before it
|
|
3
|
+
* that is less deep, so a heading that skips a level still has a parent
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* toc_tree(plugin_data.toc.headings);
|
|
7
|
+
* // [{ depth: 2, id: 'install', text: 'Install', children: [{ depth: 3, ... }] }]
|
|
8
|
+
*/
|
|
9
|
+
export function toc_tree(headings) {
|
|
10
|
+
const roots = [];
|
|
11
|
+
const open = [];
|
|
12
|
+
for (const heading of headings) {
|
|
13
|
+
const node = { ...heading, children: [] };
|
|
14
|
+
while (open.length > 0 && open[open.length - 1].depth >= node.depth)
|
|
15
|
+
open.pop();
|
|
16
|
+
if (open.length === 0)
|
|
17
|
+
roots.push(node);
|
|
18
|
+
else
|
|
19
|
+
open[open.length - 1].children.push(node);
|
|
20
|
+
open.push(node);
|
|
21
|
+
}
|
|
22
|
+
return roots;
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdsvex/plugin-toc",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-next.0",
|
|
4
|
+
"description": "Table of contents for mdsvex, the headings of a document as data and a component that lists them",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"default": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./directives": {
|
|
12
|
+
"types": "./dist/directives.d.ts",
|
|
13
|
+
"svelte": "./dist/directives.js"
|
|
14
|
+
},
|
|
15
|
+
"./Toc.svelte": {
|
|
16
|
+
"types": "./dist/Toc.svelte.d.ts",
|
|
17
|
+
"svelte": "./dist/Toc.svelte"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/pngwn/MDsveX.git",
|
|
28
|
+
"directory": "plugins/toc"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"mdsvex",
|
|
32
|
+
"mdsvex-plugin",
|
|
33
|
+
"svelte",
|
|
34
|
+
"markdown",
|
|
35
|
+
"toc",
|
|
36
|
+
"table-of-contents"
|
|
37
|
+
],
|
|
38
|
+
"author": "pngwn <hello@pngwn.io>",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"mdsvex": "^1.0.0-next.2",
|
|
42
|
+
"svelte": "^5.20.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@sveltejs/package": "^3.0.0",
|
|
46
|
+
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
|
47
|
+
"npm-run-all2": "^7.0.2",
|
|
48
|
+
"svelte": "^5.55.0",
|
|
49
|
+
"vite": "^7.3.1",
|
|
50
|
+
"@mdsvex/plugin-anchors": "^0.1.0-next.0",
|
|
51
|
+
"@mdsvex/plugin-steps": "^0.1.0-next.0",
|
|
52
|
+
"@mdsvex/plugin-tabs": "^0.1.0-next.0",
|
|
53
|
+
"mdsvex": "^1.0.0-next.2"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "run-s build:check build:package",
|
|
57
|
+
"build:check": "tsc",
|
|
58
|
+
"build:package": "svelte-package -i src -o dist",
|
|
59
|
+
"test": "vitest --disableConsoleIntercept",
|
|
60
|
+
"test:run": "vitest run"
|
|
61
|
+
}
|
|
6
62
|
}
|