-
-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
可以把MD文本格式数据传递进行渲染吗? #204
Comments
暂时不行,getDataMd 是输出 markdown |
@MaxwellEdisons Try this function for converting Markdown to the required node format:
There may be some bugs in there, but it should work for most cases |
thanks you are awesome |
function markdownToJSON(markdown: string): Node {
const lines = markdown.split("\n")
const root: Node = {
nodeData: {
id: "root",
topic: lines[0].substring(2),
root: true,
expanded: true,
children: [],
},
linkData: {},
}
let lastNodes: NodeData[] = [root.nodeData]
let listDepth = 0
for (let i = 1; i < lines.length; i++) {
const line = lines[i]
const trimmedLine = line.trim()
const leadingSpaces = line.length - line.trimStart().length
if (trimmedLine.startsWith("#")) {
listDepth = 0 // Reset list depth when encountering a header
let level = trimmedLine.split(" ")[0].length
let node: NodeData = {
topic: trimmedLine.substring(level + 1).trim(),
id: generateId(),
direction: level === 2 ? listDepth % 2 : 0,
expanded: true,
children: [],
}
while (lastNodes.length >= level) {
lastNodes.pop();
}
lastNodes.push(node);
lastNodes[lastNodes.length - 2].children.push(node);
} else if (trimmedLine.startsWith("-")) {
const listItemLevel = Math.floor(leadingSpaces / 2) + 1 // 2 spaces per level
listDepth = listItemLevel
let node: NodeData = {
topic: trimmedLine.substring(1).trim(),
id: generateId(),
listItem: true,
expanded: true,
children: [],
}
while (lastNodes.length >= listItemLevel + 1) {
lastNodes.pop();
}
lastNodes.push(node);
lastNodes[lastNodes.length - 2].children.push(node);
} else if (trimmedLine.length !== 0) {
lastNodes[lastNodes.length - 1].topic += "\n" + trimmedLine
}
}
return root
} I made a simple modification to this function, and now I can parse such a format, I hope it will help others |
可以把MD文本格式数据传递进行渲染吗?getDataMd这个方法传递进去无效
The text was updated successfully, but these errors were encountered: