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 u from 'unist-builder';
import mdastDefinitions from 'mdast-util-definitions'; import mdastDefinitions from 'mdast-util-definitions';
@ -46,10 +46,18 @@ export default function remarkSquashReferences() {
*/ */
if (['imageReference', 'linkReference'].includes(node.type)) { if (['imageReference', 'linkReference'].includes(node.type)) {
const type = node.type === 'imageReference' ? 'image' : 'link'; const type = node.type === 'imageReference' ? 'image' : 'link';
const { title, url } = getDefinition(node.identifier) || {}; const definition = getDefinition(node.identifier);
if (definition) {
const { title, url } = definition;
return u(type, { title, url, alt: node.alt }, children); return u(type, { title, url, alt: node.alt }, children);
} }
const pre = u('text', node.type === 'imageReference' ? '![' : '[');
const post = u('text', ']');
return [ pre, ...children, post];
}
/** /**
* Remove definition nodes and filter the resulting null values from the * Remove definition nodes and filter the resulting null values from the
* filtered children array. * filtered children array.
@ -60,6 +68,6 @@ export default function remarkSquashReferences() {
const filteredChildren = without(children, null); const filteredChildren = without(children, null);
return { ...node, children: filteredChildren }; return { ...node, children: flatten(filteredChildren) };
} }
} }