Skip to content

Commit

Permalink
🔄 Fix circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkoch committed Jan 28, 2025
1 parent 57fff36 commit aa26dd9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 50 deletions.
54 changes: 10 additions & 44 deletions packages/myst-cli/src/build/site/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ import type { RootState } from '../../store/index.js';
import { selectors } from '../../store/index.js';
import { transformBanner, transformThumbnail } from '../../transforms/images.js';
import { addWarningForFile } from '../../utils/addWarningForFile.js';
import { fileTitle } from '../../utils/fileInfo.js';
import { resolveFrontmatterParts } from '../../utils/resolveFrontmatterParts.js';
import version from '../../version.js';
import { getSiteTemplate } from './template.js';
import { collectExportOptions } from '../utils/collectExportOptions.js';
import { filterPages } from '../../project/load.js';
import { getRawFrontmatterFromFile } from '../../process/file.js';
import { castSession } from '../../session/cache.js';

type ManifestProject = Required<SiteManifest>['projects'][0];
import type { ManifestProject } from '../utils/projectManifest.js';
import {
indexFrontmatterFromProject,
manifestPagesFromProject,
manifestTitleFromProject,
} from '../utils/projectManifest.js';

export async function resolvePageExports(session: ISession, file: string): Promise<SiteExport[]> {
const exports = (
Expand Down Expand Up @@ -134,45 +136,9 @@ export async function localToManifestProject(
const proj = selectors.selectLocalProject(state, projectPath);
if (!proj) return null;
// Update all of the page title to the frontmatter title
const { index, file: indexFile } = proj;
const { index } = proj;
const projectFileInfo = selectors.selectFileInfo(state, proj.file);
const projectTitle = projConfig?.title || projectFileInfo.title || proj.index;
const cache = castSession(session);
const pages = await Promise.all(
proj.pages.map(async (page) => {
if ('file' in page) {
const fileInfo = selectors.selectFileInfo(state, page.file);
const title = fileInfo.title || fileTitle(page.file);
const short_title = fileInfo.short_title ?? undefined;
const description = fileInfo.description ?? '';
const thumbnail = fileInfo.thumbnail ?? '';
const thumbnailOptimized = fileInfo.thumbnailOptimized ?? '';
const banner = fileInfo.banner ?? '';
const bannerOptimized = fileInfo.bannerOptimized ?? '';
const date = fileInfo.date ?? '';
const tags = fileInfo.tags ?? [];
const { slug, level, file } = page;
const { frontmatter } = cache.$getMdast(file)?.post ?? {};
const projectPage: ManifestProject['pages'][0] = {
slug,
title,
short_title,
description,
date,
thumbnail,
thumbnailOptimized,
banner,
bannerOptimized,
tags,
level,
enumerator: frontmatter?.enumerator,
};
return projectPage;
}
return { ...page };
}),
);

const pages = await manifestPagesFromProject(session, projectPath);
const projFrontmatter = projConfig ? filterKeys(projConfig, PROJECT_FRONTMATTER_KEYS) : {};
const projConfigFile = selectors.selectLocalConfigFile(state, projectPath);
const exports = projConfigFile ? await resolvePageExports(session, projConfigFile) : [];
Expand All @@ -195,7 +161,7 @@ export async function localToManifestProject(
session.publicPath(),
{ altOutputFolder: '/', webp: true },
);
const { frontmatter } = cache.$getMdast(indexFile)?.post ?? {};
const frontmatter = indexFrontmatterFromProject(session, projectPath);
return {
...projFrontmatter,
// TODO: a null in the project frontmatter should not fall back to index page
Expand All @@ -214,7 +180,7 @@ export async function localToManifestProject(
downloads,
parts,
bibliography: projFrontmatter.bibliography || [],
title: projectTitle || 'Untitled',
title: manifestTitleFromProject(session, projectPath),
slug: projectSlug,
index,
enumerator: frontmatter?.enumerator,
Expand Down
73 changes: 73 additions & 0 deletions packages/myst-cli/src/build/utils/projectManifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { SiteManifest } from 'myst-config';
import { castSession } from '../../session/cache.js';
import type { ISession } from '../../session/types.js';
import { selectors } from '../../store/index.js';
import { fileTitle } from '../../utils/fileInfo.js';
import type { PageFrontmatter } from 'myst-frontmatter';

export type ManifestProject = Required<SiteManifest>['projects'][0];

export async function manifestPagesFromProject(session: ISession, projectPath: string) {
const state = session.store.getState();
const proj = selectors.selectLocalProject(state, projectPath);
if (!proj) return [];
const cache = castSession(session);
const pages = await Promise.all(
proj.pages.map(async (page) => {
if ('file' in page) {
const fileInfo = selectors.selectFileInfo(state, page.file);
const title = fileInfo.title || fileTitle(page.file);
const short_title = fileInfo.short_title ?? undefined;
const description = fileInfo.description ?? '';
const thumbnail = fileInfo.thumbnail ?? '';
const thumbnailOptimized = fileInfo.thumbnailOptimized ?? '';
const banner = fileInfo.banner ?? '';
const bannerOptimized = fileInfo.bannerOptimized ?? '';
const date = fileInfo.date ?? '';
const tags = fileInfo.tags ?? [];
const { slug, level, file } = page;
const { frontmatter } = cache.$getMdast(file)?.post ?? {};
const projectPage: ManifestProject['pages'][0] = {
slug,
title,
short_title,
description,
date,
thumbnail,
thumbnailOptimized,
banner,
bannerOptimized,
tags,
level,
enumerator: frontmatter?.enumerator,
};
return projectPage;
}
return { ...page };
}),
);
return pages;
}

export function manifestTitleFromProject(session: ISession, projectPath: string) {
const state = session.store.getState();
const projConfig = selectors.selectLocalProjectConfig(state, projectPath);
if (projConfig?.title) return projConfig.title;
const proj = selectors.selectLocalProject(state, projectPath);
if (!proj) return 'Untitled';
const projectFileInfo = selectors.selectFileInfo(session.store.getState(), proj.file);
return projectFileInfo.title || proj.index || 'Untitled';
}

export function indexFrontmatterFromProject(
session: ISession,
projectPath: string,
): PageFrontmatter {
const state = session.store.getState();
const cache = castSession(session);
const proj = selectors.selectLocalProject(state, projectPath);
if (!proj) return {};
const { file } = proj;
const { frontmatter } = cache.$getMdast(file)?.post ?? {};
return frontmatter ?? {};
}
15 changes: 9 additions & 6 deletions packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ import { kernelExecutionTransform, LocalDiskCache } from 'myst-execute';
import type { IOutput } from '@jupyterlab/nbformat';
import { rawDirectiveTransform } from '../transforms/raw.js';
import { addEditUrl } from '../utils/addEditUrl.js';
import { localToManifestProject } from '../build/index.js';
import {
indexFrontmatterFromProject,
manifestPagesFromProject,
manifestTitleFromProject,
} from '../build/utils/projectManifest.js';

const LINKS_SELECTOR = 'link,card,linkBlock';

Expand Down Expand Up @@ -314,20 +318,19 @@ export async function postProcessMdast(
const projectPath = selectors.selectCurrentProjectPath(storeState);
const siteConfig = selectors.selectCurrentSiteConfig(storeState);
const projectSlug = siteConfig?.projects?.find((proj) => proj.path === projectPath)?.slug;
const manifestProject = await localToManifestProject(session, projectPath, projectSlug);
if (site) {
buildTocTransform(
mdast,
vfile,
manifestProject
projectPath
? [
{
title: manifestProject.title,
title: manifestTitleFromProject(session, projectPath),
level: 1,
slug: '',
enumerator: manifestProject.enumerator,
enumerator: indexFrontmatterFromProject(session, projectPath).enumerator,
},
...manifestProject.pages,
...(await manifestPagesFromProject(session, projectPath)),
]
: undefined,
projectSlug,
Expand Down

0 comments on commit aa26dd9

Please sign in to comment.