11'use strict' ;
22
33var visit = require ( 'unist-util-visit' ) ;
4+ var has = require ( 'has' ) ;
45
56module . 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
0 commit comments