Skip to content

Commit

Permalink
Merge branch 'main' into hot-reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
rufuspollock authored Dec 14, 2023
2 parents 1b3a761 + acb5183 commit 60792bb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"remark-gfm": "^3.0.1",
"remark-parse": "^10.0.1",
"sqlite3": "^5.1.6",
"unist-util-select": "^4.0.3"
"unist-util-select": "^4.0.3",
"zod": "^3.22.4"
},
"devDependencies": {
"@changesets/changelog-github": "^0.4.8",
Expand Down
7 changes: 6 additions & 1 deletion src/lib/CustomConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { FileInfo } from "./process.js";
import { Root } from "remark-parse/lib/index.js";
import { ZodObject } from "zod";

type ComputedFields = ((fileInfo: FileInfo, ast: Root) => any)[];

Check warning on line 5 in src/lib/CustomConfig.ts

View workflow job for this annotation

GitHub Actions / Lint & format check

Unexpected any. Specify a different type
type Schemas = { [index: string]: ZodObject<any> };

Check warning on line 6 in src/lib/CustomConfig.ts

View workflow job for this annotation

GitHub Actions / Lint & format check

Unexpected any. Specify a different type

export interface CustomConfig {
computedFields?: ((fileInfo: FileInfo, ast: Root) => any)[];
computedFields?: ComputedFields;
schemas?: Schemas;
}
29 changes: 29 additions & 0 deletions src/lib/indexFolder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ZodError } from "zod";
import { CustomConfig } from "./CustomConfig.js";
import { FileInfo, processFile } from "./process.js";
import { recursiveWalkDir } from "./recursiveWalkDir.js";
Expand All @@ -14,6 +15,7 @@ export function indexFolder(
);
const files: FileInfo[] = [];
const computedFields = config.computedFields || [];
const schemas = config.schemas;
for (const filePath of filteredFilePathsToIndex) {
const fileObject = processFile(
folderPath,
Expand All @@ -22,6 +24,33 @@ export function indexFolder(
filePathsToIndex,
computedFields
);
const urlPath = fileObject?.url_path ?? "";
// This is temporary.
// Note: Subject to change pending agreement on the final structure of document types.
const flattenedFileObject = {
...fileObject,
...fileObject.metadata,
tags: fileObject.tags, // Don't override the tags
};
const documentType = urlPath.split("/")[0];

if (schemas && schemas[documentType]) {
const result = schemas[documentType].safeParse(flattenedFileObject);

if (!result.success) {
const error: ZodError = (result as any).error;

Check warning on line 41 in src/lib/indexFolder.ts

View workflow job for this annotation

GitHub Actions / Lint & format check

Unexpected any. Specify a different type

error.errors.forEach((err) => {
const errorMessage = `Error: In ${
fileObject.file_path
} for the ${documentType} schema. \n In "${err.path.join(
","
)}" field: ${err.message}`;
console.error(errorMessage);
});
}
}

files.push(fileObject);
}
return files;
Expand Down

0 comments on commit 60792bb

Please sign in to comment.