const crypto = require("crypto");
const mdx = require("../utils/mdx");
const extractExports = require("../utils/extract-exports");

module.exports = async ({ id, node, content }) => {
  const code = await mdx(content);

  // extract all the exports
  const { frontmatter, ...nodeExports } = extractExports(code);

  const mdxNode = {
    id,
    children: [],
    parent: node.id,
    internal: {
      content: content,
      type: "Mdx"
    }
  };

  mdxNode.frontmatter = {
    title: ``, // always include a title
    ...frontmatter,
    _PARENT: node.id
  };

  mdxNode.excerpt = frontmatter.excerpt;
  mdxNode.exports = nodeExports;
  mdxNode.rawBody = content;

  // Add path to the markdown file path
  if (node.internal.type === `File`) {
    mdxNode.fileAbsolutePath = node.absolutePath;
  }

  mdxNode.internal.contentDigest = crypto
    .createHash(`md5`)
    .update(JSON.stringify(mdxNode))
    .digest(`hex`);

  return mdxNode;
};

@@ -179,21 +179,36 @@
  =%3E %7B%0A  
-const
+let code%0A  try %7B%0A   
  code = 
@@ -226,16 +226,172 @@
 ontent);
+%0A  %7D catch(e) %7B%0A    // add the path of the file to simplify debugging error messages%0A    e.message += %60$%7Bnode.absolutePath%7D: $%7Be.message%7D%60;%0A    throw e;%0A  %7D
 %0A%0A  // e

make error messaging useful