Skip to content

Commit ff05889

Browse files
committed
Add support for commonmark mode
1 parent eecd70f commit ff05889

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
'use strict';
22

33
var visit = require('unist-util-visit');
4+
var has = require('has');
45

56
module.exports = getDefinitionFactory;
67

78
/* Get a definition in `node` by `identifier`. */
8-
function getDefinitionFactory(node) {
9-
return getterFactory(gather(node));
9+
function getDefinitionFactory(node, options) {
10+
return getterFactory(gather(node, options));
1011
}
1112

1213
/* Gather all definitions in `node` */
13-
function gather(node) {
14+
function gather(node, options) {
1415
var cache = {};
1516

1617
if (!node || !node.type) {
1718
throw new Error('mdast-util-definitions expected node');
1819
}
1920

20-
visit(node, 'definition', check);
21+
visit(node, 'definition', options && options.commonmark ? commonmark : normal);
2122

2223
return cache;
2324

24-
/* Add `definition` to `cache` if it has an identifier. */
25-
function check(definition) {
25+
function commonmark(definition) {
26+
var id = normalise(definition.identifier);
27+
if (!has(cache, id)) {
28+
cache[id] = definition;
29+
}
30+
}
31+
32+
function normal(definition) {
2633
cache[normalise(definition.identifier)] = definition;
2734
}
2835
}
@@ -33,7 +40,8 @@ function getterFactory(cache) {
3340

3441
/* Get a node from the bound definition-cache. */
3542
function getter(identifier) {
36-
return (identifier && cache[normalise(identifier)]) || null;
43+
var id = identifier && normalise(identifier);
44+
return id && has(cache, id) ? cache[id] : null;
3745
}
3846
}
3947

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"cache"
1414
],
1515
"dependencies": {
16+
"has": "^1.0.1",
1617
"unist-util-visit": "^1.0.0"
1718
},
1819
"repository": "https://github.com/wooorm/mdast-util-definitions",

readme.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ definition('foo');
3030

3131
## API
3232

33-
### `definitions(node)`
33+
### `definitions(node[, options])`
3434

35-
Create a cache of all `definition`s in `node`.
35+
Create a cache of all `definition`s in [`node`][node].
3636

37-
###### Parameters
37+
###### `options`
3838

39-
* `node` ([`Node`][node]) — Ancestor of definitions.
39+
* `commonmark` (`boolean`, default: false) — Turn on to use CommonMark
40+
precedence: ignore later found definitions for duplicate definitions.
41+
The default behaviour is to prefer the last found definition.
4042

4143
###### Returns
4244

test.js

+17
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,22 @@ test('mdast-util-definitions', function (t) {
6666
'should work on weird identifiers when not found'
6767
);
6868

69+
tree = remark().parse([
70+
'[example]: http://one.com',
71+
'[example]: http://two.com'
72+
].join('\n'));
73+
74+
t.deepEqual(
75+
definitions(tree)('example').url,
76+
'http://two.com',
77+
'should prefer the last of duplicate definitions by default'
78+
);
79+
80+
t.deepEqual(
81+
definitions(tree, {commonmark: true})('example').url,
82+
'http://one.com',
83+
'should prefer the first of duplicate definitions in commonmark mode'
84+
);
85+
6986
t.end();
7087
});

0 commit comments

Comments
 (0)