Feat:支持解析md文件中带html格式的标题文本
This commit is contained in:
parent
d93825dd57
commit
5cfc313f8e
@ -1,11 +1,17 @@
|
|||||||
import { fromMarkdown } from 'mdast-util-from-markdown'
|
import { fromMarkdown } from 'mdast-util-from-markdown'
|
||||||
|
|
||||||
const getNodeValue = node => {
|
const getNodeText = node => {
|
||||||
if (node.children[0]) {
|
// 优先找出其中的text类型的子节点
|
||||||
if (node.children[0].value !== undefined) {
|
let textChild = (node.children || []).find(item => {
|
||||||
return node.children[0].value
|
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 ''
|
return ''
|
||||||
}
|
}
|
||||||
@ -19,7 +25,7 @@ const handleList = node => {
|
|||||||
let node = {}
|
let node = {}
|
||||||
node.data = {
|
node.data = {
|
||||||
// 节点内容
|
// 节点内容
|
||||||
text: getNodeValue(cur)
|
text: getNodeText(cur)
|
||||||
}
|
}
|
||||||
node.children = []
|
node.children = []
|
||||||
newArr.push(node)
|
newArr.push(node)
|
||||||
@ -55,7 +61,7 @@ export const transformMarkdownTo = md => {
|
|||||||
let node = {}
|
let node = {}
|
||||||
node.data = {
|
node.data = {
|
||||||
// 节点内容
|
// 节点内容
|
||||||
text: getNodeValue(cur)
|
text: getNodeText(cur)
|
||||||
}
|
}
|
||||||
node.children = []
|
node.children = []
|
||||||
// 如果当前的层级大于上一个节点的层级,那么是其子节点
|
// 如果当前的层级大于上一个节点的层级,那么是其子节点
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user