UNPKG

alinea

Version:
100 lines (98 loc) 3.73 kB
import "../../chunks/chunk-NZLE2WMY.js"; // src/core/util/EntryFilenames.ts import * as paths from "alinea/core/util/Paths"; import { entryStatuses } from "../Entry.js"; import { ALT_STATUS } from "../Entry.js"; import { getRoot, getType } from "../Internal.js"; import { Workspace } from "../Workspace.js"; import { join } from "./Paths.js"; function workspaceMediaDir(config, workspace) { return Workspace.data(config.workspaces[workspace])?.mediaDir ?? ""; } function entryInfo(fileName) { const status = ALT_STATUS.find((s) => fileName.endsWith(`.${s}`)); if (status) return [fileName.slice(0, -status.length - 1), status]; return [fileName, "published"]; } function entryChildrenDir(config, entry, parentPaths) { const workspace = config.workspaces[entry.workspace]; if (!workspace) throw new Error(`Workspace "${entry.workspace}" does not exist`); const root = Workspace.roots(workspace)[entry.root]; if (!root) throw new Error(`Root "${entry.root}" does not exist`); const hasI18n = getRoot(root).i18n; const { locale, path, status } = entry; if (hasI18n && !locale) throw new Error("Entry is missing locale"); if (!entryStatuses.includes(status)) throw new Error(`Entry has unknown phase: ${status}`); return `/${(locale ? [locale.toLowerCase()] : []).concat( parentPaths.concat(path).map((segment) => segment === "" ? "index" : segment) ).join("/")}`; } function entryFilepath(config, entry, parentPaths) { const { status } = entry; if (!entryStatuses.includes(status)) throw new Error(`Entry has unknown phase: ${status}`); const statusSegment = status === "published" ? "" : `.${status}`; const location = `${entryChildrenDir(config, entry, parentPaths) + statusSegment}.json`.toLowerCase(); return location; } function entryFileName(config, entry, parentPaths) { const workspace = config.workspaces[entry.workspace]; if (!workspace) throw new Error(`Workspace "${entry.workspace}" does not exist`); const { source: contentDir } = Workspace.data(workspace); const root = Workspace.roots(workspace)[entry.root]; if (!root) throw new Error(`Root "${entry.root}" does not exist`); return join(contentDir, entry.root, entryFilepath(config, entry, parentPaths)); } function entryFile(config, entry) { const workspace = config.workspaces[entry.workspace]; if (!workspace) throw new Error(`Workspace "${entry.workspace}" does not exist`); const filePath = entry.filePath; const { source: contentDir } = Workspace.data(workspace); const root = Workspace.roots(workspace)[entry.root]; if (!root) throw new Error(`Root "${entry.root}" does not exist`); return join(contentDir, entry.root, filePath); } function entryUrl(type, meta) { const { entryUrl: entryUrl2 } = getType(type); if (entryUrl2) return entryUrl2(meta); const segments = meta.locale ? [meta.locale.toLowerCase()] : []; return `/${segments.concat( meta.parentPaths.concat(meta.path).filter((segment) => segment !== "index" && segment !== "") ).join("/")}`; } function pathSuffix(path, conflictingPaths) { if (conflictingPaths.includes(path)) { let suffix = 0; while (true) if (!conflictingPaths.includes(`${path}-${++suffix}`)) return suffix; } } function applySuffix(path, suffix) { return `${path}-${suffix}`; } function fileVersions(file) { const dir = paths.dirname(file); const base = paths.basename(file, ".json"); const [name] = entryInfo(base); return [ `${dir}/${name}.json`, `${dir}/${name}.draft.json`, `${dir}/${name}.archived.json` ]; } export { applySuffix, entryChildrenDir, entryFile, entryFileName, entryFilepath, entryInfo, entryUrl, fileVersions, pathSuffix, workspaceMediaDir };