Skip to content

Commit 0ec3eb9

Browse files
committed
Refactor code-style
1 parent 66d933e commit 0ec3eb9

File tree

3 files changed

+89
-63
lines changed

3 files changed

+89
-63
lines changed

index.js

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

3-
var visit = require('unist-util-visit');
3+
var visit = require('unist-util-visit')
44

5+
module.exports = normalizeHeadings
56

6-
module.exports = function (ast) {
7-
var title;
8-
var titleCount = 0;
7+
var max = 6
98

10-
visit(ast, 'heading', function (node) {
11-
if (node.depth == 1 && !titleCount++) {
12-
title = node;
13-
}
14-
});
9+
function normalizeHeadings(tree) {
10+
var multiple
11+
var first
12+
var title
13+
14+
visit(tree, 'heading', infer)
1515

16-
if (!titleCount) {
17-
visit(ast, 'heading', function (node) {
18-
node.depth = 1;
19-
return false;
20-
});
16+
// If there are no titles, but there is a first heading.
17+
if (!title && first) {
18+
first.depth = 1
2119
}
2220

23-
if (titleCount > 1) {
24-
visit(ast, 'heading', function (node) {
25-
if (node !== title && node.depth < 6) {
26-
node.depth += 1;
21+
// If there are multiple titles.
22+
if (multiple) {
23+
visit(tree, 'heading', increase)
24+
}
25+
26+
return tree
27+
28+
function infer(node) {
29+
if (!first) {
30+
first = node
31+
}
32+
33+
if (node.depth === 1) {
34+
if (title) {
35+
multiple = true
36+
} else {
37+
title = node
2738
}
28-
});
39+
}
2940
}
3041

31-
return ast;
32-
};
42+
function increase(node) {
43+
if (node !== title && node.depth < max) {
44+
node.depth++
45+
}
46+
}
47+
}

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@
3030
"unist-util-visit": "^1.0.0"
3131
},
3232
"devDependencies": {
33+
"prettier": "^1.14.2",
3334
"remark": "^9.0.0",
34-
"tape": "^4.0.0"
35+
"tape": "^4.0.0",
36+
"xo": "^0.22.0"
3537
},
3638
"scripts": {
39+
"format": "prettier --write \"**/*.js\" && xo --fix",
3740
"test": "tape test/*.js"
41+
},
42+
"prettier": {
43+
"tabWidth": 2,
44+
"useTabs": false,
45+
"singleQuote": true,
46+
"bracketSpacing": false,
47+
"semi": false,
48+
"trailingComma": "none"
49+
},
50+
"xo": {
51+
"prettier": true,
52+
"esnext": false
3853
}
3954
}

test/test.js

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
'use strict';
2-
3-
var normalizeHeadings = require('..');
4-
5-
var test = require('tape'),
6-
remark = require('remark');
7-
8-
var fs = require('fs'),
9-
path = require('path');
10-
11-
12-
var load = function (filename) {
13-
var markdown = fs.readFileSync(path.join(__dirname, filename));
14-
return remark()
15-
.use({settings: {position: false}})
16-
.parse(markdown);
17-
};
18-
19-
20-
test.Test.prototype.check = function (test, message) {
21-
var input = load(test + '.in');
22-
var output = load(test + '.out');
23-
24-
this.deepEqual(normalizeHeadings(input), output, message);
25-
};
26-
27-
28-
test('Multiple top-level headings', function (t) {
29-
t.check('no-headings', 'No-op if there is no top-level headings');
30-
t.check('one-heading', 'No-op if there is a single top-level heading');
31-
t.check('two-headings', 'Makes the second header one level deeper');
32-
t.check('more-headings', 'Shifts all other headings one level deeper');
33-
t.end();
34-
});
35-
36-
37-
test('Level 7', function (t) {
38-
t.check('hierarchy', 'There is no depth level 7');
39-
t.end();
40-
});
1+
'use strict'
2+
3+
var fs = require('fs')
4+
var path = require('path')
5+
var test = require('tape')
6+
var remark = require('remark')
7+
var normalizeHeadings = require('..')
8+
9+
var proc = remark().use({settings: {position: false}})
10+
11+
test.Test.prototype.check = check
12+
13+
test('Multiple top-level headings', function(t) {
14+
t.check('no-headings', 'No-op if there is no top-level headings')
15+
t.check('one-heading', 'No-op if there is a single top-level heading')
16+
t.check('two-headings', 'Makes the second header one level deeper')
17+
t.check('more-headings', 'Shifts all other headings one level deeper')
18+
t.end()
19+
})
20+
21+
test('Level 7', function(t) {
22+
t.check('hierarchy', 'There is no depth level 7')
23+
t.end()
24+
})
25+
26+
function check(test, message) {
27+
this.deepEqual(
28+
normalizeHeadings(load(test + '.in')),
29+
load(test + '.out'),
30+
message
31+
)
32+
}
33+
34+
function load(basename) {
35+
return proc.parse(fs.readFileSync(path.join(__dirname, basename)))
36+
}

0 commit comments

Comments
 (0)