@live-demo/plugin-rspress
Version:
Rspress plugin for interactive live demos.
49 lines (47 loc) • 1.66 kB
JavaScript
import path from "node:path";
import { fileURLToPath } from "node:url";
import { getVirtualModulesCode, htmlTags, remarkPlugin, visitFilePaths } from "@live-demo/core";
//#region src/plugin.ts
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const demoDataByPath = {};
/**
* Included by default for every demo
**/
const defaultModules = ["react", "rspress/theme"];
function liveDemoPluginRspress(options) {
const { customLayout, includeModules } = options ?? {};
if (customLayout && !/LiveDemo\.(jsx?|tsx)$/.test(customLayout)) throw new Error("[LiveDemo]: `customLayout` path should end with 'LiveDemo.(jsx?|tsx)',\nExample: `path.join(__dirname, './src/CustomLiveDemo/LiveDemo.tsx')`");
const getDemoDataByPath = () => demoDataByPath;
const extraModules = includeModules || [];
const uniqueImports = new Set(defaultModules.concat(extraModules));
return {
name: "@live-demo/plugin-rspress",
config(config) {
config.markdown = config.markdown || {};
config.markdown.mdxRs = false;
return config;
},
async routeGenerated(routes) {
const filePaths = routes.map((route) => route.absolutePath);
visitFilePaths({
filePaths,
uniqueImports,
demoDataByPath
});
},
async addRuntimeModules() {
return { _live_demo_virtual_modules: getVirtualModulesCode(uniqueImports) };
},
builderConfig: { html: { tags: htmlTags } },
markdown: {
remarkPlugins: [[remarkPlugin, {
getDemoDataByPath,
options: options?.ui
}]],
globalComponents: [customLayout ?? path.join(__dirname, "../static/LiveDemo.tsx")]
}
};
}
//#endregion
export { liveDemoPluginRspress };