remark-slug
Advanced tools
Comparing version 3.0.2 to 4.0.0
@@ -5,2 +5,7 @@ <!--remark setext--> | ||
4.0.0 / 2016-01-23 | ||
================== | ||
* Refactor API ([`4104cd2`](https://github.com/wooorm/remark-slug/commit/4104cd2)) | ||
3.0.2 / 2016-01-23 | ||
@@ -7,0 +12,0 @@ ================== |
224
index.js
@@ -19,159 +19,39 @@ /** | ||
var visit = require('unist-util-visit'); | ||
var repeat = require('repeat-string'); | ||
var slugs = require('github-slugger')(); | ||
var slugg = null; | ||
var fs = {}; | ||
var path = {}; | ||
var proc = {}; | ||
try { | ||
slugg = require('slugg'); | ||
} catch (exception) {/* empty */} | ||
try { | ||
fs = require('fs'); | ||
} catch (exception) {/* empty */} | ||
try { | ||
path = require('path'); | ||
} catch (exception) {/* empty */} | ||
/* | ||
* Hide process use from browserify and component. | ||
*/ | ||
/* istanbul ignore else */ | ||
if (typeof global !== 'undefined') { | ||
/* global global */ | ||
proc = global.process; | ||
} | ||
/* | ||
* Methods. | ||
*/ | ||
var exists = fs.existsSync; | ||
var resolve = path.resolve; | ||
/* | ||
* Constants. | ||
*/ | ||
var MODULES = 'node_modules'; | ||
var EXTENSION = '.js'; | ||
var NPM = 'npm'; | ||
var GITHUB = 'github'; | ||
var SLUGG = 'slugg'; | ||
var DASH = '-'; | ||
var UNDERSCORE = '_'; | ||
var DEFAULT_LIBRARY = GITHUB; | ||
/** | ||
* Find a library. | ||
* Patch `value` on `context` at `key`, if | ||
* `context[key]` does not already exist. | ||
* | ||
* @param {string} pathlike - File-path-like to load. | ||
* @return {*} - Library. | ||
* @param {Object} context - Context to patch. | ||
* @param {string} key - Key to patch at. | ||
* @param {*} value - Value to patch. | ||
*/ | ||
function loadLibrary(pathlike) { | ||
var cwd; | ||
var local; | ||
var npm; | ||
var plugin; | ||
if (pathlike === SLUGG && slugg) { | ||
return slugg; | ||
function patch(context, key, value) { | ||
if (!context[key]) { | ||
context[key] = value; | ||
} | ||
cwd = proc.cwd && proc.cwd(); | ||
/* istanbul ignore if */ | ||
if (!cwd) { | ||
throw new Error('Cannot lazy load library when not in node'); | ||
} | ||
local = resolve(cwd, pathlike); | ||
npm = resolve(cwd, MODULES, pathlike); | ||
if (exists(local) || exists(local + EXTENSION)) { | ||
plugin = local; | ||
} else if (exists(npm)) { | ||
plugin = npm; | ||
} else { | ||
plugin = pathlike; | ||
} | ||
return require(plugin); | ||
return context[key]; | ||
} | ||
/** | ||
* Wraps `slugg` to generate slugs just like npm would. | ||
* Patch slugs on heading nodes. | ||
* | ||
* @see https://github.com/npm/marky-markdown/blob/9761c95/lib/headings.js#L17 | ||
* Transformer is invoked for every file, so there’s no need | ||
* to specify extra logic to get per-file slug pools. | ||
* | ||
* @param {function(string): string} library - Value to | ||
* slugify. | ||
* @return {function(string): string} - Modifier. | ||
* @param {Node} ast - Root node. | ||
*/ | ||
function npmFactory(library) { | ||
/** | ||
* Generate slugs just like npm would. | ||
* | ||
* @param {string} value - Value to slugify. | ||
* @return {string} - Slug. | ||
*/ | ||
function npm(value) { | ||
return library(value).replace(/[<>]/g, '').toLowerCase(); | ||
} | ||
function transformer(ast) { | ||
slugs.reset(); | ||
return npm; | ||
} | ||
visit(ast, 'heading', function (node) { | ||
var id = slugs.slug(toString(node)); | ||
var data = patch(node, 'data', {}); | ||
/** | ||
* Wraps `slugg` to generate slugs just like GitHub would. | ||
* | ||
* @param {function(string): string} library - Library to | ||
* use. | ||
* @return {function(string): string} - Modifier. | ||
*/ | ||
function githubFactory(library) { | ||
/** | ||
* Hacky. Sometimes `slugg` uses `replacement` as an | ||
* argument to `String#replace()`, and sometimes as | ||
* a literal string. | ||
* | ||
* @param {string} $0 - Value to transform. | ||
* @return {string} - Replacement. | ||
*/ | ||
function separator($0) { | ||
var match = $0.match(/\s/g); | ||
if ($0 === DASH || $0 === UNDERSCORE) { | ||
return $0; | ||
} | ||
return repeat(DASH, match ? match.length : 0); | ||
} | ||
/** | ||
* @see seperator | ||
* @return {string} - Dash. | ||
*/ | ||
function dash() { | ||
return DASH; | ||
} | ||
separator.toString = dash; | ||
/** | ||
* Generate slugs just like GitHub would. | ||
* | ||
* @param {string} value - Value to slugify. | ||
* @return {string} - Slug. | ||
*/ | ||
function github(value) { | ||
return library(value, separator).toLowerCase(); | ||
} | ||
return github; | ||
patch(data, 'id', id); | ||
patch(data, 'htmlAttributes', {}); | ||
patch(data.htmlAttributes, 'id', id); | ||
}); | ||
} | ||
@@ -182,59 +62,5 @@ | ||
* | ||
* @param {Remark} remark - Processor. | ||
* @param {Object?} [options] - Configuration. | ||
* @return {function(node)} - Transformer. | ||
* @return {Function} - Transformer. | ||
*/ | ||
function attacher(remark, options) { | ||
var settings = options || {}; | ||
var library = settings.library || DEFAULT_LIBRARY; | ||
var isNPM = library === NPM; | ||
var isGitHub = library === GITHUB; | ||
if (isNPM || isGitHub) { | ||
library = SLUGG; | ||
} | ||
if (typeof library === 'string') { | ||
library = loadLibrary(library); | ||
} | ||
if (isNPM) { | ||
library = npmFactory(library); | ||
} else if (isGitHub) { | ||
library = githubFactory(library); | ||
} | ||
/** | ||
* Patch `value` on `context` at `key`, if | ||
* `context[key]` does not already exist. | ||
* | ||
* @param {Object} context - Context to patch. | ||
* @param {string} key - Key to patch at. | ||
* @param {*} value - Value to patch. | ||
*/ | ||
function patch(context, key, value) { | ||
if (!context[key]) { | ||
context[key] = value; | ||
} | ||
return context[key]; | ||
} | ||
/** | ||
* Adds an example section based on a valid example | ||
* JavaScript document to a `Usage` section. | ||
* | ||
* @param {Node} ast - Root node. | ||
*/ | ||
function transformer(ast) { | ||
visit(ast, 'heading', function (node) { | ||
var id = library(toString(node)); | ||
var data = patch(node, 'data', {}); | ||
patch(data, 'id', id); | ||
patch(data, 'htmlAttributes', {}); | ||
patch(data.htmlAttributes, 'id', id); | ||
}); | ||
} | ||
function attacher() { | ||
return transformer; | ||
@@ -241,0 +67,0 @@ } |
{ | ||
"name": "remark-slug", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"description": "Add anchors to remark heading nodes", | ||
@@ -16,6 +16,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"github-slugger": "^1.0.0", | ||
"mdast-util-to-string": "^1.0.0", | ||
"unist-util-visit": "^1.0.0", | ||
"repeat-string": "^1.5.0", | ||
"slugg": "^0.1.0" | ||
"unist-util-visit": "^1.0.0" | ||
}, | ||
@@ -46,4 +45,3 @@ "repository": { | ||
"slug": "^0.9.0", | ||
"tape": "^4.0.0", | ||
"to-slug-case": "^1.0.0" | ||
"tape": "^4.0.0" | ||
}, | ||
@@ -50,0 +48,0 @@ "scripts": { |
# remark-slug [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
Add anchors to [**remark**][remark] heading nodes. | ||
Add anchors to [**remark**][remark] heading nodes using GitHub’s | ||
algorithm. | ||
@@ -45,4 +46,3 @@ > Works great with [**remark-html**][remark-html], | ||
```bash | ||
remark ... -u remark-slug | ||
remark ... -u 'remark-slug=library:"npm"' | ||
remark readme.md -u slug | ||
``` | ||
@@ -52,3 +52,3 @@ | ||
### `remark.use(slug[, options])` | ||
### `remark.use(slug)` | ||
@@ -64,20 +64,4 @@ Adds slugs to markdown headings. | ||
* `slug` — This plugin; | ||
* `slug` — This plugin. | ||
* `options` (`Object?`) | ||
* `'library'` — (`string` or `Function`, default: `'github'`): | ||
* `'github'` — Slugs just like GitHub; | ||
* `'npm'` | ||
— Slugs just like npm (caveat: npm doesn’t support links in | ||
headings, [yet][marky-pr]); | ||
* `string` (e.g., `'slug'`, `'slugg'`) | ||
— Library to require (not in the browser); | ||
* `Function` (e.g., `require('slugg')`) | ||
— Library to use. | ||
## License | ||
@@ -107,4 +91,2 @@ | ||
[marky-pr]: https://github.com/npm/marky-markdown/pull/38 | ||
[remark]: https://github.com/wooorm/remark | ||
@@ -111,0 +93,0 @@ |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
3
15
0
8160
59
95
+ Addedgithub-slugger@^1.0.0
+ Addedgithub-slugger@1.5.0(transitive)
- Removedrepeat-string@^1.5.0
- Removedslugg@^0.1.0
- Removedrepeat-string@1.6.1(transitive)
- Removedslugg@0.1.2(transitive)