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) }; } }