               atom.notifications.addError 'mermaid error', detail: err
 
             if mermaidAPI.parse(content)
-              container = document.getElementsByClassName('markdown-preview-enhanced')[0]
               div = document.createElement('div')
               div.classList.add('mermaid')
               div.textContent = content
-              container.appendChild(div)
+              document.body.appendChild(div)
 
               mermaid.init null, div, ()->
                 svgElement = div.getElementsByTagName('svg')[0]
-                return cb(null, null) if not svgElement
 
                 dest = path.resolve(rootDirectoryPath, (Math.random().toString(36).substr(2, 9)) + '_mermaid.png')
 
-                saveSvgAsPng svgElement, dest, (error)->
-                  container.removeChild(div)
+                saveSvgAsPng svgElement, dest, {}, (error)->
+                  document.body.removeChild(div)
                   cb(null, {dest, start, end, content})
             else
               cb(null, null)


            dest = path.resolve(rootDirectoryPath, (Math.random().toString(36).substr(2, 9)) + '_viz.png')

            saveSvgAsPng div.children[0], dest, (error)->
              cb(null, {dest, start, end, content})




  return processCodes(codes, lines, rootDirectoryPath, callback)

saveSvgAsPng = (svgElement, dest, option={}, cb)->
  return cb(null) if !svgElement or svgElement.tagName.toLowerCase() != 'svg'

  if typeof(option) == 'function' and !cb
    cb = option
    option = {}

  svgAsPngUri svgElement, option, (data)->
    base64Data = data.replace(/^data:image\/png;base64,/, "")
    fs.writeFile dest, base64Data, 'base64', (err)->
      cb(err)
