Skip to content

Commit 5459302

Browse files
committed
Refactor to move code to lib/
1 parent 8ae5160 commit 5459302

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

index.js

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
/**
2-
* @typedef {import('mdast').Root} Root
3-
* @typedef {import('mdast-util-toc').Options} Options
2+
* @typedef {import('./lib/index.js').Options} Options
43
*/
54

6-
import {toc} from 'mdast-util-toc'
7-
8-
/**
9-
* Plugin to generate a Table of Contents (TOC).
10-
*
11-
* @type {import('unified').Plugin<[Options?]|void[], Root>}
12-
*/
13-
export default function remarkToc(options = {}) {
14-
return (node) => {
15-
const result = toc(
16-
node,
17-
Object.assign({}, options, {
18-
heading: options.heading || 'toc|table[ -]of[ -]contents?'
19-
})
20-
)
21-
22-
if (
23-
result.endIndex === null ||
24-
result.index === null ||
25-
result.index === -1 ||
26-
!result.map
27-
) {
28-
return
29-
}
30-
31-
node.children = [
32-
...node.children.slice(0, result.index),
33-
result.map,
34-
...node.children.slice(result.endIndex)
35-
]
36-
}
37-
}
5+
export {default} from './lib/index.js'

lib/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
* @typedef {import('mdast-util-toc').Options} Options
4+
*/
5+
6+
import {toc} from 'mdast-util-toc'
7+
8+
/**
9+
* Plugin to generate a Table of Contents (TOC).
10+
*
11+
* @type {import('unified').Plugin<[Options?]|void[], Root>}
12+
*/
13+
export default function remarkToc(options = {}) {
14+
return (node) => {
15+
const result = toc(
16+
node,
17+
Object.assign({}, options, {
18+
heading: options.heading || 'toc|table[ -]of[ -]contents?'
19+
})
20+
)
21+
22+
if (
23+
result.endIndex === null ||
24+
result.index === null ||
25+
result.index === -1 ||
26+
!result.map
27+
) {
28+
return
29+
}
30+
31+
node.children = [
32+
...node.children.slice(0, result.index),
33+
result.map,
34+
...node.children.slice(result.endIndex)
35+
]
36+
}
37+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"main": "index.js",
3535
"types": "index.d.ts",
3636
"files": [
37+
"lib/",
3738
"index.d.ts",
3839
"index.js"
3940
],

0 commit comments

Comments
 (0)