import os from "node:os" import path from "path" import { fileExists } from "./file-system.js" export function resolvePath(unresolvedPath) { return path.resolve(unresolvedPath.replace(/^~/, os.homedir())) } export async function firstFound(dirs, fileName) { // Note: This function depends on fileExists from file-system.js // Import it when using this function: import { fileExists } from './file-system.js' for (const dir of dirs) { const filePath = resolvePath(path.join(dir, fileName)) const exists = await fileExists(filePath) if (exists) { return filePath } } return null } export function removeCwd(paths) { const cwd = `${process.cwd()}/` return paths.map(path => path.replace(cwd, "")) } export function removeBasePaths(baseDirs, fullPath) { return baseDirs.reduce((cleanedPath, dir) => { return cleanedPath.replace(dir, "") }, fullPath) } export function replaceFileExtension(filePath, newExtension) { if (!newExtension) { return filePath } return `${stripFileExtension(filePath)}${newExtension}` } export function stripFileExtension(filePath) { return path.join( path.dirname(filePath), path.basename(filePath, path.extname(filePath)), ) } export function getCleanPath(filePath, meta) { return filePath.replace(meta.opts.runDir, "").replace(meta.opts.outDir, "/") } export function getHref(filePath, meta) { const route = getCleanPath(filePath, meta) if (route.includes("index.html")) { return route.replace("index.html", "") } return route.replace(".html", "") } export function slugifyString(str) { return str .toLowerCase() .trim() .replace(/[/\\?%@*:|"<>]/g, "-") // Replace invalid filename characters .replace(/\s+/g, "-") // Replace whitespace with dashes .replace(/-+/g, "-") // Collapse multiple dashes .replace(/\./g, "-") // Replace dots with dashes .replace(/^-+|-+$/g, "") // Trim leading/trailing dashes }