This repository was archived by the owner on Oct 1, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 9 files changed +66
-45
lines changed Expand file tree Collapse file tree 9 files changed +66
-45
lines changed Original file line number Diff line number Diff line change 1
1
coverage /
2
2
node_modules /
3
3
.DS_Store
4
+ * .d.ts
4
5
* .log
5
6
yarn.lock
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @typedef {import('mdast').Root } Root
3
+ * @typedef {import('hast').Properties } Properties
4
+ */
5
+
1
6
import { toString } from 'mdast-util-to-string'
2
7
import { visit } from 'unist-util-visit'
3
8
import BananaSlug from 'github-slugger'
4
9
5
10
const slugs = new BananaSlug ( )
6
11
7
- // Patch slugs on heading nodes.
12
+ /**
13
+ * Plugin to add anchors headings using GitHub’s algorithm.
14
+ *
15
+ * @type {import('unified').Plugin<void[], Root> }
16
+ */
8
17
export default function remarkSlug ( ) {
9
- return ( ast ) => {
18
+ return ( tree ) => {
10
19
slugs . reset ( )
11
20
12
- visit ( ast , 'heading' , ( node ) => {
21
+ visit ( tree , 'heading' , ( node ) => {
13
22
const data = node . data || ( node . data = { } )
14
- const props = data . hProperties || ( data . hProperties = { } )
23
+ const props = /** @type {Properties } */ (
24
+ data . hProperties || ( data . hProperties = { } )
25
+ )
15
26
let id = props . id
16
27
17
- id = id ? slugs . slug ( id , true ) : slugs . slug ( toString ( node ) )
28
+ id = id ? slugs . slug ( String ( id ) , true ) : slugs . slug ( toString ( node ) )
18
29
19
30
data . id = id
20
31
props . id = id
Original file line number Diff line number Diff line change 30
30
"sideEffects" : false ,
31
31
"type" : " module" ,
32
32
"main" : " index.js" ,
33
+ "types" : " index.d.ts" ,
33
34
"files" : [
35
+ " index.d.ts" ,
34
36
" index.js"
35
37
],
36
38
"dependencies" : {
39
+ "@types/hast" : " ^2.3.2" ,
40
+ "@types/mdast" : " ^3.0.0" ,
37
41
"github-slugger" : " ^1.0.0" ,
38
42
"mdast-util-to-string" : " ^3.0.0" ,
43
+ "unified" : " ^10.0.0" ,
39
44
"unist-util-visit" : " ^4.0.0"
40
45
},
41
46
"devDependencies" : {
47
+ "@types/github-slugger" : " ^1.3.0" ,
48
+ "@types/tape" : " ^4.0.0" ,
42
49
"c8" : " ^7.0.0" ,
43
50
"prettier" : " ^2.0.0" ,
44
51
"remark" : " ^14.0.0" ,
45
52
"remark-cli" : " ^10.0.0" ,
46
53
"remark-preset-wooorm" : " ^8.0.0" ,
54
+ "rimraf" : " ^3.0.0" ,
47
55
"tape" : " ^5.0.0" ,
56
+ "type-coverage" : " ^2.0.0" ,
57
+ "typescript" : " ^4.0.0" ,
48
58
"unist-builder" : " ^3.0.0" ,
49
59
"unist-util-remove-position" : " ^4.0.0" ,
50
60
"xo" : " ^0.39.0"
51
61
},
52
62
"scripts" : {
63
+ "build" : " rimraf \" *.d.ts\" && tsc && type-coverage" ,
53
64
"format" : " remark . -qfo && prettier . -w --loglevel warn && xo --fix" ,
54
65
"test-api" : " node --conditions development test.js" ,
55
66
"test-coverage" : " c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api" ,
56
- "test" : " npm run format && npm run build && npm run test-coverage"
67
+ "test" : " npm run build && npm run format && npm run test-coverage"
57
68
},
58
69
"prettier" : {
59
70
"tabWidth" : 2 ,
64
75
"trailingComma" : " none"
65
76
},
66
77
"xo" : {
67
- "prettier" : true ,
68
- "ignores" : [
69
- " types/index.d.ts"
70
- ]
78
+ "prettier" : true
71
79
},
72
80
"remarkConfig" : {
73
81
"plugins" : [
74
82
" preset-wooorm"
75
83
]
84
+ },
85
+ "typeCoverage" : {
86
+ "atLeast" : 100 ,
87
+ "detail" : true ,
88
+ "strict" : true ,
89
+ "ignoreCatch" : true
76
90
}
77
91
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @typedef {import('mdast').Root } Root
3
+ */
4
+
1
5
import test from 'tape'
2
6
import { remark } from 'remark'
3
7
import { u } from 'unist-builder'
@@ -215,6 +219,10 @@ test('remarkSlug', (t) => {
215
219
t . end ( )
216
220
} )
217
221
222
+ /**
223
+ * @param {string|null } label
224
+ * @param {string } id
225
+ */
218
226
function heading ( label , id ) {
219
227
return u (
220
228
'heading' ,
@@ -223,7 +231,11 @@ function heading(label, id) {
223
231
)
224
232
}
225
233
226
- function process ( doc , options ) {
227
- const processor = remark ( ) . use ( remarkSlug , options )
234
+ /**
235
+ * @param {string } doc
236
+ * @returns {Root }
237
+ */
238
+ function process ( doc ) {
239
+ const processor = remark ( ) . use ( remarkSlug )
228
240
return removePosition ( processor . runSync ( processor . parse ( doc ) ) , true )
229
241
}
Original file line number Diff line number Diff line change
1
+ {
2
+ "include" : [" *.js" ],
3
+ "compilerOptions" : {
4
+ "target" : " ES2020" ,
5
+ "lib" : [" ES2020" ],
6
+ "module" : " ES2020" ,
7
+ "moduleResolution" : " node" ,
8
+ "allowJs" : true ,
9
+ "checkJs" : true ,
10
+ "declaration" : true ,
11
+ "emitDeclarationOnly" : true ,
12
+ "allowSyntheticDefaultImports" : true ,
13
+ "skipLibCheck" : true ,
14
+ "strict" : true
15
+ }
16
+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments