使用Goldmark在golang中添加ChildNode会导致堆栈溢出
使用迭代而不是递归来遍历Node并添加ChildNode。
示例如下:
func AddChildNode(parentNode ast.Node, childNode ast.Node) {
var nodeQueue []ast.Node
nodeQueue = append(nodeQueue, parentNode)
for len(nodeQueue) > 0 {
curNode := nodeQueue[0]
nodeQueue = nodeQueue[1:]
if curParent, ok := curNode.(ast.Parent); ok {
curParent.AppendChild(childNode)
break
}
childNum := curNode.ChildCount()
for i := 0; i < childNum; i++ {
nodeQueue = append(nodeQueue, curNode.Child(i))
}
}
}