var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import esbuild from "esbuild";
import fs from "fs";
import {transform} from "@astrojs/compiler";
function markdown({config}) {
  return {
    name: "@astrojs/vite-plugin-markdown",
    enforce: "pre",
    async load(id) {
      if (id.endsWith(".md")) {
        let source = await fs.promises.readFile(id, "utf8");
        let render = config.markdownOptions.render;
        let renderOpts = {};
        if (Array.isArray(render)) {
          renderOpts = render[1];
          render = render[0];
        }
        if (typeof render === "string") {
          ({default: render} = await import(render));
        }
        let renderResult = await render(source, renderOpts);
        let {frontmatter, metadata, code: astroResult} = renderResult;
        const _a = frontmatter, {layout = "", components = "", setup = ""} = _a, content = __objRest(_a, ["layout", "components", "setup"]);
        content.astro = metadata;
        const prelude = `---
${layout ? `import Layout from '${layout}';` : ""}
${components ? `import * from '${components}';` : ""}
${setup}
---`;
        if (/\bLayout\b/.test(prelude)) {
          astroResult = `${prelude}
<Layout content={${JSON.stringify(content)}}>

${astroResult}

</Layout>`;
        } else {
          astroResult = `${prelude}
${astroResult}`;
        }
        let {code: tsResult} = await transform(astroResult, {sourcefile: id, sourcemap: "inline", internalURL: "astro/internal"});
        tsResult = `
export const metadata = ${JSON.stringify(metadata)};
export const frontmatter = ${JSON.stringify(content)};
${tsResult}`;
        const {code, map} = await esbuild.transform(tsResult, {loader: "ts", sourcemap: "inline", sourcefile: id});
        return {
          code,
          map: null
        };
      }
      return null;
    }
  };
}
export {
  markdown as default
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic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