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 13, 2018
1 parent 01fee31 commit 8de801b
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 134 deletions.
26 changes: 13 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
'use strict';
'use strict'

var toString = require('mdast-util-to-string');
var visit = require('unist-util-visit');
var slugs = require('github-slugger')();
var toString = require('mdast-util-to-string')
var visit = require('unist-util-visit')
var slugs = require('github-slugger')()

module.exports = slug;
module.exports = slug

function slug() {
return transformer;
return transformer
}

/* Patch slugs on heading nodes. */
function transformer(ast) {
slugs.reset();
slugs.reset()

visit(ast, 'heading', visitor);
visit(ast, 'heading', visitor)

function visitor(node) {
var id = slugs.slug(toString(node));
var id = slugs.slug(toString(node))

if (!node.data) {
node.data = {};
node.data = {}
}

if (!node.data.hProperties) {
node.data.hProperties = {};
node.data.hProperties = {}
}

node.data.id = id;
node.data.hProperties.id = id;
node.data.id = id
node.data.hProperties.id = id
}
}
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,24 @@
"xo": "^0.22.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify . -s remarkSlug > remark-slug.js",
"build-mangle": "browserify . -s remarkSlug -p tinyify > remark-slug.min.js",
"build-md": "remark . -qfo",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.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-slug.js"
Expand Down
20 changes: 10 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ Say we have the following file, `example.md`:
And our script, `example.js`, looks as follows:

```javascript
var fs = require('fs');
var unified = require('unified');
var markdown = require('remark-parse');
var slug = require('remark-slug');
var remark2rehype = require('remark-rehype');
var html = require('rehype-stringify');
var fs = require('fs')
var unified = require('unified')
var markdown = require('remark-parse')
var slug = require('remark-slug')
var remark2rehype = require('remark-rehype')
var html = require('rehype-stringify')

unified()
.use(markdown)
.use(slug)
.use(remark2rehype)
.use(html)
.process(fs.readFileSync('example.md'), function (err, file) {
if (err) throw err;
console.log(String(file));
});
.process(fs.readFileSync('example.md'), function(err, file) {
if (err) throw err
console.log(String(file))
})
```

Now, running `node example` yields:
Expand Down
211 changes: 106 additions & 105 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,143 +1,144 @@
'use strict';
'use strict'

var test = require('tape');
var remark = require('remark');
var u = require('unist-builder');
var removePosition = require('unist-util-remove-position');
var slug = require('.');
var test = require('tape')
var remark = require('remark')
var u = require('unist-builder')
var removePosition = require('unist-util-remove-position')
var slug = require('.')

function process(doc, options) {
var processor = remark().use(slug, options);
var tree = processor.runSync(processor.parse(doc));
var processor = remark().use(slug, options)

removePosition(tree, true);

return tree;
return removePosition(processor.runSync(processor.parse(doc)), true)
}

test('remark-slug', function (t) {
var processor = remark().use(slug);
var ast;
test('remark-slug', function(t) {
var processor = remark().use(slug)
var ast

ast = process([
'# Normal',
'',
'## Table of Contents',
'',
'# Baz',
''
].join('\n'));
ast = process(
['# Normal', '', '## Table of Contents', '', '# Baz', ''].join('\n')
)

t.deepEqual(
[ast.children[0].data.id, ast.children[1].data.id, ast.children[2].data.id],
['normal', 'table-of-contents', 'baz'],
'should patch `id`s'
);
)

t.deepEqual(
[ast.children[0].data.hProperties.id, ast.children[1].data.hProperties.id, ast.children[2].data.hProperties.id],
[
ast.children[0].data.hProperties.id,
ast.children[1].data.hProperties.id,
ast.children[2].data.hProperties.id
],
['normal', 'table-of-contents', 'baz'],
'should patch `hProperties.id`s'
);
)

ast = processor.parse('# Normal', {position: false});
ast = processor.parse('# Normal', {position: false})

ast.children[0].data = {foo: 'bar'};
ast.children[0].data = {foo: 'bar'}

processor.run(ast);
processor.run(ast)

t.equal(
ast.children[0].data.foo,
'bar',
'should not overwrite `data` on headings'
);
)

ast = processor.parse('# Normal', {position: false});
ast = processor.parse('# Normal', {position: false})

ast.children[0].data = {
hProperties: {className: 'bar'}
};
}

processor.run(ast);
processor.run(ast)

t.equal(
ast.children[0].data.hProperties.className,
'bar',
'should not overwrite `data.hProperties` on headings'
);

t.deepEqual(process([
'## I ♥ unicode',
'',
'## Dash-dash',
'',
'## en–dash',
'',
'## em–dash',
'',
'## 😄 unicode emoji',
'',
'## 😄-😄 unicode emoji',
'',
'## 😄_😄 unicode emoji',
'',
'##',
'',
'## ',
'',
'## Initial spaces',
'',
'## Final spaces ',
'',
'## Duplicate',
'',
'## Duplicate',
'',
'## :ok: No underscore',
'',
'## :ok_hand: Single',
'',
'## :ok_hand::hatched_chick: Two in a row with no spaces',
'',
'## :ok_hand: :hatched_chick: Two in a row',
''
].join('\n')),
u('root', [
heading('I ♥ unicode', 'i--unicode'),
heading('Dash-dash', 'dash-dash'),
heading('en–dash', 'endash'),
heading('em–dash', 'emdash'),
heading('😄 unicode emoji', '-unicode-emoji'),
heading('😄-😄 unicode emoji', '--unicode-emoji'),
heading('😄_😄 unicode emoji', '_-unicode-emoji'),
heading(null, ''),
heading(null, '-1'),
heading('Initial spaces', 'initial-spaces'),
heading('Final spaces', 'final-spaces'),
heading('Duplicate', 'duplicate'),
heading('Duplicate', 'duplicate-1'),
heading(':ok: No underscore', 'ok-no-underscore'),
heading(':ok_hand: Single', 'ok_hand-single'),
heading(
':ok_hand::hatched_chick: Two ' +
'in a row with no spaces',
'ok_handhatched_chick-two-in-a-row-' +
'with-no-spaces'
),
heading(
':ok_hand: :hatched_chick: Two in a row',
'ok_hand-hatched_chick-two-in-a-row'
)
]),
'should create GitHub slugs'
);
)

t.end();
});
t.deepEqual(
process(
[
'## I ♥ unicode',
'',
'## Dash-dash',
'',
'## en–dash',
'',
'## em–dash',
'',
'## 😄 unicode emoji',
'',
'## 😄-😄 unicode emoji',
'',
'## 😄_😄 unicode emoji',
'',
'##',
'',
'## ',
'',
'## Initial spaces',
'',
'## Final spaces ',
'',
'## Duplicate',
'',
'## Duplicate',
'',
'## :ok: No underscore',
'',
'## :ok_hand: Single',
'',
'## :ok_hand::hatched_chick: Two in a row with no spaces',
'',
'## :ok_hand: :hatched_chick: Two in a row',
''
].join('\n')
),
u('root', [
heading('I ♥ unicode', 'i--unicode'),
heading('Dash-dash', 'dash-dash'),
heading('en–dash', 'endash'),
heading('em–dash', 'emdash'),
heading('😄 unicode emoji', '-unicode-emoji'),
heading('😄-😄 unicode emoji', '--unicode-emoji'),
heading('😄_😄 unicode emoji', '_-unicode-emoji'),
heading(null, ''),
heading(null, '-1'),
heading('Initial spaces', 'initial-spaces'),
heading('Final spaces', 'final-spaces'),
heading('Duplicate', 'duplicate'),
heading('Duplicate', 'duplicate-1'),
heading(':ok: No underscore', 'ok-no-underscore'),
heading(':ok_hand: Single', 'ok_hand-single'),
heading(
':ok_hand::hatched_chick: Two in a row with no spaces',
'ok_handhatched_chick-two-in-a-row-with-no-spaces'
),
heading(
':ok_hand: :hatched_chick: Two in a row',
'ok_hand-hatched_chick-two-in-a-row'
)
]),
'should create GitHub slugs'
)

t.end()
})

function heading(label, id) {
return u('heading', {
depth: 2,
data: {id: id, hProperties: {id: id}}
}, label ? [u('text', label)] : []);
return u(
'heading',
{
depth: 2,
data: {id: id, hProperties: {id: id}}
},
label ? [u('text', label)] : []
)
}

0 comments on commit 8de801b

Please sign in to comment.