UNPKG

myst-cli

Version:
146 lines (145 loc) 5.37 kB
import { resolve } from 'node:path'; function mutableCopy(obj) { if (!obj) return; return JSON.parse(JSON.stringify(obj)); } export function selectLocalProject(state, path) { return mutableCopy(state.local.projects[resolve(path)]); } export function selectAffiliation(state, id) { return state.local.affiliations[id]; } export function selectLocalSiteConfig(state, path) { return mutableCopy(state.local.config.sites[resolve(path)]); } export function selectCurrentSiteConfig(state) { if (!state.local.config.currentSitePath) return undefined; const config = state.local.config.sites[resolve(state.local.config.currentSitePath)]; const path = selectCurrentProjectPath(state); if (config.projects || !path) return config; return { ...config, projects: [{ path }] }; } export function selectCurrentSitePath(state) { return state.local.config.currentSitePath; } export function selectCurrentSiteFile(state) { if (!state.local.config.currentSitePath) return undefined; return state.local.config.filenames[resolve(state.local.config.currentSitePath)]; } export function selectLocalProjectConfig(state, path) { return mutableCopy(state.local.config.projects[resolve(path)]); } export function selectCurrentProjectConfig(state) { if (!state.local.config.currentProjectPath) return undefined; return mutableCopy(state.local.config.projects[resolve(state.local.config.currentProjectPath)]); } export function selectProjectParts(state, path) { var _a; return [...((_a = state.local.config.projectParts[resolve(path)]) !== null && _a !== void 0 ? _a : [])]; } export function selectFileParts(state, file) { var _a; return [...((_a = state.local.config.fileParts[resolve(file)]) !== null && _a !== void 0 ? _a : [])]; } export function selectFileFromPart(state, partFile) { let file; Object.entries(state.local.config.fileParts).forEach(([key, value]) => { if (file) return; if (value.includes(partFile)) file = key; }); return file; } export function selectCurrentProjectPath(state) { return state.local.config.currentProjectPath; } export function selectCurrentProjectFile(state) { if (!state.local.config.currentProjectPath) return undefined; return state.local.config.filenames[resolve(state.local.config.currentProjectPath)]; } export function selectLocalConfigFile(state, path) { return state.local.config.filenames[resolve(path)]; } export function selectLocalRawConfig(state, path) { return mutableCopy(state.local.config.rawConfigs[resolve(path)]); } export function selectConfigExtensions(state) { var _a; return [...((_a = state.local.config.configExtensions) !== null && _a !== void 0 ? _a : [])]; } export function selectReloadingState(state) { const { reloading, reloadRequested } = state.local.watch; return { reloading, reloadRequested }; } export function selectFileInfo(state, path) { var _a; const { title, short_title, description, date, thumbnail, thumbnailOptimized, banner, bannerOptimized, tags, sha256, url, dataUrl, } = (_a = state.local.watch.files[resolve(path)]) !== null && _a !== void 0 ? _a : {}; return { title, short_title, description, date, thumbnail, thumbnailOptimized, banner, bannerOptimized, tags, sha256, url, dataUrl, }; } export function selectFileDependencies(state, path) { var _a, _b; return (_b = (_a = state.local.watch.files[resolve(path)]) === null || _a === void 0 ? void 0 : _a.localDependencies) !== null && _b !== void 0 ? _b : []; } export function selectAllDependencies(state, projectPath) { return Object.entries(state.local.watch.files) .filter(([file]) => !projectPath || file.startsWith(resolve(projectPath))) .map(([, value]) => { var _a; return (_a = value.localDependencies) !== null && _a !== void 0 ? _a : []; }) .flat(); } export function selectDependentFiles(state, path) { const dependentFiles = []; Object.entries(state.local.watch.files).forEach(([key, value]) => { var _a; if ((_a = value.localDependencies) === null || _a === void 0 ? void 0 : _a.includes(resolve(path))) { dependentFiles.push(key); } }); return dependentFiles; } export function selectPageSlug(state, projectPath, path) { const project = selectLocalProject(state, projectPath); if (!project) return undefined; if (path === project.file) return project.index; const found = project.pages .filter((page) => 'file' in page) .find(({ file }) => file === path); return found === null || found === void 0 ? void 0 : found.slug; } export function selectLinkStatus(state, url) { return mutableCopy(state.local.links[url]); } export function selectFileWarnings(state, file) { return mutableCopy(state.local.warnings[file]); } export function selectFileWarningsByRule(state, ruleId) { const ruleWarnings = []; Object.entries(state.local.warnings).forEach(([file, warnings]) => { warnings.forEach((warning) => { if (warning.ruleId === ruleId) ruleWarnings.push({ file, ...warning }); }); }); return ruleWarnings; }