do not squash references without definitions

This commit is contained in:
Shawn Erquhart 2017-09-28 11:32:17 -04:00 committed by Benaiah Mischenko
parent 9fbdbf5171
commit dd9d49117a

View File

@ -1,4 +1,4 @@
import { without } from 'lodash';
import { without, flatten } from 'lodash';
import u from 'unist-builder';
import mdastDefinitions from 'mdast-util-definitions';
@ -46,8 +46,16 @@ export default function remarkSquashReferences() {
*/
if (['imageReference', 'linkReference'].includes(node.type)) {
const type = node.type === 'imageReference' ? 'image' : 'link';
const { title, url } = getDefinition(node.identifier) || {};
return u(type, { title, url, alt: node.alt }, children);
const definition = getDefinition(node.identifier);
if (definition) {
const { title, url } = definition;
return u(type, { title, url, alt: node.alt }, children);
}
const pre = u('text', node.type === 'imageReference' ? '![' : '[');
const post = u('text', ']');
return [ pre, ...children, post];
}
/**
@ -60,6 +68,6 @@ export default function remarkSquashReferences() {
const filteredChildren = without(children, null);
return { ...node, children: filteredChildren };
return { ...node, children: flatten(filteredChildren) };
}
}