Skip to content

Commit

Permalink
Remove peer-dependency on remark-slug
Browse files Browse the repository at this point in the history
The peer-dependency is now automatically included.
  • Loading branch information
wooorm committed Feb 4, 2016
1 parent da90ead commit 39cfd46
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 72 deletions.
54 changes: 18 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var visit = require('unist-util-visit');
var definitions = require('mdast-util-definitions');
var gh = require('github-url-to-object');
var urljoin = require('urljoin');
var slug = require('remark-slug');

/*
* Methods.
Expand Down Expand Up @@ -272,32 +273,11 @@ function gatherReferences(file, project) {
*/
function gatherExposedFactory() {
var cache = {};
var hasHeadings = false;
var hasSlugs = false;

/**
* Access found files and headings.
*
* @return {Object.<string, boolean>} - Map of
* file-paths, with `true` as their value.
* @throws {Error} - When headings are found, but no
* heading has a slug.
*/
function done() {
if (hasHeadings && !hasSlugs) {
throw new Error(
'Missing slugs. Use for example `remark-slug` to generate ' +
'heading IDs'
);
}

return cache;
}

/**
* Find headings in `file`.
*
* @property {Function} done - Access found links.
* @property {Object} cache - Found links.
* @param {File} file - Virtual file.
* @returns {Function} - Itself.
*/
Expand All @@ -314,25 +294,20 @@ function gatherExposedFactory() {
cache[filePath] = true;

visit(ast, 'heading', function (node) {
var data = node.data || {};
var attributes = data.htmlAttributes || {};
var id = attributes.id || data.id;

/* istanbul ignore next */
id = id || (node.attributes && node.attributes.id);

hasHeadings = true;
var data = node.data || /* istanbul ignore next */ {};
var attrs = data.htmlAttributes;
var id = (attrs && attrs.id) || data.id;

if (id) {
cache[filePath + '#' + id] = hasSlugs = true;
cache[filePath + '#' + id] = true;
}
});
}

return gather;
}

gather.done = done;
gather.cache = cache;

return gather;
}
Expand Down Expand Up @@ -434,16 +409,13 @@ function completerFactory(project) {
*/
function completer(set, done) {
var gatherExposed = gatherExposedFactory();
var exposed;

set.valueOf().forEach(gatherExposed);

exposed = gatherExposed.done();

set.valueOf().forEach(function (file) {
/* istanbul ignore else - stdin */
if (file.filePath()) {
validate(exposed, file, project);
validate(gatherExposed.cache, file, project);
}
});

Expand Down Expand Up @@ -564,6 +536,16 @@ function attacher(remark, options, fileSet) {

fileSet.use(completerFactory(repo));

/*
* Attach `slug`.
*/

remark.use(slug);

/*
* Expose transformer.
*/

return transformerFactory(repo, fileSet);
}

Expand Down
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ npm install remark-validate-links

![Example of how remark-validate-links looks on screen](https://cdn.rawgit.com/wooorm/remark-validate-links/master/screenshot.png)

Use **remark-validate-links** together with [**remark**](https://github.com/wooorm/remark)
and [**remark-slug**](https://github.com/wooorm/remark-slug):
Use **remark-validate-links** together with [**remark**](https://github.com/wooorm/remark):

```bash
npm install --global remark remark-slug remark-validate-links
npm install --global remark remark-validate-links
```

Let’s say `readme.md` is this document, and `example.md` looks as follows:
Expand All @@ -58,7 +57,7 @@ So does this: [README](readme.md#installation).
Then, to run **remark-validate-links** on `example.md` and `readme.md`:

```bash
remark -u remark-slug -u remark-validate-links example.md
remark -u remark-validate-links example.md
#
# Yields:
#
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/suggestions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ This should suggest: [link](#helloo).
## Files

So should this: [k](example.md#fiiiles).

##

An empty header.
32 changes: 0 additions & 32 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,13 @@ test('remark-validate-links', function (t) {
'should throw an error when not on the CLI'
);

t.test('should warn when used without slugs', function (st) {
assertion({
'files': 'example.md',
'color': false,
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../index.js'
]
}, function (err, res) {
st.ifErr(err);

st.equal(
res.stderr.split('\n').slice(0, 2).join('\n'),
[
'example.md',
' 1:1 error Error: Missing slugs. Use for ' +
'example `remark-slug` to generate heading IDs'
].join('\n')
);

st.end();
});
});

t.test('should ignore unfound files', function (st) {
assertion({
'files': ['definitions.md', 'FOOOO'],
'color': false,
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../node_modules/remark-slug',
'../../../index.js'
]
}, function (err, res) {
Expand Down Expand Up @@ -141,7 +115,6 @@ test('remark-validate-links', function (t) {
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../node_modules/remark-slug',
'../../../index.js'
]
}, function (err, res) {
Expand Down Expand Up @@ -188,7 +161,6 @@ test('remark-validate-links', function (t) {
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../node_modules/remark-slug',
'../../../index.js'
]
}, function (err, res) {
Expand Down Expand Up @@ -246,7 +218,6 @@ test('remark-validate-links', function (t) {
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../node_modules/remark-slug',
'../../../index.js'
]
}, function (err, res) {
Expand Down Expand Up @@ -277,7 +248,6 @@ test('remark-validate-links', function (t) {
'detectRC': false,
'detectIgnore': false,
'plugins': {
'../../../node_modules/remark-slug': null,
'../../../index.js': {
'repository': 'wooorm/test'
}
Expand Down Expand Up @@ -377,7 +347,6 @@ test('remark-validate-links', function (t) {
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../node_modules/remark-slug',
'../../../index.js'
]
}, function (err, res) {
Expand Down Expand Up @@ -468,7 +437,6 @@ test('remark-validate-links', function (t) {
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../node_modules/remark-slug',
'../../../index.js'
]
}, function (err, res) {
Expand Down

0 comments on commit 39cfd46

Please sign in to comment.