Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 10, 2018
1 parent c55e2ec commit fa93dc8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
remark-toc.js
remark-toc.min.js
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict';
'use strict'

var slug = require('remark-slug');
var util = require('mdast-util-toc');
var slug = require('remark-slug')
var util = require('mdast-util-toc')

module.exports = toc;
module.exports = toc

var DEFAULT_HEADING = 'toc|table[ -]of[ -]contents?';
var DEFAULT_HEADING = 'toc|table[ -]of[ -]contents?'

function toc(options) {
var settings = options || {};
var heading = settings.heading || DEFAULT_HEADING;
var depth = settings.maxDepth || 6;
var tight = settings.tight;
var settings = options || {}
var heading = settings.heading || DEFAULT_HEADING
var depth = settings.maxDepth || 6
var tight = settings.tight

this.use(slug);
this.use(slug)

return transformer;
return transformer

/* Adds an example section based on a valid example
* JavaScript document to a `Usage` section. */
Expand All @@ -24,17 +24,17 @@ function toc(options) {
heading: heading,
maxDepth: depth,
tight: tight
});
})

if (result.index === null || result.index === -1 || !result.map) {
return;
return
}

/* Replace markdown. */
node.children = [].concat(
node.children.slice(0, result.index),
result.map,
node.children.slice(result.endIndex)
);
)
}
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"is-hidden": "^1.1.0",
"negate": "^1.0.0",
"nyc": "^12.0.0",
"prettier": "^1.14.2",
"remark": "^9.0.0",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
Expand All @@ -42,17 +43,24 @@
"xo": "^0.22.0"
},
"scripts": {
"build-md": "remark *.md -qfo",
"format": "remark *.md -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify . -s remarkToc > remark-toc.js",
"build-mangle": "browserify . -s remarkToc -p tinyify > remark-toc.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"ignores": [
"remark-toc.js"
Expand Down
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ Say we have the following file, `example.md`:
And our script, `example.js`, looks as follows:

```javascript
var vfile = require('to-vfile');
var remark = require('remark');
var toc = require('remark-toc');
var vfile = require('to-vfile')
var remark = require('remark')
var toc = require('remark-toc')

remark()
.use(toc)
.process(vfile.readSync('example.md'), function (err, file) {
if (err) throw err;
console.log(String(file));
});
.process(vfile.readSync('example.md'), function(err, file) {
if (err) throw err
console.log(String(file))
})
```

Now, running `node example` yields:
Expand Down
75 changes: 41 additions & 34 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
'use strict';
'use strict'

var test = require('tape');
var fs = require('fs');
var path = require('path');
var remark = require('remark');
var negate = require('negate');
var hidden = require('is-hidden');
var toc = require('..');
var test = require('tape')
var fs = require('fs')
var path = require('path')
var remark = require('remark')
var negate = require('negate')
var hidden = require('is-hidden')
var toc = require('..')

var read = fs.readFileSync;
var exists = fs.existsSync;
var join = path.join;
var read = fs.readFileSync
var join = path.join

var ROOT = join(__dirname, 'fixtures');
var ROOT = join(__dirname, 'fixtures')

var fixtures = fs.readdirSync(ROOT);
var fixtures = fs.readdirSync(ROOT)

function process(value, config) {
return remark().use(toc, config).processSync(value).toString();
return remark()
.use(toc, config)
.processSync(value)
.toString()
}

test('remark-toc()', function (t) {
t.equal(typeof toc, 'function', 'should be a function');
test('remark-toc()', function(t) {
t.equal(typeof toc, 'function', 'should be a function')

t.doesNotThrow(function () {
toc.call(remark());
}, 'should not throw if not passed options');
t.doesNotThrow(function() {
toc.call(remark())
}, 'should not throw if not passed options')

t.end();
});
t.end()
})

test('Fixtures', function (t) {
fixtures.filter(negate(hidden)).forEach(function (fixture) {
var filepath = join(ROOT, fixture);
var output = read(join(filepath, 'output.md'), 'utf-8');
var input = read(join(filepath, 'input.md'), 'utf-8');
var config = join(filepath, 'config.json');
var result;
test('Fixtures', function(t) {
fixtures.filter(negate(hidden)).forEach(function(fixture) {
var filepath = join(ROOT, fixture)
var output = read(join(filepath, 'output.md'), 'utf-8')
var input = read(join(filepath, 'input.md'), 'utf-8')
var config
var result

config = exists(config) ? JSON.parse(read(config, 'utf-8')) : {};
result = process(input, config);
try {
config = JSON.parse(read(join(filepath, 'config.json')))
} catch (err) {
config = {}
}

t.equal(result, output, 'should work on `' + fixture + '`');
});
result = process(input, config)

t.end();
});
t.equal(result, output, 'should work on `' + fixture + '`')
})

t.end()
})

0 comments on commit fa93dc8

Please sign in to comment.