diff --git a/simple-mind-map/src/parse/markdownTo.js b/simple-mind-map/src/parse/markdownTo.js index e17038c7..a7407025 100644 --- a/simple-mind-map/src/parse/markdownTo.js +++ b/simple-mind-map/src/parse/markdownTo.js @@ -1,11 +1,17 @@ import { fromMarkdown } from 'mdast-util-from-markdown' -const getNodeValue = node => { - if (node.children[0]) { - if (node.children[0].value !== undefined) { - return node.children[0].value +const getNodeText = node => { + // 优先找出其中的text类型的子节点 + let textChild = (node.children || []).find(item => { + return item.type === 'text' + }) + // 没有找到,那么直接使用第一个子节点 + textChild = textChild || node.children[0] + if (textChild) { + if (textChild.value !== undefined) { + return textChild.value } - return getNodeValue(node.children[0]) + return getNodeText(textChild) } return '' } @@ -19,7 +25,7 @@ const handleList = node => { let node = {} node.data = { // 节点内容 - text: getNodeValue(cur) + text: getNodeText(cur) } node.children = [] newArr.push(node) @@ -55,7 +61,7 @@ export const transformMarkdownTo = md => { let node = {} node.data = { // 节点内容 - text: getNodeValue(cur) + text: getNodeText(cur) } node.children = [] // 如果当前的层级大于上一个节点的层级,那么是其子节点