From 5cfc313f8e6e4e66051809c7464318a23bb12274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Mon, 13 May 2024 10:13:48 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E6=94=AF=E6=8C=81=E8=A7=A3?= =?UTF-8?q?=E6=9E=90md=E6=96=87=E4=BB=B6=E4=B8=AD=E5=B8=A6html=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E6=A0=87=E9=A2=98=E6=96=87=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/parse/markdownTo.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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 = [] // 如果当前的层级大于上一个节点的层级,那么是其子节点