UNPKG

vite-plugin-react18-pages

Version:

<p> <a href="https://www.npmjs.com/package/vite-plugin-react-pages" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/v/vite-plugin-react-pages.svg" alt="npm package" /></a> </p>

38 lines 1.62 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImageMdxPlugin = void 0; const unist_util_visit_1 = __importDefault(require("unist-util-visit")); // After we migrating to mdx@2.x, we should probably use: // https://github.com/remcohaszing/remark-mdx-images function ImageMdxPlugin() { return transformer; function transformer(tree, file) { const children = tree.children; const addImports = []; (0, unist_util_visit_1.default)(tree, 'image', (node, index, parent) => { const { url = '', alt, title } = node; if (url.startsWith('./') || url.startsWith('../')) { const nextIndex = addImports.length; const varName = `_img${nextIndex}`; addImports.push(`import ${varName} from "${url}";`); const altAttr = alt ? `alt="${alt}"` : ''; const titleAttr = title ? `title="${title}"` : ''; parent === null || parent === void 0 ? void 0 : parent.children.splice(index, 1, { type: 'jsx', value: `<img src={${varName}} ${altAttr} ${titleAttr} />`, }); } }); children.unshift(...addImports.map((importStr) => { return { type: 'import', value: importStr, }; })); } } exports.ImageMdxPlugin = ImageMdxPlugin; //# sourceMappingURL=mdx-plugin-image.js.map