nimiq-vitepress-theme
Version:
Nimiq UI theme for VitePress
92 lines (91 loc) • 2.39 kB
JavaScript
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { viteHtmlToMarkdownPlugin } from "@mdream/vite";
import { GitChangelog } from "@nolebase/vitepress-plugin-git-changelog/vite";
import llmstxt from "vitepress-plugin-llms";
import { groupIconVitePlugin } from "../code-groups/vite.mjs";
function getProjectVersion(rootDir = process.cwd()) {
try {
const packageJsonPath = path.resolve(rootDir, "package.json");
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
return packageJson.version;
}
} catch (error) {
console.warn("Failed to read version from package.json:", error);
}
return void 0;
}
export function NimiqVitepressVitePlugin({
repoURL,
contentPath = "",
gitChangelog,
llms = true
}) {
const { resolveId, configureServer, load, transform } = groupIconVitePlugin();
const externalPlugins = [];
if (gitChangelog !== false) {
const changelogConfig = gitChangelog || (repoURL ? { repoURL } : {});
if (Object.keys(changelogConfig).length > 0) {
externalPlugins.push(GitChangelog(changelogConfig));
}
}
if (llms !== false) {
const llmsConfig = typeof llms === "object" ? llms : {};
externalPlugins.push(llmstxt(llmsConfig));
}
const version = getProjectVersion();
const nimiqConfig = {
repoURL,
contentPath,
version
};
const plugins = [
{
name: "nimiq-vitepress-plugin",
enforce: "pre",
resolveId,
configureServer,
load,
transform,
config: () => ({
define: {
__NIMIQ_VITEPRESS_CONFIG__: JSON.stringify(nimiqConfig)
},
optimizeDeps: {
exclude: [
"nimiq-vitepress-theme",
"virtual:nolebase-git-changelog"
]
},
ssr: {
noExternal: [
"nimiq-vitepress-theme"
]
}
})
},
...externalPlugins
];
plugins.push(
viteHtmlToMarkdownPlugin({
cacheEnabled: true,
exclude: [
"**/node_modules/**",
"**/@vite/**",
"**/@id/**",
"**/@fs/**",
"**/__*/**",
// Exclude Vite internals
"**/*.js",
"**/*.mjs",
"**/*.ts",
"**/*.tsx",
"**/*.json",
"**/*.css"
]
})
);
return plugins;
}