From 2baa500c1741a9f821f7bae98311d1c69d5430de 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: Thu, 7 Mar 2024 11:27:09 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BC=98=E5=8C=96markdown=E7=9A=84?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=A6=82=E8=A6=81?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/parse/toMarkdown.js | 30 +++++++++++-------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/simple-mind-map/src/parse/toMarkdown.js b/simple-mind-map/src/parse/toMarkdown.js index 13e48053..8d391f4d 100644 --- a/simple-mind-map/src/parse/toMarkdown.js +++ b/simple-mind-map/src/parse/toMarkdown.js @@ -1,12 +1,7 @@ -import { walk } from '../utils' +import { walk, nodeRichTextToTextWithWrap } from '../utils' -let el = null -const getText = str => { - if (!el) { - el = document.createElement('div') - } - el.innerHTML = str - return el.textContent +const getNodeText = data => { + return data.richText ? nodeRichTextToTextWithWrap(data.text) : data.text } const getTitleMark = level => { @@ -24,21 +19,22 @@ export const transformToMarkdown = root => { root, null, (node, parent, isRoot, layerIndex) => { - let level = layerIndex + 1 - let text = node.data.richText ? getText(node.data.text) : node.data.text + const level = layerIndex + 1 if (level <= 6) { content += getTitleMark(level) } else { content += getIndentMark(level) } - content += ' ' + text + content += ' ' + getNodeText(node.data) // 概要 - let generalization = node.data.generalization - if (generalization && generalization.text) { - let generalizationText = generalization.richText - ? getText(generalization.text) - : generalization.text - content += `[${generalizationText}]` + const generalization = node.data.generalization + if (Array.isArray(generalization)) { + content += generalization.map(item => { + return ` [${getNodeText(item)}]` + }) + } else if (generalization && generalization.text) { + const generalizationText = getNodeText(generalization) + content += ` [${generalizationText}]` } content += '\n\n' // 备注