src/index.js
function visitCodeBlock(ast, vFile, isSimple) {
  return visit(ast, 'code', (node, index, parent) => {
    const { lang, value, position } = node;
    const destinationDir = getDestinationDir(vFile);
    let newNode;

    // If this codeblock is not mermaid, bail.
    if (lang !== 'mermaid') {
      return node;
    }

    // Are we just transforming to a <div>, or replacing with an image?
    if (isSimple) {
      newNode = createMermaidDiv(value);

      vFile.info(`${lang} code block replaced with div`, position, PLUGIN_NAME);

    // Otherwise, let's try and generate a graph!
    } else {
      let graphSvgFilename;
      try {
        graphSvgFilename = render(value, destinationDir);

        vFile.info(`${lang} code block replaced with graph`, position, PLUGIN_NAME);
      } catch (error) {
        vFile.message(error, position, PLUGIN_NAME);
        return node;
      }

      newNode = {
        type: 'image',
        title: '`mermaid` image',
        url: graphSvgFilename,
      };
    }

    parent.children.splice(index, 1, newNode);

    return node;
  });
}
function visitCodeBlock(ast, vFile, isSimple) {
  return visit(ast, 'code', (node, index, parent) => {
    const { lang, value, position } = node;
    const destinationDir = getDestinationDir(vFile);
    let newNode;

    // If this codeblock is not mermaid, bail.
    if (lang !== 'mermaid') {
      return node;
    }

    // Are we just transforming to a <div>, or replacing with an image?
    if (isSimple) {
      newNode = createMermaidDiv(value);

      vFile.info(`${lang} code block replaced with div`, position, PLUGIN_NAME);

    // Otherwise, let's try and generate a graph!
    } else {
      let graphSvgFilename;
      try {
        graphSvgFilename = render(value, destinationDir);

        vFile.info(`${lang} code block replaced with graph`, position, PLUGIN_NAME);
      } catch (error) {
        vFile.message(error, position, PLUGIN_NAME);
        return node;
      }

      newNode = {
        type: 'image',
        title: '`mermaid` image',
        url: graphSvgFilename,
      };
    }

    parent.children.splice(index, 1, newNode);

    return node;
  });
}
function visitCodeBlock ( ast , vFile , isSimple ) { return visit ( ast , 'code' , ( node , index , parent ) => { const { lang , value , position } = node ; const destinationDir = getDestinationDir ( vFile ) ; let newNode ; // If this codeblock is not mermaid, bail. if ( lang !== 'mermaid' ) { return node ; } // Are we just transforming to a <div>, or replacing with an image? if ( isSimple ) { newNode = createMermaidDiv ( value ) ; vFile . info ( ` ${ lang } ` , position , PLUGIN_NAME ) ; // Otherwise, let's try and generate a graph! } else { let graphSvgFilename ; try { graphSvgFilename = render ( value , destinationDir ) ; vFile . info ( ` ${ lang } ` , position , PLUGIN_NAME ) ; } catch ( error ) { vFile . message ( error , position , PLUGIN_NAME ) ; return node ; } newNode = { type : 'image' , title : '`mermaid` image' , url : graphSvgFilename , } ; } parent . children . splice ( index , 1 , newNode ) ; return node ; } ) ; }
Given the MDAST ast, look for all fenced codeblocks that have a language of
`mermaid` and pass that to mermaid.cli to render the image. Replaces the
codeblocks with an image of the rendered graph.

@param {object}  ast
@param {vFile}   vFile
@param {boolean} isSimple
@return {function}
Given the MDAST ast look for all fenced codeblocks that have a language of mermaid and pass that to mermaid . cli to render the image . Replaces the codeblocks with an image of the rendered graph .