Skip to content

Commit 0a50017

Browse files
committed
Refactor to move implementation to lib/
1 parent daf6040 commit 0a50017

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed

index.js

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,5 @@
11
/**
2-
* @typedef {import('mdast').Heading} Heading
3-
* @typedef {'atx'|'atx-closed'|'setext'} Style
2+
* @typedef {import('./lib/index.js').Style} Style
43
*/
54

6-
/**
7-
* @param {Heading} node
8-
* @param {Style} [relative]
9-
* @returns {Style|null}
10-
*/
11-
export function headingStyle(node, relative) {
12-
const last = node.children[node.children.length - 1]
13-
const depth = node.depth
14-
const pos = node && node.position && node.position.end
15-
const final = last && last.position && last.position.end
16-
17-
if (!pos) {
18-
return null
19-
}
20-
21-
// This can only occur for `'atx'` and `'atx-closed'` headings.
22-
// This might incorrectly match `'atx'` headings with lots of trailing white
23-
// space as an `'atx-closed'` heading.
24-
if (!last) {
25-
if (pos.column - 1 <= depth * 2) {
26-
return consolidate(depth, relative)
27-
}
28-
29-
return 'atx-closed'
30-
}
31-
32-
if (final && final.line + 1 === pos.line) {
33-
return 'setext'
34-
}
35-
36-
if (final && final.column + depth < pos.column) {
37-
return 'atx-closed'
38-
}
39-
40-
return consolidate(depth, relative)
41-
}
42-
43-
/**
44-
* Get the probable style of an atx-heading, depending on preferred style.
45-
*
46-
* @param {number} depth
47-
* @param {Style|undefined} relative
48-
* @returns {Style|null}
49-
*/
50-
function consolidate(depth, relative) {
51-
return depth < 3
52-
? 'atx'
53-
: relative === 'atx' || relative === 'setext'
54-
? relative
55-
: null
56-
}
5+
export {headingStyle} from './lib/index.js'

lib/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @typedef {import('mdast').Heading} Heading
3+
* @typedef {'atx'|'atx-closed'|'setext'} Style
4+
*/
5+
6+
/**
7+
* @param {Heading} node
8+
* @param {Style} [relative]
9+
* @returns {Style|null}
10+
*/
11+
export function headingStyle(node, relative) {
12+
const last = node.children[node.children.length - 1]
13+
const depth = node.depth
14+
const pos = node && node.position && node.position.end
15+
const final = last && last.position && last.position.end
16+
17+
if (!pos) {
18+
return null
19+
}
20+
21+
// This can only occur for `'atx'` and `'atx-closed'` headings.
22+
// This might incorrectly match `'atx'` headings with lots of trailing white
23+
// space as an `'atx-closed'` heading.
24+
if (!last) {
25+
if (pos.column - 1 <= depth * 2) {
26+
return consolidate(depth, relative)
27+
}
28+
29+
return 'atx-closed'
30+
}
31+
32+
if (final && final.line + 1 === pos.line) {
33+
return 'setext'
34+
}
35+
36+
if (final && final.column + depth < pos.column) {
37+
return 'atx-closed'
38+
}
39+
40+
return consolidate(depth, relative)
41+
}
42+
43+
/**
44+
* Get the probable style of an atx-heading, depending on preferred style.
45+
*
46+
* @param {number} depth
47+
* @param {Style|undefined} relative
48+
* @returns {Style|null}
49+
*/
50+
function consolidate(depth, relative) {
51+
return depth < 3
52+
? 'atx'
53+
: relative === 'atx' || relative === 'setext'
54+
? relative
55+
: null
56+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"main": "index.js",
3030
"types": "index.d.ts",
3131
"files": [
32+
"lib/",
3233
"index.d.ts",
3334
"index.js"
3435
],

0 commit comments

Comments
 (0)