-
Notifications
You must be signed in to change notification settings - Fork 12
Update parsing to work with the more generic project-felt markdown repo #232
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,7 @@ import type { CollectionDefinition } from '../cli/getConfig' | |
| import { convertToMDX } from '../cli/convertToMDX' | ||
|
|
||
| function defineContent(contentObj: CollectionDefinition) { | ||
| const { base, packageName, pattern, name } = contentObj | ||
|
|
||
| const { base, packageName, pattern, name, frontmatterDefaults, frontmatterMapping } = contentObj | ||
|
|
||
| if (!base && !packageName) { | ||
| // eslint-disable-next-line no-console | ||
|
|
@@ -24,26 +23,57 @@ function defineContent(contentObj: CollectionDefinition) { | |
| convertToMDX(`${base}/${pattern}`) | ||
| const mdxPattern = pattern.replace(/\.md$/, '.mdx') | ||
|
|
||
| const hasExternalFrontmatter = !!(frontmatterDefaults || frontmatterMapping) | ||
|
|
||
| const baseSchema = z.object({ | ||
| id: hasExternalFrontmatter ? z.string().optional() : z.string(), | ||
| section: hasExternalFrontmatter ? z.string().optional() : z.string(), | ||
| subsection: z.string().optional(), | ||
| title: z.string().optional(), | ||
| // Generic frontmatter fields from external sources | ||
| category: z.string().optional(), | ||
| subcategory: z.string().optional(), | ||
| description: z.string().optional(), | ||
| tags: z.array(z.string()).optional(), | ||
| propComponents: z.array(z.string()).optional(), | ||
| tab: z.string().optional().default(tabMap[name]), // for component tabs | ||
| source: z.string().optional(), | ||
| tabName: z.string().optional(), | ||
| sortValue: z.number().optional(), // used for sorting nav entries, | ||
| cssPrefix: z | ||
| .union([ | ||
| z.string().transform((val: string) => [val]), | ||
| z.array(z.string()), | ||
| z.null().transform(() => undefined), | ||
| ]) | ||
| .optional(), | ||
| }).transform((data) => { | ||
| const result: Record<string, unknown> = { ...data } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s only for TS's view of the data after applying config-based mapping/defaults |
||
|
|
||
| // Apply frontmatter mapping (e.g. { title: "id" } maps the title value to id) | ||
| if (frontmatterMapping) { | ||
| for (const [sourceField, targetField] of Object.entries(frontmatterMapping)) { | ||
| if (result[sourceField] != null && result[targetField] == null) { | ||
| result[targetField] = result[sourceField] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Apply frontmatter defaults (e.g. { section: "AI" } sets section if not already present) | ||
| if (frontmatterDefaults) { | ||
| for (const [field, value] of Object.entries(frontmatterDefaults)) { | ||
| if (result[field] == null) { | ||
| result[field] = value | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| }) | ||
|
|
||
| return defineCollection({ | ||
| loader: glob({ base, pattern: mdxPattern }), | ||
| schema: z.object({ | ||
| id: z.string(), | ||
| section: z.string(), | ||
| subsection: z.string().optional(), | ||
| title: z.string().optional(), | ||
| propComponents: z.array(z.string()).optional(), | ||
| tab: z.string().optional().default(tabMap[name]), // for component tabs | ||
| source: z.string().optional(), | ||
| tabName: z.string().optional(), | ||
| sortValue: z.number().optional(), // used for sorting nav entries, | ||
| cssPrefix: z | ||
| .union([ | ||
| z.string().transform((val: string) => [val]), | ||
| z.array(z.string()), | ||
| z.null().transform(() => undefined), | ||
| ]) | ||
| .optional(), | ||
| }), | ||
| schema: baseSchema, | ||
| }) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.