A script to convert MDX with frontmatter to MDX with an export.
// node fs comes with promises nowconst fs = require("fs").promises;// front-matter is what we used in gatsby-plugin-mdx to parse out frontmatterconst fm = require("front-matter");const dir = "./a-directory";// node doesn't allow top-level async/await yet, so we make it a function// and then call itasync function run() {// read the filenames in the relevant directory.const files = await fs.readdir(dir, "utf-8");//await Promise.all(files.filter(name => name.endsWith("mdx")).map(async filename => {// if we don't use `utf-8` we get a bufferconst file = await fs.readFile(`${dir}/${filename}`, "utf-8");// attributes are the json representation of the frontmatter// body is everything elseconst { attributes, body } = fm(file);// grab the json representation of the frontmatter and export it// as the identifier metaconst content = `export const meta = ${JSON.stringify(attributes,null,2)}${body}`;// write the file back outawait fs.writeFile(`${dir}/${filename}`, content, "utf-8");// return content but it doesn't matter because we don't use itreturn content;}));}run();