@callstack/rspress-theme
Version:
Callstack theme for Rspress docs
109 lines (108 loc) • 4.72 kB
JavaScript
import node_fs from "node:fs";
import { createRequire } from "node:module";
import node_path from "node:path";
import { fileURLToPath } from "node:url";
const { resolve } = createRequire(import.meta.url);
const dirname = node_path.dirname(fileURLToPath(import.meta.url));
function excludeFalse(value) {
return false !== value;
}
function getThemeAssets() {
const assetsDirPath = node_path.join(dirname, '../assets');
const contents = node_fs.readdirSync(assetsDirPath);
return contents.map(node_path.parse).map(({ base, name })=>[
`@theme-assets/${name}`,
node_path.join(assetsDirPath, base)
]);
}
function getThemeAliases(existingThemeAlias) {
const ckThemeExportsPath = node_path.join(dirname, 'theme');
const rspressThemeDefaultPath = resolve('@rspress/theme-default', {
paths: [
resolve('rspress')
]
});
const aliases = {};
if (Array.isArray(existingThemeAlias)) {
const index = existingThemeAlias.indexOf(rspressThemeDefaultPath);
if (-1 !== index) {
aliases['@theme'] = existingThemeAlias.filter(excludeFalse).slice();
aliases['@theme'].splice(index, 0, ckThemeExportsPath);
} else aliases['@theme'] = existingThemeAlias.filter(excludeFalse).concat(ckThemeExportsPath);
} else aliases['@theme'] = ckThemeExportsPath;
aliases['@default-theme'] = rspressThemeDefaultPath;
aliases['rspress/theme'] = ckThemeExportsPath;
return aliases;
}
function getThemeAssetAlias(existingAssetAlias) {
const assetOverrides = getThemeAssets();
const aliases = {};
for (const [assetAlias, assetPath] of assetOverrides)if (Array.isArray(existingAssetAlias)) {
aliases[assetAlias] = existingAssetAlias.filter(excludeFalse);
aliases[assetAlias].push(assetPath);
} else if (existingAssetAlias) aliases[assetAlias] = [
existingAssetAlias,
assetPath
];
else aliases[assetAlias] = assetPath;
return aliases;
}
function getBuilderConfig(options) {
var _options_links, _options_links1, _options_links2;
return {
source: {
define: {
HOME_BANNER_LINK: JSON.stringify(null == (_options_links = options.links) ? void 0 : _options_links.homeBanner),
HOME_FOOTER_LINK: JSON.stringify(null == (_options_links1 = options.links) ? void 0 : _options_links1.homeFooter),
OUTLINE_CTA_LINK: JSON.stringify(null == (_options_links2 = options.links) ? void 0 : _options_links2.outlineCTA)
}
},
resolve: {
alias: (alias)=>{
const assetAliases = getThemeAssetAlias(alias['@theme-assets']);
Object.assign(alias, assetAliases);
const themeAssetsAlias = alias['@theme-assets'];
delete alias['@theme-assets'];
Object.assign(alias, {
'@theme-assets': themeAssetsAlias
});
const themeAliases = getThemeAliases(alias['@theme']);
Object.assign(alias, themeAliases);
}
}
};
}
function addThemeOverrides(themeConfig = {}) {
if (themeConfig.overview) {
if (!themeConfig.overview.filterNameText) themeConfig.overview.filterNameText = '';
} else themeConfig.overview = {
filterNameText: ''
};
if (!themeConfig.outlineTitle) themeConfig.outlineTitle = 'Contents';
if (!themeConfig.searchNoResultsText) themeConfig.searchNoResultsText = 'No results found, try something different than';
if (!themeConfig.searchSuggestedQueryText) themeConfig.searchSuggestedQueryText = '';
return themeConfig;
}
function normalizeOptions(options) {
var _options_links, _options_links1, _options_links2;
return {
links: {
homeBanner: (null == (_options_links = options.links) ? void 0 : _options_links.homeBanner) ?? 'https://callstack.com',
homeFooter: (null == (_options_links1 = options.links) ? void 0 : _options_links1.homeFooter) ?? 'https://callstack.com',
outlineCTA: (null == (_options_links2 = options.links) ? void 0 : _options_links2.outlineCTA) ?? 'https://callstack.com'
}
};
}
function pluginCallstackTheme(options = {}) {
const normalizedOptions = normalizeOptions(options);
return {
name: 'plugin-callstack-theme',
builderConfig: getBuilderConfig(normalizedOptions),
config: (config)=>{
config.themeConfig = addThemeOverrides(config.themeConfig);
return config;
},
globalStyles: node_path.join(node_path.dirname(dirname), 'styles/styles.css')
};
}
export { pluginCallstackTheme };