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 CHANGED
@@ -1,21 +1,24 @@
1
1
  # remark-flexible-toc
2
2
 
3
- [![NPM version][npm-image]][npm-url]
4
- [![Build][github-build]][github-build-url]
5
- ![npm-typescript]
6
- [![License][github-license]][github-license-url]
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
+ [![codecov](https://codecov.io/gh/ipikuka/remark-flexible-toc/graph/badge.svg?token=QBNX6L8W2G)](https://codecov.io/gh/ipikuka/remark-flexible-toc)
7
+ [![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fipikuka%2Fremark-flexible-toc%2Fmaster%2Fpackage.json)](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 (compatible with new parser "[micromark][micromark]").
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
- "**unified**" is a project that transforms content with abstract syntax trees (ASTs). "**remark**" adds support for markdown to unified. "**mdast**" is the markdown abstract syntax tree (AST) that remark uses.
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 an array of reference if provided in the options
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 the table of contents (TOC) via file.data
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 the table of contents (TOC), since we provided an array of reference in the options
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[]; // default is undefined
130
- prefix?: string; // default is undefined
131
- fallback?: (toc: TocItem[]) => undefined; // default is 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 can store custom data in "node.data.hProperties" like "id" etc.
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
- As a note, 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`.
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 the TOC items in case you show the ordered TOC.
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][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 MDX version.3.
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
- + [`remark-flexible-code-titles`](https://www.npmjs.com/package/remark-flexible-code-titles)
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
- + [`remark-flexible-containers`](https://www.npmjs.com/package/remark-flexible-containers)
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
- + [`remark-ins`](https://www.npmjs.com/package/remark-ins)
377
+ - [`remark-ins`](https://www.npmjs.com/package/remark-ins)
370
378
  – Remark plugin to add `ins` element in markdown
371
- + [`remark-flexible-paragraphs`](https://www.npmjs.com/package/remark-flexible-paragraphs)
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
- + [`remark-flexible-markers`](https://www.npmjs.com/package/remark-flexible-markers)
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
- + [`remark-flexible-toc`](https://www.npmjs.com/package/remark-flexible-toc)
376
- – Remark plugin to expose the table of contents via Vfile.data or via an option reference
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
- + [`recma-mdx-escape-missing-components`](https://www.npmjs.com/package/recma-mdx-escape-missing-components)
381
- – Recma plugin to to set the default value `() => null` for the Components in MDX in case of missing or not provided
382
- + [`recma-mdx-change-props`](https://www.npmjs.com/package/recma-mdx-change-props)
383
- – Recma plugin to change the 'props' parameter into '_props' in the function '_createMdxContent' in the compiled source in order to be able to use {props.foo} like expressions for the `next-mdx-remote` or `next-mdx-remote-client` users.
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][license] © ipikuka
402
+ [MIT License](./LICENSE) © ipikuka
388
403
 
389
404
  ### Keywords
390
405
 
391
- [unified][unifiednpm] [remark][remarknpm] [remark-plugin][remarkpluginnpm] [mdast][mdastnpm] [markdown][markdownnpm] [mdxnpm][mdxnpm] [remark toc][remarktocnpm] [remark table of contents][remarktableofcontentsnpm]
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
- [npm-url]: https://www.npmjs.com/package/remark-flexible-toc
408
- [npm-image]: https://img.shields.io/npm/v/remark-flexible-toc
409
- [github-license]: https://img.shields.io/github/license/ipikuka/remark-flexible-toc
410
- [github-license-url]: https://github.com/ipikuka/remark-flexible-toc/blob/master/LICENSE
411
- [github-build]: https://github.com/ipikuka/remark-flexible-toc/actions/workflows/publish.yml/badge.svg
412
- [github-build-url]: https://github.com/ipikuka/remark-flexible-toc/actions/workflows/publish.yml
413
- [npm-typescript]: https://img.shields.io/npm/types/remark-flexible-toc
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
- if (!_parent)
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
- const href = `#${settings.prefix ?? ""}${slugger.slug(value)}`;
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,
@@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,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBAC3B,yDAAyD;gBACzD,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAChC,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,21 +1,22 @@
1
1
  {
2
2
  "name": "remark-flexible-toc",
3
- "version": "1.0.0",
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
- "prebuild": "rimraf dist",
11
- "build": "npm run prebuild && tsc",
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
- "test": "vitest",
15
- "test:ci": "vitest --watch=false",
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:ci && npm run prettier && npm run lint"
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.10.5",
52
- "@typescript-eslint/eslint-plugin": "^6.16.0",
53
- "@typescript-eslint/parser": "^6.16.0",
54
- "dedent": "^0.7.0",
55
- "eslint": "^8.56.0",
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.2",
58
- "prettier": "^3.1.1",
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.0.0",
67
+ "remark-rehype": "^11.1.0",
64
68
  "rimraf": "^5.0.5",
65
- "typescript": "^5.3.3",
69
+ "type-coverage": "^2.27.1",
70
+ "typescript": "^5.4.3",
66
71
  "unified": "^11.0.4",
67
- "vitest": "^1.3.0"
72
+ "vitest": "^1.4.0"
68
73
  },
69
74
  "dependencies": {
70
- "@types/mdast": "^4.0.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
- if (!_parent) return;
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
- const href = `#${settings.prefix ?? ""}${slugger.slug(value)}`;
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,