Skip to content

Commit

Permalink
Make linter and checker happy, change operation slugs, handle types
Browse files Browse the repository at this point in the history
  • Loading branch information
stalgiag committed Feb 29, 2024
1 parent 932b000 commit d6b57e0
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 175 deletions.
30 changes: 16 additions & 14 deletions src/content/reference/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ const returnSchema = z.object({

const exampleSchema = z.string();

export const referenceSchema = z.object({
title: z.string(),
module: z.string(),
submodule: z.string(),
file: z.string(),
description: z.string(),
line: z.number(),
params: z.array(paramSchema).optional(),
itemtype: z.string().optional(),
class: z.string().optional(),
chainable: z.boolean().optional(),
return: returnSchema.optional(),
example: z.array(exampleSchema).optional(),
});

export const referenceCollection = defineCollection({
type: "content",
schema: z.object({
title: z.string(),
module: z.string(),
submodule: z.string(),
file: z.string(),
description: z.string(),
line: z.number(),
params: z.array(paramSchema).optional(),
itemtype: z.string().optional(),
class: z.string().optional(),
chainable: z.boolean().optional(),
return: returnSchema.optional(),
example: z.array(exampleSchema).optional(),
}),
schema: referenceSchema,
});
34 changes: 0 additions & 34 deletions src/content/reference/en/p5/>.mdx

This file was deleted.

33 changes: 0 additions & 33 deletions src/content/reference/en/p5/>=.mdx

This file was deleted.

33 changes: 0 additions & 33 deletions src/content/reference/en/p5/<.mdx

This file was deleted.

33 changes: 0 additions & 33 deletions src/content/reference/en/p5/<=.mdx

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/content/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { referenceCollection } from "./reference/config";
import type { z } from "astro/zod";
import { referenceSchema } from "./reference/config";

type AstroBaseContentType = {
id: string;
slug: string;
body: string;
render: function;
collection: string;
};

type WithAstroBase<T> = T & AstroBaseContentType;

export type ReferenceDocContentItem = WithAstroBase<
z.infer<typeof referenceCollection>
z.infer<typeof referenceSchema>
>;
8 changes: 6 additions & 2 deletions src/pages/reference/[...id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ export async function getStaticPaths() {
const { id } = Astro.params;
const referenceEntries = await getCollection("reference");
const entriesById = referenceEntries.reduce((acc, entry) => {
acc[entry.id.replace(/\.mdx$/, "")] = entry;
// TODO: Find correct way to handle Zod -> TS type inference
const entriesById: Record<string, any> = referenceEntries.reduce<
Record<string, any>
>((acc, entry) => {
const id = entry.id.replace(/\.mdx$/, "");
acc[id] = entry;
return acc;
}, {});
Expand Down
3 changes: 1 addition & 2 deletions src/pages/reference/index.astro
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
import { getCollection } from "astro:content";
import type { ReferenceDocContentItem } from "../../content/types";
const referenceEntries = await getCollection("reference");
---

<ul>
{
referenceEntries.map((refEntry: ReferenceDocContentItem) => (
referenceEntries.map((refEntry) => (
<li>
<a href={`/reference/${refEntry.id.replace(/\.mdx$/, "")}`}>
{refEntry.data.title}
Expand Down
5 changes: 0 additions & 5 deletions src/pages/reference/utils.ts

This file was deleted.

44 changes: 29 additions & 15 deletions src/scripts/builders/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
ReferenceMDXDoc,
ReferenceModulePathTree,
} from "../../../types/builders.interface";
import { sanitizeName } from "../utils";

/* Base path for the content directory */
const prefix = "./src/content/reference/en/";
Expand Down Expand Up @@ -126,10 +127,6 @@ const convertToMDX = async (
) => {
if (!doc || !doc.name || !doc.file) return;

// Some names contain characters that need to be sanitized for MDX
const sanitizeName = (name: string) =>
name.replace(/</g, "&lt;").replace(/>/g, "&gt;");

let frontMatterArgs = {
title: sanitizeName(doc.name),
module: doc.module,
Expand Down Expand Up @@ -231,17 +228,18 @@ const getClassFrontmatter = (doc: ReferenceClassDefinition) => {
};
};

// TODO: Add back in when the pathing issue is resolved
/* Adds class method previews to the class docs */
const addMemberMethodPreviewsToClassDocs = (doc: ReferenceClassItemMethod) => {
if (!memberMethodPreviews[doc.class]) {
memberMethodPreviews[doc.class] = {};
}
const classMethodPath = `../${modulePathTree.classes[doc.class][doc.name]}`;
memberMethodPreviews[doc.class][doc.name] = {
description: doc.description,
path: classMethodPath,
};
};
// const addMemberMethodPreviewsToClassDocs = (doc: ReferenceClassItemMethod) => {
// if (!memberMethodPreviews[doc.class]) {
// memberMethodPreviews[doc.class] = {};
// }
// const classMethodPath = `../${modulePathTree.classes[doc.class][doc.name]}`;
// memberMethodPreviews[doc.class][doc.name] = {
// description: doc.description,
// path: classMethodPath,
// };
// };

/* Converts all docs to MDX */
const convertDocsToMDX = async (
Expand Down Expand Up @@ -277,8 +275,24 @@ const saveMDX = async (mdxDocs: ReferenceMDXDoc[]) => {
console.log("Saving MDX...");
for (const { mdx, savePath, name } of mdxDocs) {
try {
let fileName = sanitizeName(name);
// TODO: Place elsewhere
if (fileName[0] === "&") {
// Need special cases for >, >=, <, <=, and ===
if (fileName === "&gt;") {
fileName = "gt";
} else if (fileName === "&gt;=") {
fileName = "gte";
} else if (fileName === "&lt;") {
fileName = "lt";
} else if (fileName === "&lt;=") {
fileName = "lte";
} else if (fileName === "&equals;") {
fileName = "equals";
}
}
await fs.mkdir(savePath, { recursive: true });
await fs.writeFile(`${savePath}/${name}.mdx`, mdx.toString());
await fs.writeFile(`${savePath}/${fileName}.mdx`, mdx.toString());
} catch (err) {
console.error(`Error saving MDX: ${err}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/parsers/reference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cloneLibraryRepo, readFile } from "./utils";
import { cloneLibraryRepo, readFile } from "../utils";
import { exec } from "child_process";
import path from "path";
import { fileURLToPath } from "url";
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/parsers/utils.ts → src/scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ export const fixAbsolutePathInPreprocessor = async (localSavePath: string) => {
return false;
}
};

/* Some names contain characters that need to be sanitized for pathing, MDX, etc. */
export const sanitizeName = (name: string) =>
name.replace(/</g, "&lt;").replace(/>/g, "&gt;");

0 comments on commit d6b57e0

Please sign in to comment.