Skip to content

Commit be36f14

Browse files
committed
Refactor code-style
1 parent 89fbc0b commit be36f14

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

lib/index.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,49 @@
55

66
import {toc} from 'mdast-util-toc'
77

8+
/** @type {Readonly<Options>} */
9+
const emptyOptions = {}
10+
811
/**
9-
* Plugin to generate a Table of Contents (TOC).
12+
* Generate a table of contents (TOC).
1013
*
11-
* @type {import('unified').Plugin<[Options?]|void[], Root>}
14+
* @param {Readonly<Options> | null | undefined} [options]
15+
* Configuration (optional).
16+
* @returns
17+
* Transform.
1218
*/
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-
)
19+
export default function remarkToc(options) {
20+
let settings = options || emptyOptions
21+
22+
if (!settings.heading) {
23+
settings = {...settings, heading: 'toc|table[ -]of[ -]contents?'}
24+
}
25+
26+
/**
27+
* Transform.
28+
*
29+
* @param {Root} tree
30+
* Tree.
31+
* @returns {undefined}
32+
* Nothing.
33+
*/
34+
return function (tree) {
35+
const result = toc(tree, settings)
2136

2237
if (
23-
result.endIndex === null ||
24-
result.index === null ||
38+
result.endIndex === undefined ||
39+
result.endIndex === -1 ||
40+
result.index === undefined ||
2541
result.index === -1 ||
2642
!result.map
2743
) {
2844
return
2945
}
3046

31-
node.children = [
32-
...node.children.slice(0, result.index),
47+
tree.children = [
48+
...tree.children.slice(0, result.index),
3349
result.map,
34-
...node.children.slice(result.endIndex)
50+
...tree.children.slice(result.endIndex)
3551
]
3652
}
3753
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
],
4141
"dependencies": {
4242
"@types/mdast": "^4.0.0",
43-
"mdast-util-toc": "^7.0.0",
44-
"unified": "^11.0.0"
43+
"mdast-util-toc": "^7.0.0"
4544
},
4645
"devDependencies": {
4746
"@types/node": "^20.0.0",

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ so:
238238
async function main() {
239239
const file = await remark()
240240
- .use(remarkToc)
241-
+ .use(remarkToc, {tight: true, ordered: true})
241+
+ .use(remarkToc, {ordered: true, tight: true})
242242
.process(await read('example.md'))
243243

244244
console.log(String(file))
@@ -265,7 +265,7 @@ so:
265265
async function main() {
266266
const file = await remark()
267267
- .use(remarkToc)
268-
+ .use(remarkToc, {maxDepth: 3, skip: 'delta', parents: ['root', 'listItem']})
268+
+ .use(remarkToc, {maxDepth: 3, parents: ['listItem', 'root'], skip: 'delta'})
269269
.process(await read('example.md'))
270270

271271
console.log(String(file))

test/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ import assert from 'node:assert/strict'
66
import fs from 'node:fs/promises'
77
import process from 'node:process'
88
import test from 'node:test'
9-
import {remark} from 'remark'
109
import {isHidden} from 'is-hidden'
10+
import {remark} from 'remark'
1111
import remarkToc from '../index.js'
1212

1313
test('remarkToc', async function (t) {
14+
await t.test('should expose the public api', async function () {
15+
assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
16+
'default'
17+
])
18+
})
19+
})
20+
21+
test('fixtures', async function (t) {
1422
const base = new URL('fixtures/', import.meta.url)
1523
const folders = await fs.readdir(base)
1624

@@ -38,9 +46,7 @@ test('remarkToc', async function (t) {
3846
config = JSON.parse(String(await fs.readFile(configUrl)))
3947
} catch {}
4048

41-
const proc = remark()
42-
// @ts-expect-error: to do: fix type.
43-
.use(remarkToc, config)
49+
const proc = remark().use(remarkToc, config)
4450

4551
const actual = String(await proc.process(input))
4652

0 commit comments

Comments
 (0)