From dd9d49117a5d14ed54b07b831839328cf127983d Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Thu, 28 Sep 2017 11:32:17 -0400 Subject: [PATCH] do not squash references without definitions --- .../serializers/remarkSquashReferences.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/components/Widgets/Markdown/serializers/remarkSquashReferences.js b/src/components/Widgets/Markdown/serializers/remarkSquashReferences.js index 049e69cb..64c57f3d 100644 --- a/src/components/Widgets/Markdown/serializers/remarkSquashReferences.js +++ b/src/components/Widgets/Markdown/serializers/remarkSquashReferences.js @@ -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) }; } }