Files
website/node_modules/@astrojs/starlight/utils/createPathFormatter.ts
2024-05-06 17:15:30 -04:00

58 lines
1.4 KiB
TypeScript

import type { AstroConfig } from 'astro';
import { fileWithBase, pathWithBase } from './base';
import {
ensureHtmlExtension,
ensureTrailingSlash,
stripHtmlExtension,
stripTrailingSlash,
} from './path';
interface FormatPathOptions {
format?: AstroConfig['build']['format'];
trailingSlash?: AstroConfig['trailingSlash'];
}
const formatStrategies = {
file: {
addBase: fileWithBase,
handleExtension: (href: string) => ensureHtmlExtension(href),
},
directory: {
addBase: pathWithBase,
handleExtension: (href: string) => stripHtmlExtension(href),
},
};
const trailingSlashStrategies = {
always: ensureTrailingSlash,
never: stripTrailingSlash,
ignore: (href: string) => href,
};
/** Format a path based on the project config. */
function formatPath(
href: string,
{ format = 'directory', trailingSlash = 'ignore' }: FormatPathOptions
) {
const formatStrategy = formatStrategies[format];
const trailingSlashStrategy = trailingSlashStrategies[trailingSlash];
// Add base
href = formatStrategy.addBase(href);
// Handle extension
href = formatStrategy.handleExtension(href);
// Skip trailing slash handling for `build.format: 'file'`
if (format === 'file') return href;
// Handle trailing slash
href = href === '/' ? href : trailingSlashStrategy(href);
return href;
}
export function createPathFormatter(opts: FormatPathOptions) {
return (href: string) => formatPath(href, opts);
}