From 39cfd466bff952e40565f033c7539ff1b7cda6d4 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 4 Feb 2016 10:53:11 +0100 Subject: [PATCH] Remove peer-dependency on `remark-slug` The peer-dependency is now automatically included. --- index.js | 54 ++++++++++++------------------------ readme.md | 7 ++--- test/fixtures/suggestions.md | 4 +++ test/index.js | 32 --------------------- 4 files changed, 25 insertions(+), 72 deletions(-) diff --git a/index.js b/index.js index 46e1ee1..781adf6 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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.} - 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. */ @@ -314,17 +294,12 @@ 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; } }); } @@ -332,7 +307,7 @@ function gatherExposedFactory() { return gather; } - gather.done = done; + gather.cache = cache; return gather; } @@ -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); } }); @@ -564,6 +536,16 @@ function attacher(remark, options, fileSet) { fileSet.use(completerFactory(repo)); + /* + * Attach `slug`. + */ + + remark.use(slug); + + /* + * Expose transformer. + */ + return transformerFactory(repo, fileSet); } diff --git a/readme.md b/readme.md index 5bab417..d77e9d4 100644 --- a/readme.md +++ b/readme.md @@ -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: @@ -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: # diff --git a/test/fixtures/suggestions.md b/test/fixtures/suggestions.md index 2ec246f..23b2068 100644 --- a/test/fixtures/suggestions.md +++ b/test/fixtures/suggestions.md @@ -5,3 +5,7 @@ This should suggest: [link](#helloo). ## Files So should this: [k](example.md#fiiiles). + +## + +An empty header. diff --git a/test/index.js b/test/index.js index 875fa0d..7fe144d 100644 --- a/test/index.js +++ b/test/index.js @@ -76,31 +76,6 @@ 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'], @@ -108,7 +83,6 @@ test('remark-validate-links', function (t) { 'detectRC': false, 'detectIgnore': false, 'plugins': [ - '../../../node_modules/remark-slug', '../../../index.js' ] }, function (err, res) { @@ -141,7 +115,6 @@ test('remark-validate-links', function (t) { 'detectRC': false, 'detectIgnore': false, 'plugins': [ - '../../../node_modules/remark-slug', '../../../index.js' ] }, function (err, res) { @@ -188,7 +161,6 @@ test('remark-validate-links', function (t) { 'detectRC': false, 'detectIgnore': false, 'plugins': [ - '../../../node_modules/remark-slug', '../../../index.js' ] }, function (err, res) { @@ -246,7 +218,6 @@ test('remark-validate-links', function (t) { 'detectRC': false, 'detectIgnore': false, 'plugins': [ - '../../../node_modules/remark-slug', '../../../index.js' ] }, function (err, res) { @@ -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' } @@ -377,7 +347,6 @@ test('remark-validate-links', function (t) { 'detectRC': false, 'detectIgnore': false, 'plugins': [ - '../../../node_modules/remark-slug', '../../../index.js' ] }, function (err, res) { @@ -468,7 +437,6 @@ test('remark-validate-links', function (t) { 'detectRC': false, 'detectIgnore': false, 'plugins': [ - '../../../node_modules/remark-slug', '../../../index.js' ] }, function (err, res) {