Skip to content

Commit d66e8d0

Browse files
committed
Refactor code-style
1 parent 5ec78bb commit d66e8d0

File tree

5 files changed

+68
-64
lines changed

5 files changed

+68
-64
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
mdast-util-heading-style.js
3+
mdast-util-heading-style.min.js

index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict';
1+
'use strict'
22

3-
/* Expose. */
4-
module.exports = style;
3+
module.exports = style
54

6-
/* Check the style of a heading. */
75
function style(node, relative) {
8-
var last = node.children[node.children.length - 1];
9-
var depth = node.depth;
10-
var pos = node && node.position && node.position.end;
11-
var final = last && last.position && last.position.end;
6+
var last = node.children[node.children.length - 1]
7+
var depth = node.depth
8+
var pos = node && node.position && node.position.end
9+
var final = last && last.position && last.position.end
1210

1311
if (!pos) {
14-
return null;
12+
return null
1513
}
1614

1715
/* This can only occur for `'atx'` and `'atx-closed'`
@@ -20,25 +18,29 @@ function style(node, relative) {
2018
* `'atx-closed'` heading. */
2119
if (!last) {
2220
if (pos.column - 1 <= depth * 2) {
23-
return consolidate(depth, relative);
21+
return consolidate(depth, relative)
2422
}
2523

26-
return 'atx-closed';
24+
return 'atx-closed'
2725
}
2826

2927
if (final.line + 1 === pos.line) {
30-
return 'setext';
28+
return 'setext'
3129
}
3230

3331
if (final.column + depth < pos.column) {
34-
return 'atx-closed';
32+
return 'atx-closed'
3533
}
3634

37-
return consolidate(depth, relative);
35+
return consolidate(depth, relative)
3836
}
3937

4038
/* Get the probable style of an atx-heading, depending on
4139
* preferred style. */
4240
function consolidate(depth, relative) {
43-
return depth < 3 ? 'atx' : relative === 'atx' || relative === 'setext' ? relative : null;
41+
return depth < 3
42+
? 'atx'
43+
: relative === 'atx' || relative === 'setext'
44+
? relative
45+
: null
4446
}

package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"devDependencies": {
2626
"browserify": "^16.0.0",
2727
"nyc": "^12.0.0",
28+
"prettier": "^1.14.2",
2829
"remark": "^9.0.0",
2930
"remark-cli": "^5.0.0",
3031
"remark-preset-wooorm": "^4.0.0",
@@ -33,17 +34,24 @@
3334
"xo": "^0.22.0"
3435
},
3536
"scripts": {
36-
"build-md": "remark . --quiet --frail --output",
37+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3738
"build-bundle": "browserify . -s mdastUtilHeadingStyle > mdast-util-heading-style.js",
3839
"build-mangle": "browserify . -s mdastUtilHeadingStyle -p tinyify > mdast-util-heading-style.min.js",
39-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
40-
"lint": "xo",
41-
"test-api": "node test.js",
40+
"build": "npm run build-bundle && npm run build-mangle",
41+
"test-api": "node test",
4242
"test-coverage": "nyc --reporter lcov tape test.js",
43-
"test": "npm run build && npm run lint && npm run test-coverage"
43+
"test": "npm run format && npm run build && npm run test-coverage"
44+
},
45+
"prettier": {
46+
"tabWidth": 2,
47+
"useTabs": false,
48+
"singleQuote": true,
49+
"bracketSpacing": false,
50+
"semi": false,
51+
"trailingComma": "none"
4452
},
4553
"xo": {
46-
"space": true,
54+
"prettier": true,
4755
"esnext": false,
4856
"ignore": [
4957
"mdast-util-heading-style.js"

readme.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ npm install mdast-util-heading-style
1313
## Usage
1414

1515
```js
16-
var style = require('mdast-util-heading-style');
17-
var remark = require('remark')();
16+
var style = require('mdast-util-heading-style')
17+
var remark = require('remark')()
1818

19-
style(remark.parse('# ATX').children[0]); // 'atx'
20-
style(remark.parse('# ATX #\n').children[0]); // 'atx-closed'
21-
style(remark.parse('ATX\n===').children[0]); // 'setext'
19+
style(remark.parse('# ATX').children[0]) // => 'atx'
20+
style(remark.parse('# ATX #\n').children[0]) // => 'atx-closed'
21+
style(remark.parse('ATX\n===').children[0]) // => 'setext'
2222

23-
style(remark.parse('### ATX').children[0]); // null
24-
style(remark.parse('### ATX').children[0], 'setext'); // 'setext'
23+
style(remark.parse('### ATX').children[0]) // => null
24+
style(remark.parse('### ATX').children[0], 'setext') // => 'setext'
2525
```
2626

2727
## API

test.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,108 @@
1-
'use strict';
1+
'use strict'
22

3-
var test = require('tape');
4-
var remark = require('remark')();
5-
var style = require('.');
3+
var test = require('tape')
4+
var remark = require('remark')()
5+
var style = require('.')
66

7-
test('mdast-util-heading-style', function (t) {
8-
t.throws(
9-
function () {
10-
style();
11-
},
12-
'should fail without node'
13-
);
7+
test('mdast-util-heading-style', function(t) {
8+
t.throws(function() {
9+
style()
10+
}, 'should fail without node')
1411

1512
t.equal(
1613
style({
1714
type: 'heading',
18-
children: [
19-
{type: 'text', value: 'foo'}
20-
]
15+
children: [{type: 'text', value: 'foo'}]
2116
}),
2217
null,
2318
'should NOT fail on undetectable nodes'
24-
);
19+
)
2520

26-
t.equal(
27-
style(remark.parse('# ATX').children[0]),
28-
'atx',
29-
'should detect atx'
30-
);
21+
t.equal(style(remark.parse('# ATX').children[0]), 'atx', 'should detect atx')
3122

3223
t.equal(
3324
style(remark.parse('# ATX #').children[0]),
3425
'atx-closed',
3526
'should detect closed atx'
36-
);
27+
)
3728

3829
t.equal(
3930
style(remark.parse('ATX\n===').children[0]),
4031
'setext',
4132
'should detect closed setext'
42-
);
33+
)
4334

4435
t.equal(
4536
style(remark.parse('### ATX').children[0]),
4637
null,
4738
'should work on ambiguous nodes'
48-
);
39+
)
4940

5041
t.equal(
5142
style(remark.parse('### ATX').children[0], 'atx'),
5243
'atx',
5344
'should work on ambiguous nodes (preference to atx)'
54-
);
45+
)
5546

5647
t.equal(
5748
style(remark.parse('### ATX').children[0], 'setext'),
5849
'setext',
5950
'should work on ambiguous nodes (preference to setext)'
60-
);
51+
)
6152

6253
t.equal(
6354
style(remark.parse('###### ######').children[0]),
6455
'atx-closed',
6556
'should work on empty nodes (#1)'
66-
);
57+
)
6758

6859
t.equal(
6960
style(remark.parse('### ###').children[0]),
7061
'atx-closed',
7162
'should work on empty nodes (#2)'
72-
);
63+
)
7364

7465
t.equal(
7566
style(remark.parse('# #').children[0]),
7667
'atx-closed',
7768
'should work on empty nodes (#3)'
78-
);
69+
)
7970

8071
t.equal(
8172
style(remark.parse('###### ').children[0], 'atx'),
8273
'atx',
8374
'should work on empty nodes (#4)'
84-
);
75+
)
8576

8677
t.equal(
8778
style(remark.parse('### ').children[0], 'atx'),
8879
'atx',
8980
'should work on empty nodes (#5)'
90-
);
81+
)
9182

9283
t.equal(
9384
style(remark.parse('## ').children[0]),
9485
'atx',
9586
'should work on empty nodes (#6)'
96-
);
87+
)
9788

9889
t.equal(
9990
style(remark.parse('###### ').children[0], 'setext'),
10091
'setext',
10192
'should work on empty nodes (#7)'
102-
);
93+
)
10394

10495
t.equal(
10596
style(remark.parse('### ').children[0], 'setext'),
10697
'setext',
10798
'should work on empty nodes (#8)'
108-
);
99+
)
109100

110101
t.equal(
111102
style(remark.parse('## ').children[0], 'setext'),
112103
'atx',
113104
'should work on empty nodes (#9)'
114-
);
105+
)
115106

116-
t.end();
117-
});
107+
t.end()
108+
})

0 commit comments

Comments
 (0)