remark-flexible-toc 1.0.0 → 1.1.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/README.md +72 -49
- package/dist/esm/index.js +5 -2
- package/dist/esm/index.js.map +1 -1
- package/package.json +31 -19
- package/src/index.ts +6 -3
package/README.md
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
# remark-flexible-toc
|
|
2
2
|
|
|
3
|
-
[![NPM version][npm-
|
|
4
|
-
[![
|
|
5
|
-
![
|
|
6
|
-
[![
|
|
3
|
+
[![NPM version][badge-npm-version]][npm-package-url]
|
|
4
|
+
[![NPM downloads][badge-npm-download]][npm-package-url]
|
|
5
|
+
[![Build][badge-build]][github-workflow-url]
|
|
6
|
+
[](https://codecov.io/gh/ipikuka/remark-flexible-toc)
|
|
7
|
+
[](https://github.com/ipikuka/remark-flexible-toc)
|
|
8
|
+
[![typescript][badge-typescript]][typescript-url]
|
|
9
|
+
[![License][badge-license]][github-license-url]
|
|
7
10
|
|
|
8
|
-
This package is a [unified][unified] ([remark][remark]) plugin to expose the table of contents via Vfile.data or via an option reference
|
|
11
|
+
This package is a [unified][unified] ([remark][remark]) plugin to expose the table of contents via `Vfile.data` or via an option reference in markdown.
|
|
9
12
|
|
|
10
|
-
|
|
13
|
+
**[unified][unified]** is a project that transforms content with abstract syntax trees (ASTs) using the new parser **[micromark][micromark]**. **[remark][remark]** adds support for markdown to unified. **[mdast][mdast]** is the Markdown Abstract Syntax Tree (AST) which is a specification for representing markdown in a syntax tree.
|
|
11
14
|
|
|
12
|
-
**This plugin is a remark plugin that gets info from the mdast.**
|
|
15
|
+
**This plugin is a remark plugin that doesn't transform the mdast but gets info from the mdast.**
|
|
13
16
|
|
|
14
17
|
## When should I use this?
|
|
15
18
|
|
|
16
19
|
This plugin `remark-flexible-toc` is useful if you want to get the table of contents (TOC) from the markdown/MDX document. The `remark-flexible-toc` exposes the table of contents (TOC) in two ways:
|
|
17
|
-
+ by adding the `toc` into the Vfile.data
|
|
18
|
-
+ by mutating
|
|
20
|
+
+ by adding the `toc` into the `Vfile.data`
|
|
21
|
+
+ by mutating a reference array if provided in the options
|
|
19
22
|
|
|
20
23
|
## Installation
|
|
21
24
|
|
|
@@ -67,10 +70,10 @@ async function main() {
|
|
|
67
70
|
.use(rehypeStringify)
|
|
68
71
|
.process(await read("example.md"));
|
|
69
72
|
|
|
70
|
-
// the first way of getting
|
|
73
|
+
// the first way of getting TOC via file.data
|
|
71
74
|
console.log(file.data.toc);
|
|
72
75
|
|
|
73
|
-
// the second way of getting
|
|
76
|
+
// the second way of getting TOC, since we provided a reference array in the options
|
|
74
77
|
console.log(toc);
|
|
75
78
|
}
|
|
76
79
|
```
|
|
@@ -126,9 +129,9 @@ use(remarkFlexibleToc, {
|
|
|
126
129
|
maxDepth?: HeadingDepth; // default: 6
|
|
127
130
|
skipLevels?: HeadingDepth[]; // default: [1]
|
|
128
131
|
skipParents?: Exclude<HeadingParent, "root">[]; // default: []
|
|
129
|
-
exclude?: string | string[];
|
|
130
|
-
prefix?: string;
|
|
131
|
-
fallback?: (toc: TocItem[]) => undefined;
|
|
132
|
+
exclude?: string | string[];
|
|
133
|
+
prefix?: string;
|
|
134
|
+
fallback?: (toc: TocItem[]) => undefined;
|
|
132
135
|
} as FlexibleTocOptions);
|
|
133
136
|
```
|
|
134
137
|
|
|
@@ -231,7 +234,7 @@ Now, all TOC items' `href`s will start with that prefix like `#text-prefix-the-s
|
|
|
231
234
|
|
|
232
235
|
#### `callback`
|
|
233
236
|
|
|
234
|
-
It is a **callback function** `callback?: (toc: TocItem[]) => undefined;` which takes the TOC items as an argument and returns nothing. It is usefull for logging the TOC, forexample, or modifing the TOC. **It is allowed that the callback function is able mutate the TOC items
|
|
237
|
+
It is a **callback function** `callback?: (toc: TocItem[]) => undefined;` which takes the TOC items as an argument and returns nothing. It is usefull for logging the TOC, forexample, or modifing the TOC. **It is allowed that the callback function is able to mutate the TOC items !**
|
|
235
238
|
|
|
236
239
|
The option has no default value.
|
|
237
240
|
|
|
@@ -243,7 +246,7 @@ use(remarkFlexibleToc, {
|
|
|
243
246
|
});
|
|
244
247
|
```
|
|
245
248
|
|
|
246
|
-
Now, each time when you compile the source, the TOC will be logged into console for debugging purpose.
|
|
249
|
+
Now, each time when you compile the source, the TOC will be logged into the console for debugging purpose.
|
|
247
250
|
|
|
248
251
|
## A Table of Content (TOC) Item
|
|
249
252
|
|
|
@@ -254,11 +257,14 @@ type TocItem = {
|
|
|
254
257
|
depth: HeadingDepth; // 1 | 2 | 3 | 4 | 5 | 6
|
|
255
258
|
numbering: number[]; // explained below
|
|
256
259
|
parent: HeadingParent; // "root"| "blockquote" | "footnoteDefinition" | "listItem" | "container" | "mdxJsxFlowElement"
|
|
257
|
-
data?: Record<string, unknown>; // Other remark plugins
|
|
260
|
+
data?: Record<string, unknown>; // Other remark plugins may store custom data in "node.data.hProperties" like "id" etc.
|
|
258
261
|
};
|
|
259
262
|
```
|
|
260
263
|
|
|
261
|
-
|
|
264
|
+
> [!NOTE]
|
|
265
|
+
> If there is a remark plugin before the `remark-flexible-toc` in the plugin chain, which provides custom id for headings like `remark-heading-id`, that custom id takes precedence for `href`.
|
|
266
|
+
|
|
267
|
+
The `remark-flexible-toc` uses the `github-slugger` internally for producing unique links. Then, it is possible you to use [`rehype-slug`](https://github.com/rehypejs/rehype-slug) (forIDs on headings) and [`rehype-autolink-headings`](https://github.com/rehypejs/rehype-autolink-headings) (for anchors that link-to-self) because they use the same `github-slugger`.
|
|
262
268
|
|
|
263
269
|
As an example for the unique heading links (notice the same heading texts).
|
|
264
270
|
|
|
@@ -309,9 +315,9 @@ The `github-slugger` produces unique links with using a counter mechanism intern
|
|
|
309
315
|
]
|
|
310
316
|
```
|
|
311
317
|
|
|
312
|
-
## Numbering for Ordered Table of Contents
|
|
318
|
+
## Numbering for Ordered Table of Contents (TOC)
|
|
313
319
|
|
|
314
|
-
The `remark-flexible-toc` produces always the `numbering` for
|
|
320
|
+
The `remark-flexible-toc` produces always the `numbering` for TOC items in case you show the ordered TOC.
|
|
315
321
|
|
|
316
322
|
The **numbering** of a TOC item is an array of number. The numbers in the `numbering` corresponds the **level of the headers**. With that structure, you know which header is under which header.
|
|
317
323
|
|
|
@@ -346,13 +352,13 @@ This plugin does not modify the `mdast` (markdown abstract syntax tree), collect
|
|
|
346
352
|
|
|
347
353
|
## Types
|
|
348
354
|
|
|
349
|
-
This package is fully typed with [TypeScript][
|
|
355
|
+
This package is fully typed with [TypeScript][typescript].
|
|
350
356
|
|
|
351
357
|
The plugin exports the types `FlexibleTocOptions`, `HeadingParent`, `HeadingDepth`, `TocItem`.
|
|
352
358
|
|
|
353
359
|
## Compatibility
|
|
354
360
|
|
|
355
|
-
This plugin works with unified version 6+ and remark version 7+. It is compatible with
|
|
361
|
+
This plugin works with `unified` version 6+ and `remark` version 7+. It is compatible with `mdx` version 2+.
|
|
356
362
|
|
|
357
363
|
## Security
|
|
358
364
|
|
|
@@ -360,54 +366,71 @@ Use of `remark-flexible-toc` does not involve rehype (hast) or user content so t
|
|
|
360
366
|
|
|
361
367
|
## My Plugins
|
|
362
368
|
|
|
369
|
+
I like to contribute the Unified / Remark / MDX ecosystem, so I recommend you to have a look my plugins.
|
|
370
|
+
|
|
363
371
|
### My Remark Plugins
|
|
364
372
|
|
|
365
|
-
|
|
373
|
+
- [`remark-flexible-code-titles`](https://www.npmjs.com/package/remark-flexible-code-titles)
|
|
366
374
|
– Remark plugin to add titles or/and containers for the code blocks with customizable properties
|
|
367
|
-
|
|
375
|
+
- [`remark-flexible-containers`](https://www.npmjs.com/package/remark-flexible-containers)
|
|
368
376
|
– Remark plugin to add custom containers with customizable properties in markdown
|
|
369
|
-
|
|
377
|
+
- [`remark-ins`](https://www.npmjs.com/package/remark-ins)
|
|
370
378
|
– Remark plugin to add `ins` element in markdown
|
|
371
|
-
|
|
379
|
+
- [`remark-flexible-paragraphs`](https://www.npmjs.com/package/remark-flexible-paragraphs)
|
|
372
380
|
– Remark plugin to add custom paragraphs with customizable properties in markdown
|
|
373
|
-
|
|
381
|
+
- [`remark-flexible-markers`](https://www.npmjs.com/package/remark-flexible-markers)
|
|
374
382
|
– Remark plugin to add custom `mark` element with customizable properties in markdown
|
|
375
|
-
|
|
376
|
-
– Remark plugin to expose the table of contents via
|
|
383
|
+
- [`remark-flexible-toc`](https://www.npmjs.com/package/remark-flexible-toc)
|
|
384
|
+
– Remark plugin to expose the table of contents via `vfile.data` or via an option reference
|
|
385
|
+
- [`remark-mdx-remove-esm`](https://www.npmjs.com/package/remark-mdx-remove-esm)
|
|
386
|
+
– Remark plugin to remove import and/or export statements (mdxjsEsm)
|
|
387
|
+
|
|
388
|
+
### My Rehype Plugins
|
|
389
|
+
|
|
390
|
+
- [`rehype-pre-language`](https://www.npmjs.com/package/rehype-pre-language)
|
|
391
|
+
– Rehype plugin to add language information as a property to `pre` element
|
|
377
392
|
|
|
378
393
|
### My Recma Plugins
|
|
379
394
|
|
|
380
|
-
|
|
381
|
-
– Recma plugin to
|
|
382
|
-
|
|
383
|
-
– Recma plugin to change the
|
|
395
|
+
- [`recma-mdx-escape-missing-components`](https://www.npmjs.com/package/recma-mdx-escape-missing-components)
|
|
396
|
+
– Recma plugin to set the default value `() => null` for the Components in MDX in case of missing or not provided so as not to throw an error
|
|
397
|
+
- [`recma-mdx-change-props`](https://www.npmjs.com/package/recma-mdx-change-props)
|
|
398
|
+
– Recma plugin to change the `props` parameter into the `_props` in the `function _createMdxContent(props) {/* */}` in the compiled source in order to be able to use `{props.foo}` like expressions. It is useful for the `next-mdx-remote` or `next-mdx-remote-client` users in `nextjs` applications.
|
|
384
399
|
|
|
385
400
|
## License
|
|
386
401
|
|
|
387
|
-
[MIT]
|
|
402
|
+
[MIT License](./LICENSE) © ipikuka
|
|
388
403
|
|
|
389
404
|
### Keywords
|
|
390
405
|
|
|
391
|
-
[unified][unifiednpm] [remark][remarknpm] [remark
|
|
406
|
+
🟩 [unified][unifiednpm] 🟩 [remark][remarknpm] 🟩 [remark plugin][remarkpluginnpm] 🟩 [mdast][mdastnpm] 🟩 [markdown][markdownnpm] 🟩 [mdx][mdxnpm] 🟩 [remark toc][remarktocnpm] 🟩 [remark table of contents][remarktableofcontentsnpm]
|
|
407
|
+
|
|
392
408
|
|
|
393
|
-
[unified]: https://github.com/unifiedjs/unified
|
|
394
409
|
[unifiednpm]: https://www.npmjs.com/search?q=keywords:unified
|
|
395
|
-
[remark]: https://github.com/remarkjs/remark
|
|
396
410
|
[remarknpm]: https://www.npmjs.com/search?q=keywords:remark
|
|
397
411
|
[remarkpluginnpm]: https://www.npmjs.com/search?q=keywords:remark%20plugin
|
|
398
|
-
[mdast]: https://github.com/syntax-tree/mdast
|
|
399
412
|
[mdastnpm]: https://www.npmjs.com/search?q=keywords:mdast
|
|
400
|
-
[micromark]: https://github.com/micromark/micromark
|
|
401
|
-
[typescript]: https://www.typescriptlang.org/
|
|
402
|
-
[license]: https://github.com/ipikuka/remark-flexible-toc/blob/main/LICENSE
|
|
403
|
-
[mdxnpm]: https://www.npmjs.com/search?q=keywords:mdx
|
|
404
413
|
[markdownnpm]: https://www.npmjs.com/search?q=keywords:markdown
|
|
414
|
+
[mdxnpm]: https://www.npmjs.com/search?q=keywords:mdx
|
|
405
415
|
[remarktocnpm]: https://www.npmjs.com/search?q=keywords:remark%20toc
|
|
406
416
|
[remarktableofcontentsnpm]: https://www.npmjs.com/search?q=keywords:remark%20table%20of%20contents
|
|
407
|
-
|
|
408
|
-
[
|
|
409
|
-
[
|
|
410
|
-
[
|
|
411
|
-
[
|
|
412
|
-
[
|
|
413
|
-
[
|
|
417
|
+
|
|
418
|
+
[unified]: https://github.com/unifiedjs/unified
|
|
419
|
+
[remark]: https://github.com/remarkjs/remark
|
|
420
|
+
[remarkplugins]: https://github.com/remarkjs/remark/blob/main/doc/plugins.md
|
|
421
|
+
[mdast]: https://github.com/syntax-tree/mdast
|
|
422
|
+
[micromark]: https://github.com/micromark/micromark
|
|
423
|
+
[typescript]: https://www.typescriptlang.org/
|
|
424
|
+
|
|
425
|
+
[badge-npm-version]: https://img.shields.io/npm/v/remark-flexible-toc
|
|
426
|
+
[badge-npm-download]:https://img.shields.io/npm/dt/remark-flexible-toc
|
|
427
|
+
[npm-package-url]: https://www.npmjs.com/package/remark-flexible-toc
|
|
428
|
+
|
|
429
|
+
[badge-license]: https://img.shields.io/github/license/ipikuka/remark-flexible-toc
|
|
430
|
+
[github-license-url]: https://github.com/ipikuka/remark-flexible-toc/blob/main/LICENSE
|
|
431
|
+
|
|
432
|
+
[badge-build]: https://github.com/ipikuka/remark-flexible-toc/actions/workflows/publish.yml/badge.svg
|
|
433
|
+
[github-workflow-url]: https://github.com/ipikuka/remark-flexible-toc/actions/workflows/publish.yml
|
|
434
|
+
|
|
435
|
+
[badge-typescript]: https://img.shields.io/npm/types/remark-flexible-toc
|
|
436
|
+
[typescript-url]: https://www.typescriptlang.org/
|
package/dist/esm/index.js
CHANGED
|
@@ -56,11 +56,12 @@ const RemarkFlexibleToc = (options) => {
|
|
|
56
56
|
const slugger = new GithubSlugger();
|
|
57
57
|
const tocItems = [];
|
|
58
58
|
visit(tree, "heading", (_node, _index, _parent) => {
|
|
59
|
-
|
|
59
|
+
/* v8 ignore next */
|
|
60
|
+
if (!_parent || typeof _index === "undefined")
|
|
60
61
|
return;
|
|
61
62
|
const depth = _node.depth;
|
|
62
63
|
const value = toString(_node, { includeImageAlt: false });
|
|
63
|
-
|
|
64
|
+
let href = `#${settings.prefix ?? ""}${slugger.slug(value)}`;
|
|
64
65
|
const parent = _parent.type;
|
|
65
66
|
// maxDepth check
|
|
66
67
|
if (depth > settings.maxDepth)
|
|
@@ -79,6 +80,8 @@ const RemarkFlexibleToc = (options) => {
|
|
|
79
80
|
const data = _node.data?.hProperties
|
|
80
81
|
? { ..._node.data.hProperties }
|
|
81
82
|
: undefined;
|
|
83
|
+
if (data?.["id"])
|
|
84
|
+
href = `#${data["id"]}`;
|
|
82
85
|
tocItems.push({
|
|
83
86
|
value,
|
|
84
87
|
href,
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,aAAa,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAuDhD,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,aAAa,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAuDhD,MAAM,gBAAgB,GAAuB;IAC3C,OAAO,EAAE,KAAK;IACd,MAAM,EAAE,EAAE;IACV,QAAQ,EAAE,CAAC;IACX,UAAU,EAAE,CAAC,CAAC,CAAC;IACf,WAAW,EAAE,EAAE;CAChB,CAAC;AAIF;;;;;;;;GAQG;AACH,SAAS,YAAY,CAAC,GAAc;IAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACpC,MAAM,OAAO,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACvB,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAE5B,IAAI,SAAS,GAAa,EAAE,CAAC;QAE7B,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC/C,MAAM,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CA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
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Remark plugin to expose the table of contents via Vfile.data or via an option reference",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./dist/esm/index.js",
|
|
7
7
|
"main": "./dist/esm/index.js",
|
|
8
8
|
"types": "./dist/esm/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"lint": "eslint .",
|
|
10
|
+
"build": "rimraf dist && tsc --build && type-coverage",
|
|
11
|
+
"format": "npm run prettier && npm run lint",
|
|
13
12
|
"prettier": "prettier --write .",
|
|
14
|
-
"
|
|
15
|
-
"test
|
|
13
|
+
"lint": "eslint .",
|
|
14
|
+
"test": "vitest --watch=false",
|
|
15
|
+
"test:watch": "vitest",
|
|
16
16
|
"test:file": "vitest without_options.spec.ts",
|
|
17
17
|
"prepack": "npm run build",
|
|
18
|
-
"prepublishOnly": "npm run test
|
|
18
|
+
"prepublishOnly": "npm run test && npm run format && npm run test-coverage",
|
|
19
|
+
"test-coverage": "vitest run --coverage"
|
|
19
20
|
},
|
|
20
21
|
"files": [
|
|
21
22
|
"dist/",
|
|
@@ -48,29 +49,40 @@
|
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
50
51
|
"@types/dedent": "^0.7.2",
|
|
51
|
-
"@types/node": "^20.
|
|
52
|
-
"@
|
|
53
|
-
"@typescript-eslint/
|
|
54
|
-
"
|
|
55
|
-
"
|
|
52
|
+
"@types/node": "^20.11.30",
|
|
53
|
+
"@types/remark-heading-id": "^1.0.0",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "^7.3.1",
|
|
55
|
+
"@typescript-eslint/parser": "^7.3.1",
|
|
56
|
+
"@vitest/coverage-v8": "^1.4.0",
|
|
57
|
+
"dedent": "^1.5.1",
|
|
58
|
+
"eslint": "^8.57.0",
|
|
56
59
|
"eslint-config-prettier": "^9.1.0",
|
|
57
|
-
"eslint-plugin-prettier": "^5.1.
|
|
58
|
-
"prettier": "^3.
|
|
60
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
61
|
+
"prettier": "^3.2.5",
|
|
59
62
|
"rehype-format": "^5.0.0",
|
|
60
63
|
"rehype-stringify": "^10.0.0",
|
|
61
64
|
"remark-gfm": "^4.0.0",
|
|
65
|
+
"remark-heading-id": "^1.0.1",
|
|
62
66
|
"remark-parse": "^11.0.0",
|
|
63
|
-
"remark-rehype": "^11.
|
|
67
|
+
"remark-rehype": "^11.1.0",
|
|
64
68
|
"rimraf": "^5.0.5",
|
|
65
|
-
"
|
|
69
|
+
"type-coverage": "^2.27.1",
|
|
70
|
+
"typescript": "^5.4.3",
|
|
66
71
|
"unified": "^11.0.4",
|
|
67
|
-
"vitest": "^1.
|
|
72
|
+
"vitest": "^1.4.0"
|
|
68
73
|
},
|
|
69
74
|
"dependencies": {
|
|
70
|
-
"@types/mdast": "^4.0.
|
|
75
|
+
"@types/mdast": "^4.0.3",
|
|
71
76
|
"github-slugger": "^2.0.0",
|
|
72
77
|
"mdast-util-to-string": "^4.0.0",
|
|
73
78
|
"unist-util-visit": "^5.0.0"
|
|
74
79
|
},
|
|
75
|
-
"sideEffects": false
|
|
80
|
+
"sideEffects": false,
|
|
81
|
+
"typeCoverage": {
|
|
82
|
+
"atLeast": 100,
|
|
83
|
+
"detail": true,
|
|
84
|
+
"ignoreAsAssertion": true,
|
|
85
|
+
"ignoreCatch": true,
|
|
86
|
+
"strict": true
|
|
87
|
+
}
|
|
76
88
|
}
|
package/src/index.ts
CHANGED
|
@@ -57,7 +57,7 @@ type PartiallyRequiredFlexibleTocOptions = Prettify<
|
|
|
57
57
|
>
|
|
58
58
|
>;
|
|
59
59
|
|
|
60
|
-
const DEFAULT_SETTINGS = {
|
|
60
|
+
const DEFAULT_SETTINGS: FlexibleTocOptions = {
|
|
61
61
|
tocName: "toc",
|
|
62
62
|
tocRef: [],
|
|
63
63
|
maxDepth: 6,
|
|
@@ -127,11 +127,12 @@ const RemarkFlexibleToc: Plugin<[FlexibleTocOptions?], Root> = (options) => {
|
|
|
127
127
|
const tocItems: TocItem[] = [];
|
|
128
128
|
|
|
129
129
|
visit(tree, "heading", (_node, _index, _parent) => {
|
|
130
|
-
|
|
130
|
+
/* v8 ignore next */
|
|
131
|
+
if (!_parent || typeof _index === "undefined") return;
|
|
131
132
|
|
|
132
133
|
const depth = _node.depth;
|
|
133
134
|
const value = toString(_node, { includeImageAlt: false });
|
|
134
|
-
|
|
135
|
+
let href = `#${settings.prefix ?? ""}${slugger.slug(value)}`;
|
|
135
136
|
const parent = _parent.type;
|
|
136
137
|
|
|
137
138
|
// maxDepth check
|
|
@@ -152,6 +153,8 @@ const RemarkFlexibleToc: Plugin<[FlexibleTocOptions?], Root> = (options) => {
|
|
|
152
153
|
? { ...(_node.data as ExtendedHeadingData).hProperties }
|
|
153
154
|
: undefined;
|
|
154
155
|
|
|
156
|
+
if (data?.["id"]) href = `#${data["id"]}`;
|
|
157
|
+
|
|
155
158
|
tocItems.push({
|
|
156
159
|
value,
|
|
157
160
|
href,
|