UNPKG

myst-cli

Version:
140 lines (139 loc) 4.91 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) { return [...(state.local.config.projectParts[resolve(path)] ?? [])]; } export function selectFileParts(state, file) { return [...(state.local.config.fileParts[resolve(file)] ?? [])]; } 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) { return [...(state.local.config.configExtensions ?? [])]; } export function selectReloadingState(state) { const { reloading, reloadRequested } = state.local.watch; return { reloading, reloadRequested }; } export function selectFileInfo(state, path) { const { title, short_title, description, date, thumbnail, thumbnailOptimized, banner, bannerOptimized, tags, sha256, url, dataUrl, } = state.local.watch.files[resolve(path)] ?? {}; return { title, short_title, description, date, thumbnail, thumbnailOptimized, banner, bannerOptimized, tags, sha256, url, dataUrl, }; } export function selectFileDependencies(state, path) { return state.local.watch.files[resolve(path)]?.localDependencies ?? []; } export function selectAllDependencies(state, projectPath) { return Object.entries(state.local.watch.files) .filter(([file]) => !projectPath || file.startsWith(resolve(projectPath))) .map(([, value]) => value.localDependencies ?? []) .flat(); } export function selectDependentFiles(state, path) { const dependentFiles = []; Object.entries(state.local.watch.files).forEach(([key, value]) => { if (value.localDependencies?.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?.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; }