mdast-util-definitions
Advanced tools
Comparing version 1.1.1 to 1.2.0
112
index.js
@@ -1,92 +0,50 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2015-2016 Titus Wormer. All rights reserved. | ||
* @license MIT | ||
* @module mdast:util:definitions | ||
* @fileoverview Get a definition in `node` by `identifier`. | ||
*/ | ||
'use strict'; | ||
/* | ||
* Dependencies. | ||
*/ | ||
var visit = require('unist-util-visit'); | ||
var has = require('has'); | ||
/** | ||
* Factory to get a node from the given definition-cache. | ||
* | ||
* @private | ||
* @param {Object.<string, Node>} cache - Definitions. | ||
* @return {function(string): Node?} - Getter, bound to | ||
* `cache`. | ||
*/ | ||
function getterFactory(cache) { | ||
/** | ||
* Get a node from the bound definition-cache. | ||
* | ||
* @private | ||
* @param {string} identifier - Identifier of | ||
* definition. | ||
* @return {Node?} - Definition, if found. | ||
*/ | ||
function getter(identifier) { | ||
return (identifier && cache[identifier.toUpperCase()]) || null; | ||
} | ||
module.exports = getDefinitionFactory; | ||
return getter; | ||
/* Get a definition in `node` by `identifier`. */ | ||
function getDefinitionFactory(node, options) { | ||
return getterFactory(gather(node, options)); | ||
} | ||
/** | ||
* Gather all definitions in `node` | ||
* | ||
* @private | ||
* @param {Node} node - (Grand)parent of definitions. | ||
* @return {Object.<string, Node>} - Map of found | ||
* definitions by their identifier. | ||
*/ | ||
function gather(node) { | ||
var cache = {}; | ||
/* Gather all definitions in `node` */ | ||
function gather(node, options) { | ||
var cache = {}; | ||
if (!node || !node.type) { | ||
throw new Error('mdast-util-definitions expected node'); | ||
} | ||
if (!node || !node.type) { | ||
throw new Error('mdast-util-definitions expected node'); | ||
} | ||
/** | ||
* Add `definition` to `cache` if it has an identifier. | ||
* | ||
* @param {Node} definition - Definition node. | ||
*/ | ||
function check(definition) { | ||
cache[definition.identifier.toUpperCase()] = definition; | ||
visit(node, 'definition', options && options.commonmark ? commonmark : normal); | ||
return cache; | ||
function commonmark(definition) { | ||
var id = normalise(definition.identifier); | ||
if (!has(cache, id)) { | ||
cache[id] = definition; | ||
} | ||
} | ||
visit(node, 'definition', check); | ||
function normal(definition) { | ||
cache[normalise(definition.identifier)] = definition; | ||
} | ||
} | ||
return cache; | ||
/* Factory to get a node from the given definition-cache. */ | ||
function getterFactory(cache) { | ||
return getter; | ||
/* Get a node from the bound definition-cache. */ | ||
function getter(identifier) { | ||
var id = identifier && normalise(identifier); | ||
return id && has(cache, id) ? cache[id] : null; | ||
} | ||
} | ||
/** | ||
* Get a definition in `node` by `identifier`. | ||
* | ||
* Supports weird keys (like `__proto__`). | ||
* | ||
* @example | ||
* var ast = remark.parse('[example]: http://example.com "Example"'); | ||
* var getDefinition = getDefinitionFactory(ast); | ||
* getDefinition('example'); | ||
* // {type: 'definition', 'title': 'Example', ...} | ||
* | ||
* @param {Node} node - (Grand)parent of definitions. | ||
* @return {function(string): Node?} - Getter. | ||
*/ | ||
function getDefinitionFactory(node) { | ||
return getterFactory(gather(node)); | ||
function normalise(identifier) { | ||
return identifier.toUpperCase(); | ||
} | ||
/* | ||
* Expose | ||
*/ | ||
module.exports = getDefinitionFactory; |
{ | ||
"name": "mdast-util-definitions", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Find definition nodes in mdast nodes", | ||
@@ -16,2 +16,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"has": "^1.0.1", | ||
"unist-util-visit": "^1.0.0" | ||
@@ -30,28 +31,29 @@ }, | ||
"browserify": "^13.0.0", | ||
"eslint": "^2.0.0", | ||
"esmangle": "^1.0.0", | ||
"istanbul": "^0.4.0", | ||
"jscs": "^3.0.0", | ||
"jscs-jsdoc": "^2.0.0", | ||
"remark": "^5.0.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-slug": "^4.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"tape": "^4.0.0" | ||
"remark": "^6.2.0", | ||
"nyc": "^10.0.0", | ||
"remark-cli": "^2.0.0", | ||
"remark-preset-wooorm": "^1.0.0", | ||
"tape": "^4.0.0", | ||
"xo": "^0.17.1" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . --quiet --frail", | ||
"build-md": "remark . --quiet --frail --output", | ||
"build-bundle": "browserify index.js --no-builtins -s mdastUtilDefinitions > mdast-util-definitions.js", | ||
"build-mangle": "esmangle mdast-util-definitions.js > mdast-util-definitions.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint-api": "eslint .", | ||
"lint-style": "jscs --reporter inline .", | ||
"lint": "npm run lint-api && npm run lint-style", | ||
"lint": "xo", | ||
"test-api": "node test.js", | ||
"test-coverage": "istanbul cover test.js", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"ignore": [ | ||
"mdast-util-definitions.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"presets": "wooorm" | ||
} | ||
} |
# mdast-util-definitions [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat] | ||
<!--lint disable list-item-spacing heading-increment list-item-indent--> | ||
<!--lint disable no-duplicate-headings--> | ||
Get definitions in [MDAST][] nodes by `identifier`. Supports funky | ||
@@ -18,5 +14,2 @@ keys, like `__proto__` or `toString`. | ||
**mdast-util-definitions** is also available as an AMD, CommonJS, and | ||
globals module, [uncompressed and compressed][releases]. | ||
## Usage | ||
@@ -41,9 +34,11 @@ | ||
### `definitions(node)` | ||
### `definitions(node[, options])` | ||
Create a cache of all `definition`s in `node`. | ||
Create a cache of all `definition`s in [`node`][node]. | ||
###### Parameters | ||
###### `options` | ||
* `node` ([`Node`][node]) — Ancestor of definitions. | ||
* `commonmark` (`boolean`, default: false) — Turn on to use CommonMark | ||
precedence: ignore later found definitions for duplicate definitions. | ||
The default behaviour is to prefer the last found definition. | ||
@@ -82,4 +77,2 @@ ###### Returns | ||
[releases]: https://github.com/wooorm/mdast-util-definitions/releases | ||
[license]: LICENSE | ||
@@ -86,0 +79,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8
5762
2
38
86
+ Addedhas@^1.0.1
+ Addedhas@1.0.4(transitive)