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 9, 2018
1 parent d2131f4 commit 0d162c5
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 155 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

# remark-defsplit

[![Build Status][travis-badge]][travis] [![Dependency Status][david-badge]][david]
[![Build Status][travis-badge]][travis]
[![Dependency Status][david-badge]][david]

Extract inline link/image destinations as separate definitions.

[travis]: https://travis-ci.org/eush77/remark-defsplit
[travis-badge]: https://travis-ci.org/eush77/remark-defsplit.svg
[david]: https://david-dm.org/eush77/remark-defsplit
[david-badge]: https://david-dm.org/eush77/remark-defsplit.png

## Example

```
```bash
$ travisjs badge
[![Build Status](https://travis-ci.org/eush77/remark-defsplit.svg?branch=master)](https://travis-ci.org/eush77/remark-defsplit)
```

```
```bash
$ travisjs badge | remark --use remark-defsplit='id:["travis-badge","travis"]'
[![Build Status][travis-badge]][travis]

Expand All @@ -31,13 +27,13 @@ $ travisjs badge | remark --use remark-defsplit='id:["travis-badge","travis"]'
With [remark](https://github.com/wooorm/remark) do:
```
```javascript
remark().use(remarkDefsplit, [options]).processSync(src)
```
#### options.id
Type: `String | [String]` <br>
Type: `String | [String]`.
Default: `[]`
Array of identifiers to use for new definitions in place of auto-generated ones.
Expand All @@ -46,7 +42,7 @@ Array of identifiers to use for new definitions in place of auto-generated ones.
With [remark](https://github.com/wooorm/remark) do:
```
```sh
remark --use remark-defsplit[=options] </path/to/src
```
Expand All @@ -59,15 +55,24 @@ remark --use remark-defsplit[=options] </path/to/src
— Reverse, thus rewriting references and definitions into normal links
and images
[remark-reference-links]: https://github.com/wooorm/remark-reference-links
[remark-inline-links]: https://github.com/wooorm/remark-inline-links

## Install
```
```sh
npm install remark-defsplit
```
## License
MIT
[travis]: https://travis-ci.org/eush77/remark-defsplit
[travis-badge]: https://travis-ci.org/eush77/remark-defsplit.svg
[david]: https://david-dm.org/eush77/remark-defsplit
[david-badge]: https://david-dm.org/eush77/remark-defsplit.png
[remark-reference-links]: https://github.com/wooorm/remark-reference-links
[remark-inline-links]: https://github.com/wooorm/remark-inline-links
138 changes: 73 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,102 @@
'use strict';
'use strict'

var Index = require('unist-util-index');

var url = require('url'),
path = require('path');
var url = require('url')
var path = require('path')
var Index = require('unist-util-index')

var concat = [].concat
var push = [].push

module.exports = defsplit

function defsplit(opts) {
var id = (opts || {}).id || []
var ids = (typeof id === 'object' && 'length' in id ? id : [id]).map(String)

return transform

function transform(ast) {
var definitionsById = new Index(ast, 'definition', 'identifier')
var definitionsByUrl = new Index(ast, 'definition', 'url')
var definitions = []
var hosts = Object.create(null)

postorder(ast)

push.apply(ast.children, definitions)

function postorder(node) {
var nodes

module.exports = function (opts) {
opts = opts || {};
opts.id = (opts.id
? (Array.isArray(opts.id)
? opts.id.map(String)
: [String(opts.id)])
: []);

return function (ast) {
var definitionsById = Index(ast, 'definition', function (definition) {
return definition.identifier.toLowerCase();
});
var definitionsByLink = Index(ast, 'definition', 'url');
var newDefinitions = [];
var hostCount = Object.create(null);

(function postorder (node) {
if (node.children) {
node.children = concat.apply([], node.children.map(postorder));
node.children = concat.apply([], node.children.map(postorder))
}
return forNode.apply(null, arguments) || node;
}(ast));

[].push.apply(ast.children, newDefinitions);

function forNode (node) {
if (node.type == 'definition' || node.type == 'heading') {
var nodes = newDefinitions.concat(node);
newDefinitions.length = 0;
return nodes;
if (node.type === 'definition' || node.type === 'heading') {
nodes = definitions.concat(node)
definitions = []
return nodes
}

if (node.type !== 'link' && node.type !== 'image') return;
if (node.type === 'link' || node.type === 'image') {
node.type += 'Reference'
node.referenceType = 'full'
node.identifier = identifier(node.url, node.title)

node.type += 'Reference';
node.referenceType = 'full';
node.identifier = identifier(node.url, node.title);
delete node.url
delete node.title
}

delete node.url;
delete node.title;
return node
}

function identifier (link, title) {
var identifier;

var found = definitionsByLink.get(link).some(function (def) {
if (def.title == title) {
identifier = def.identifier;
return true;
}
});
function identifier(link, title) {
var identifier = null
var found = definitionsByUrl.get(link).some(some)
var host
var definition

if (found) {
return identifier;
return identifier
}

if (!(identifier = opts.id.shift())) {
var host = urlHost(link);
hostCount[host] |= 0;
identifier = ids.shift()

if (!identifier) {
host = urlHost(link)
hosts[host] |= 0

do {
identifier = (host ? host + '-' : '') + ++hostCount[host];
} while (definitionsById.get(identifier).length);
identifier = (host ? host + '-' : '') + ++hosts[host]
} while (definitionsById.get(identifier).length)
}

var newDefinition = {
definition = {
type: 'definition',
identifier: identifier,
title: title,
url: link
};
}

newDefinitions.push(newDefinition);
definitionsById.add(newDefinition);
definitionsByLink.add(newDefinition);
definitions.push(definition)
definitionsById.add(definition)
definitionsByUrl.add(definition)

return identifier;
}
};
};
return identifier

function some(def) {
if (def.title === title) {
identifier = def.identifier
return true
}

return false
}
}
}
}

function urlHost (link) {
var host = url.parse(link).host;
return host ? path.parse(host).name : '';
function urlHost(link) {
var host = url.parse(link).host
return host ? path.parse(host).name : ''
}
27 changes: 25 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,33 @@
"unist-util-index": "^1.0.0"
},
"devDependencies": {
"prettier": "^1.14.2",
"remark": "^9.0.0",
"tape": "^4.0.2"
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.0.2",
"xo": "^0.22.0"
},
"scripts": {
"test": "node test/test"
"format": "remark *.md -qfo && prettier --write \"**/*.js\" && xo --fix",
"test-api": "node test/test",
"test": "npm run format && npm run test-api"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true,
"esnext": false
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
}
}
Loading

0 comments on commit 0d162c5

Please sign in to comment.