Skip to content

Commit

Permalink
chore: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed May 22, 2024
1 parent a210d14 commit ef1029a
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions packages/myst-cli/src/upgrade/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolveExtension } from '../utils/resolveExtension.js';
import { defined } from './utils.js';
import { join, relative } from 'node:path';
import { cwd } from 'node:process';

import type { Entry as MySTEntry } from 'myst-toc';
const TOCTreeOptions = z
.object({
caption: z.string(),
Expand All @@ -15,16 +15,19 @@ const TOCTreeOptions = z
})
.partial();

type FileEntry = z.infer<typeof FileEntry>;
const FileEntry = z.object({
file: z.string(),
title: z.string().optional(),
});

type URLEntry = z.infer<typeof URLEntry>;
const URLEntry = z.object({
url: z.string(),
title: z.string().optional(),
});

type GlobEntry = z.infer<typeof GlobEntry>;
const GlobEntry = z.object({
glob: z.string(),
});
Expand Down Expand Up @@ -185,17 +188,10 @@ export function validateSphinxExternalTOC(toc: unknown): SphinxExternalTOC | und
}
}

function convertGeneric(dir: string, data: Record<string, unknown>): any {
// TODO: handle numbering
if ('parts' in data || 'subtrees' in data) {
const parts = (data.parts ?? data.subtrees) as Record<string, unknown>[];
return parts.map((part, index) => {
return { title: part.caption ?? `Part ${index}`, children: convertGeneric(dir, part) };
});
} else if ('chapters' in data || 'sections' in data) {
const chapters = (data.chapters ?? data.sections) as Record<string, unknown>[];
return chapters.map((chapter) => convertGeneric(dir, chapter));
} else if ('file' in data) {
type PrimitiveEntry = FileEntry | URLEntry | GlobEntry;

function convertPrimitive(dir: string, data: PrimitiveEntry): MySTEntry {
if ('file' in data) {
const resolved = resolveExtension(join(dir, data.file as string));
// TODO: check this is valid!
return {
Expand All @@ -212,7 +208,25 @@ function convertGeneric(dir: string, data: Record<string, unknown>): any {
pattern: data.glob,
};
} else {
throw new Error("This should not happen!");
throw new Error('This should not happen!');
}
}

function convertGeneric(dir: string, data: Record<string, unknown>): any {
// The JB schema is quite complex, so rather than being type-safe here
// we'll drop type-information in order to write something readable

// TODO: handle numbering
if ('parts' in data || 'subtrees' in data) {
const parts = (data.parts ?? data.subtrees) as Record<string, unknown>[];
return parts.map((part, index) => {
return { title: part.caption ?? `Part ${index}`, children: convertGeneric(dir, part) };
});
} else if ('chapters' in data || 'sections' in data) {
const chapters = (data.chapters ?? data.sections) as Record<string, unknown>[];
return chapters.map((chapter) => convertGeneric(dir, chapter));
} else {
return convertPrimitive(dir, data as any);
}
}
export function upgradeTOC(data: SphinxExternalTOC) {
Expand Down

0 comments on commit ef1029a

Please sign in to comment.