{"version":3,"file":"index.mjs","sources":["../src/templates/fragment.ts","../src/templates/react.ts","../src/templates/string.ts","../src/index.ts"],"sourcesContent":["export default (html: string) => `export default () => {\n  const div = document.createElement('div')\n  div.innerHTML = ${html}\n\n  const frag = document.createDocumentFragment()\n  frag.append(...div.children)\n  return frag\n}`\n","export default (html: string) => `export default (props) => {\n  const __html = ${html}\n  return <div {...props} dangerouslySetInnerHTML={{ __html }} />\n}`\n","export default (html: string) => `export default ${html}`\n","import type { Plugin } from \"vite\";\nimport type { PluggableList } from \"unified\";\n\nimport { readFile } from \"fs/promises\";\nimport { join } from \"path\";\n\nimport { unified } from \"unified\";\nimport remarkParse from \"remark-parse\";\nimport remarkRehype from \"remark-rehype\";\nimport rehypeStringify from \"rehype-stringify\";\nimport type { Options as RemarkParseOptions } from \"remark-parse\";\nimport type { Options as RemarkRehypeOptions } from \"remark-rehype\";\nimport type { Options as RehypeStringifyOptions } from \"rehype-stringify\";\n\nimport * as DEFAULT_TEMPLATES from \"./templates\";\nimport { TemplateFn } from \"./templates/utils\";\n\ntype PluginOptions = {\n  templates?: Record<string, TemplateFn>,\n  remarkPlugins?: PluggableList;\n  rehypePlugins?: PluggableList;\n  remarkParseOptions?: RemarkParseOptions;\n  remarkRehypeOptions?: RemarkRehypeOptions;\n  rehypeStringifyOptions?: RehypeStringifyOptions;\n};\n\nexport * from './templates/utils'\n\nexport default function vitePlugin({\n  templates = {},\n  remarkPlugins = [],\n  rehypePlugins = [],\n  remarkParseOptions = {},\n  remarkRehypeOptions = {},\n  rehypeStringifyOptions = {},\n}: PluginOptions = {}): Plugin {\n  const templateFns = {\n    ...DEFAULT_TEMPLATES,\n    ...templates,\n  }\n\n  // unified plugin stack\n  const unifiedPlugins: PluggableList = [\n    [remarkParse, remarkParseOptions],\n    ...remarkPlugins,\n    [remarkRehype, remarkRehypeOptions],\n    ...rehypePlugins,\n    [rehypeStringify, rehypeStringifyOptions],\n  ];\n\n  const compiler = unified().use(unifiedPlugins);\n\n  let rootPath = process.cwd();\n\n  return {\n    name: \"vite-plugin-remark-rehype\",\n\n    configResolved(config) {\n      rootPath = config.root;\n    },\n\n    async transform(mdContent, id) {\n      try {\n        const url = new URL(id, 'file://')\n\n        if (url.pathname.endsWith(\".md\")) {\n          const templateName = url.searchParams.get('as') ?? 'string'\n          const templateFn = templateFns[templateName] ?? templateFns['string']\n\n          const htmlContent = await compiler.process(mdContent);\n          const stringifiedHtml = JSON.stringify(\n            htmlContent.toString(\"utf8\")\n          )\n\n          return {\n            code: templateFn(stringifiedHtml)\n          };\n        }\n      } catch {\n        // we dont know what file can be transformed so we're limiting\n        // to fs-loaded files for now. if there's a PR or some issue, let's see.\n      }\n    },\n\n    configureServer: (server) => {\n      server.middlewares.use(async (req, res, next) => {\n        if (!req.url?.endsWith(\".md\")) {\n          return next();\n        }\n\n        try {\n          const fileContent = await readFile(join(rootPath, req.url));\n          const htmlContent = await compiler.process(fileContent);\n\n          res.setHeader(\"Content-Type\", \"text/html; charset=utf-8\");\n          res.end(htmlContent.toString(\"utf-8\"));\n        } catch {\n          next();\n        }\n      });\n    },\n  };\n}\n"],"names":["html","vitePlugin","templates","remarkPlugins","rehypePlugins","remarkParseOptions","remarkRehypeOptions","rehypeStringifyOptions","templateFns","_extends","DEFAULT_TEMPLATES","unifiedPlugins","remarkParse","remarkRehype","rehypeStringify","unified","use","process","cwd","name","configResolved","config","rootPath","root","async","mdContent","id","url","URL","pathname","endsWith","_url$searchParams$get","_templateFns$template","templateName","searchParams","get","templateFn","htmlContent","compiler","code","JSON","stringify","toString","_unused","configureServer","server","middlewares","req","res","next","_req$url","fileContent","readFile","join","setHeader","end","_unused2"],"mappings":"kaAAA,+BAAgBA,GAAiB,0FAEbA,gHCFJA,GAAiB,iDACdA,gFCDHA,GAAmC,kBAAAA,KC4B3BC,SAAAA,GAAWC,UACjCA,EAAY,GAAEC,cACdA,EAAgB,GAAEC,cAClBA,EAAgB,GAAEC,mBAClBA,EAAqB,CAAE,EAAAC,oBACvBA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