Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#55 , Implement document types #89

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 All @@ -69,4 +70,4 @@
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
}
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 @@
);
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 @@
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
Loading